@@ -80,14 +80,14 @@ callConsistentFn(RumState *rumstate, RumScanKey key)
80
80
81
81
if (res && key -> attnum == rumstate -> attrnAddToColumn )
82
82
{
83
- int i ;
83
+ uint32 i ;
84
84
85
85
/* remember some addinfo value for later ordering by addinfo
86
86
from another column */
87
87
88
88
key -> outerAddInfoIsNull = true;
89
89
90
- for (i = 0 ; i < key -> nuserentries ; i ++ )
90
+ for (i = 0 ; i < key -> nuserentries ; i ++ )
91
91
{
92
92
if (key -> entryRes [i ] && key -> addInfoIsNull [0 ] == false)
93
93
{
@@ -571,6 +571,10 @@ startScanKey(RumState *rumstate, RumScanKey key)
571
571
key -> isFinished = false;
572
572
}
573
573
574
+ /*
575
+ * Compare entries position. At first consider isFinished flag, then compare
576
+ * item pointers.
577
+ */
574
578
static int
575
579
cmpEntries (RumScanEntry e1 , RumScanEntry e2 )
576
580
{
@@ -670,6 +674,10 @@ startScan(IndexScanDesc scan)
670
674
671
675
if (useFastScan )
672
676
{
677
+ /*
678
+ * We are going to use fast scan. Do some preliminaries. Start scan
679
+ * of each entry and sort entries by descending item pointers.
680
+ */
673
681
so -> sortedEntries = (RumScanEntry * )palloc (sizeof (RumScanEntry ) *
674
682
so -> totalentries );
675
683
memcpy (so -> sortedEntries , so -> entries , sizeof (RumScanEntry ) *
@@ -1237,15 +1245,21 @@ scanGetItemRegular(IndexScanDesc scan, ItemPointer advancePast,
1237
1245
return TRUE;
1238
1246
}
1239
1247
1248
+ /*
1249
+ * Finds part of page containing requested item using small index at the end
1250
+ * of page.
1251
+ */
1240
1252
static bool
1241
- scanPage (RumState * rumstate , RumScanEntry entry , ItemPointer item , Page page , bool equalOk )
1253
+ scanPage (RumState * rumstate , RumScanEntry entry , ItemPointer item , Page page ,
1254
+ bool equalOk )
1242
1255
{
1243
- int j ;
1256
+ int j ;
1244
1257
RumKey iter_item ;
1245
- Pointer ptr ;
1246
- OffsetNumber first = FirstOffsetNumber , i , maxoff ;
1247
- bool found ;
1248
- int cmp ;
1258
+ Pointer ptr ;
1259
+ OffsetNumber first = FirstOffsetNumber ,
1260
+ i , maxoff ;
1261
+ bool found ;
1262
+ int cmp ;
1249
1263
1250
1264
ItemPointerSetMin (& iter_item .iptr );
1251
1265
@@ -1300,6 +1314,9 @@ scanPage(RumState *rumstate, RumScanEntry entry, ItemPointer item, Page page, bo
1300
1314
return true;
1301
1315
}
1302
1316
1317
+ /*
1318
+ * Find item pointer of entry with is greater or equal to given item pointer.
1319
+ */
1303
1320
static void
1304
1321
entryFindItem (RumState * rumstate , RumScanEntry entry , RumKey * item )
1305
1322
{
@@ -1311,6 +1328,7 @@ entryFindItem(RumState *rumstate, RumScanEntry entry, RumKey *item)
1311
1328
return ;
1312
1329
}
1313
1330
1331
+ /* Try to find in loaded part of page */
1314
1332
if (rumCompareItemPointers (& entry -> list [entry -> nlist - 1 ].iptr ,
1315
1333
& item -> iptr ) >= 0 )
1316
1334
{
@@ -1329,13 +1347,13 @@ entryFindItem(RumState *rumstate, RumScanEntry entry, RumKey *item)
1329
1347
}
1330
1348
}
1331
1349
1332
-
1333
1350
if (!BufferIsValid (entry -> buffer ))
1334
1351
{
1335
1352
entry -> isFinished = TRUE;
1336
1353
return ;
1337
1354
}
1338
1355
1356
+ /* Check rest of page */
1339
1357
LockBuffer (entry -> buffer , RUM_SHARE );
1340
1358
1341
1359
if (scanPage (rumstate , entry , & item -> iptr ,
@@ -1346,6 +1364,7 @@ entryFindItem(RumState *rumstate, RumScanEntry entry, RumKey *item)
1346
1364
return ;
1347
1365
}
1348
1366
1367
+ /* Try to traverse to another leaf page */
1349
1368
entry -> gdi -> btree .items = item ;
1350
1369
entry -> gdi -> btree .curitem = 0 ;
1351
1370
@@ -1363,6 +1382,7 @@ entryFindItem(RumState *rumstate, RumScanEntry entry, RumKey *item)
1363
1382
return ;
1364
1383
}
1365
1384
1385
+ /* At last try to traverse by right links */
1366
1386
for (;;)
1367
1387
{
1368
1388
/*
@@ -1402,17 +1422,20 @@ entryFindItem(RumState *rumstate, RumScanEntry entry, RumKey *item)
1402
1422
}
1403
1423
}
1404
1424
1425
+ /*
1426
+ * Do preConsistent check for all the key where applicable.
1427
+ */
1405
1428
static bool
1406
1429
preConsistentCheck (RumScanOpaque so )
1407
1430
{
1408
- RumState * rumstate = & so -> rumstate ;
1409
- int i , j ;
1410
- bool recheck ;
1431
+ RumState * rumstate = & so -> rumstate ;
1432
+ uint32 i , j ;
1433
+ bool recheck ;
1411
1434
1412
1435
for (j = 0 ; j < so -> nkeys ; j ++ )
1413
1436
{
1414
- RumScanKey key = & so -> keys [j ];
1415
- bool hasFalse = false;
1437
+ RumScanKey key = & so -> keys [j ];
1438
+ bool hasFalse = false;
1416
1439
1417
1440
if (key -> orderBy )
1418
1441
continue ;
@@ -1448,29 +1471,43 @@ preConsistentCheck(RumScanOpaque so)
1448
1471
return true;
1449
1472
}
1450
1473
1474
+ /*
1475
+ * Shift value of some entry which index in so->sortedEntries is equal or greater
1476
+ * to i.
1477
+ */
1451
1478
static void
1452
1479
entryShift (int i , RumScanOpaque so , bool find )
1453
1480
{
1454
- int minIndex = -1 , j ;
1455
- uint32 minPredictNumberResult = 0 ;
1481
+ int minIndex = -1 ,
1482
+ j ;
1483
+ uint32 minPredictNumberResult = 0 ;
1456
1484
RumState * rumstate = & so -> rumstate ;
1457
1485
1486
+ /*
1487
+ * It's more efficient to move entry with smallest posting list/tree. So
1488
+ * find one.
1489
+ */
1458
1490
for (j = i ; j < so -> totalentries ; j ++ )
1459
1491
{
1460
- if (minIndex < 0 || so -> sortedEntries [j ]-> predictNumberResult < minPredictNumberResult )
1492
+ if (minIndex < 0 ||
1493
+ so -> sortedEntries [j ]-> predictNumberResult < minPredictNumberResult )
1461
1494
{
1462
1495
minIndex = j ;
1463
1496
minPredictNumberResult = so -> sortedEntries [j ]-> predictNumberResult ;
1464
1497
}
1465
1498
}
1466
1499
1500
+ /* Do shift of required type */
1467
1501
if (find )
1468
- entryFindItem (rumstate , so -> sortedEntries [minIndex ], & so -> sortedEntries [i - 1 ]-> curItem );
1502
+ entryFindItem (rumstate , so -> sortedEntries [minIndex ],
1503
+ & so -> sortedEntries [i - 1 ]-> curItem );
1469
1504
else if (!so -> sortedEntries [minIndex ]-> isFinished )
1470
1505
entryGetItem (rumstate , so -> sortedEntries [minIndex ]);
1471
1506
1507
+ /* Restore order of so->sortedEntries */
1472
1508
while (minIndex > 0 &&
1473
- cmpEntries (so -> sortedEntries [minIndex ], so -> sortedEntries [minIndex - 1 ]) > 0 )
1509
+ cmpEntries (so -> sortedEntries [minIndex ],
1510
+ so -> sortedEntries [minIndex - 1 ]) > 0 )
1474
1511
{
1475
1512
RumScanEntry tmp ;
1476
1513
tmp = so -> sortedEntries [minIndex ];
@@ -1480,13 +1517,16 @@ entryShift(int i, RumScanOpaque so, bool find)
1480
1517
}
1481
1518
}
1482
1519
1520
+ /*
1521
+ * Get next item pointer using fast scan.
1522
+ */
1483
1523
static bool
1484
1524
scanGetItemFast (IndexScanDesc scan , ItemPointer advancePast ,
1485
- ItemPointerData * item , bool * recheck )
1525
+ ItemPointerData * item , bool * recheck )
1486
1526
{
1487
1527
RumScanOpaque so = (RumScanOpaque ) scan -> opaque ;
1488
- int i , j , k ;
1489
- bool preConsistentFalse , consistentFalse ;
1528
+ int i , j , k ;
1529
+ bool preConsistentFalse , consistentFalse ;
1490
1530
1491
1531
if (so -> entriesIncrIndex >= 0 )
1492
1532
{
@@ -1496,6 +1536,10 @@ scanGetItemFast(IndexScanDesc scan, ItemPointer advancePast,
1496
1536
1497
1537
for (;;)
1498
1538
{
1539
+ /*
1540
+ * Our entries is ordered by descending of item pointers.
1541
+ * The first goal is to find border where preConsistent becomes false.
1542
+ */
1499
1543
preConsistentFalse = false;
1500
1544
j = 0 ;
1501
1545
k = 0 ;
@@ -1517,6 +1561,10 @@ scanGetItemFast(IndexScanDesc scan, ItemPointer advancePast,
1517
1561
}
1518
1562
}
1519
1563
1564
+ /*
1565
+ * If we found false in preConsistent then we can safely move entries
1566
+ * which was true in preConsistent argument.
1567
+ */
1520
1568
if (so -> sortedEntries [i - 1 ]-> isFinished == TRUE)
1521
1569
return false;
1522
1570
@@ -1526,6 +1574,7 @@ scanGetItemFast(IndexScanDesc scan, ItemPointer advancePast,
1526
1574
continue ;
1527
1575
}
1528
1576
1577
+ /* Call consistent method */
1529
1578
consistentFalse = false;
1530
1579
for (i = 0 ; i < so -> nkeys ; i ++ )
1531
1580
{
@@ -1563,6 +1612,7 @@ scanGetItemFast(IndexScanDesc scan, ItemPointer advancePast,
1563
1612
if (consistentFalse )
1564
1613
continue ;
1565
1614
1615
+ /* Calculate recheck from each key */
1566
1616
* recheck = false;
1567
1617
for (i = 0 ; i < so -> nkeys ; i ++ )
1568
1618
{
@@ -1586,6 +1636,9 @@ scanGetItemFast(IndexScanDesc scan, ItemPointer advancePast,
1586
1636
return false;
1587
1637
}
1588
1638
1639
+ /*
1640
+ * Get next item whether using regular or fast scan.
1641
+ */
1589
1642
static bool
1590
1643
scanGetItem (IndexScanDesc scan , ItemPointer advancePast ,
1591
1644
ItemPointerData * item , bool * recheck )
@@ -1776,7 +1829,7 @@ collectMatchesForHeapRow(IndexScanDesc scan, pendingPosition *pos)
1776
1829
OffsetNumber attrnum ;
1777
1830
Page page ;
1778
1831
IndexTuple itup ;
1779
- int i ,
1832
+ uint32 i ,
1780
1833
j ;
1781
1834
1782
1835
/*
@@ -1997,7 +2050,7 @@ scanPendingInsert(IndexScanDesc scan)
1997
2050
MemoryContext oldCtx ;
1998
2051
bool recheck ,
1999
2052
match ;
2000
- int i ;
2053
+ uint32 i ;
2001
2054
pendingPosition pos ;
2002
2055
Buffer metabuffer = ReadBuffer (scan -> indexRelation , RUM_METAPAGE_BLKNO );
2003
2056
BlockNumber blkno ;
@@ -2145,7 +2198,7 @@ keyGetOrdering(RumState *rumstate, MemoryContext tempCtx, RumScanKey key,
2145
2198
ItemPointer iptr )
2146
2199
{
2147
2200
RumScanEntry entry ;
2148
- int i ;
2201
+ uint32 i ;
2149
2202
2150
2203
if (key -> useAddToColumn )
2151
2204
{
@@ -2200,7 +2253,7 @@ static void
2200
2253
insertScanItem (RumScanOpaque so , bool recheck )
2201
2254
{
2202
2255
RumSortItem * item ;
2203
- int i , j ;
2256
+ uint32 i , j ;
2204
2257
2205
2258
item = (RumSortItem * )
2206
2259
MemoryContextAlloc (rum_tuplesort_get_memorycontext (so -> sortstate ),
@@ -2297,7 +2350,7 @@ rumgettuple(IndexScanDesc scan, ScanDirection direction)
2297
2350
item = rum_tuplesort_getrum (so -> sortstate , true, & should_free );
2298
2351
if (item )
2299
2352
{
2300
- int i , j = 0 ;
2353
+ uint32 i , j = 0 ;
2301
2354
2302
2355
scan -> xs_ctup .t_self = item -> iptr ;
2303
2356
scan -> xs_recheck = item -> recheck ;
0 commit comments