@@ -1230,11 +1230,6 @@ SimpleLruTruncate(SlruCtl ctl, int cutoffPage)
1230
1230
/* update the stats counter of truncates */
1231
1231
pgstat_count_slru_truncate (shared -> slru_stats_idx );
1232
1232
1233
- /*
1234
- * The cutoff point is the start of the segment containing cutoffPage.
1235
- */
1236
- cutoffPage -= cutoffPage % SLRU_PAGES_PER_SEGMENT ;
1237
-
1238
1233
/*
1239
1234
* Scan shared memory and remove any pages preceding the cutoff page, to
1240
1235
* ensure we won't rewrite them later. (Since this is normally called in
@@ -1247,9 +1242,7 @@ restart:;
1247
1242
1248
1243
/*
1249
1244
* While we are holding the lock, make an important safety check: the
1250
- * planned cutoff point must be <= the current endpoint page. Otherwise we
1251
- * have already wrapped around, and proceeding with the truncation would
1252
- * risk removing the current segment.
1245
+ * current endpoint page must not be eligible for removal.
1253
1246
*/
1254
1247
if (ctl -> PagePrecedes (shared -> latest_page_number , cutoffPage ))
1255
1248
{
@@ -1281,8 +1274,11 @@ restart:;
1281
1274
* Hmm, we have (or may have) I/O operations acting on the page, so
1282
1275
* we've got to wait for them to finish and then start again. This is
1283
1276
* the same logic as in SlruSelectLRUPage. (XXX if page is dirty,
1284
- * wouldn't it be OK to just discard it without writing it? For now,
1285
- * keep the logic the same as it was.)
1277
+ * wouldn't it be OK to just discard it without writing it?
1278
+ * SlruMayDeleteSegment() uses a stricter qualification, so we might
1279
+ * not delete this page in the end; even if we don't delete it, we
1280
+ * won't have cause to read its data again. For now, keep the logic
1281
+ * the same as it was.)
1286
1282
*/
1287
1283
if (shared -> page_status [slotno ] == SLRU_PAGE_VALID )
1288
1284
SlruInternalWritePage (ctl , slotno , NULL );
@@ -1372,19 +1368,134 @@ SlruDeleteSegment(SlruCtl ctl, int segno)
1372
1368
LWLockRelease (shared -> ControlLock );
1373
1369
}
1374
1370
1371
+ /*
1372
+ * Determine whether a segment is okay to delete.
1373
+ *
1374
+ * segpage is the first page of the segment, and cutoffPage is the oldest (in
1375
+ * PagePrecedes order) page in the SLRU containing still-useful data. Since
1376
+ * every core PagePrecedes callback implements "wrap around", check the
1377
+ * segment's first and last pages:
1378
+ *
1379
+ * first<cutoff && last<cutoff: yes
1380
+ * first<cutoff && last>=cutoff: no; cutoff falls inside this segment
1381
+ * first>=cutoff && last<cutoff: no; wrap point falls inside this segment
1382
+ * first>=cutoff && last>=cutoff: no; every page of this segment is too young
1383
+ */
1384
+ static bool
1385
+ SlruMayDeleteSegment (SlruCtl ctl , int segpage , int cutoffPage )
1386
+ {
1387
+ int seg_last_page = segpage + SLRU_PAGES_PER_SEGMENT - 1 ;
1388
+
1389
+ Assert (segpage % SLRU_PAGES_PER_SEGMENT == 0 );
1390
+
1391
+ return (ctl -> PagePrecedes (segpage , cutoffPage ) &&
1392
+ ctl -> PagePrecedes (seg_last_page , cutoffPage ));
1393
+ }
1394
+
1395
+ #ifdef USE_ASSERT_CHECKING
1396
+ static void
1397
+ SlruPagePrecedesTestOffset (SlruCtl ctl , int per_page , uint32 offset )
1398
+ {
1399
+ TransactionId lhs ,
1400
+ rhs ;
1401
+ int newestPage ,
1402
+ oldestPage ;
1403
+ TransactionId newestXact ,
1404
+ oldestXact ;
1405
+
1406
+ /*
1407
+ * Compare an XID pair having undefined order (see RFC 1982), a pair at
1408
+ * "opposite ends" of the XID space. TransactionIdPrecedes() treats each
1409
+ * as preceding the other. If RHS is oldestXact, LHS is the first XID we
1410
+ * must not assign.
1411
+ */
1412
+ lhs = per_page + offset ; /* skip first page to avoid non-normal XIDs */
1413
+ rhs = lhs + (1U << 31 );
1414
+ Assert (TransactionIdPrecedes (lhs , rhs ));
1415
+ Assert (TransactionIdPrecedes (rhs , lhs ));
1416
+ Assert (!TransactionIdPrecedes (lhs - 1 , rhs ));
1417
+ Assert (TransactionIdPrecedes (rhs , lhs - 1 ));
1418
+ Assert (TransactionIdPrecedes (lhs + 1 , rhs ));
1419
+ Assert (!TransactionIdPrecedes (rhs , lhs + 1 ));
1420
+ Assert (!TransactionIdFollowsOrEquals (lhs , rhs ));
1421
+ Assert (!TransactionIdFollowsOrEquals (rhs , lhs ));
1422
+ Assert (!ctl -> PagePrecedes (lhs / per_page , lhs / per_page ));
1423
+ Assert (!ctl -> PagePrecedes (lhs / per_page , rhs / per_page ));
1424
+ Assert (!ctl -> PagePrecedes (rhs / per_page , lhs / per_page ));
1425
+ Assert (!ctl -> PagePrecedes ((lhs - per_page ) / per_page , rhs / per_page ));
1426
+ Assert (ctl -> PagePrecedes (rhs / per_page , (lhs - 3 * per_page ) / per_page ));
1427
+ Assert (ctl -> PagePrecedes (rhs / per_page , (lhs - 2 * per_page ) / per_page ));
1428
+ Assert (ctl -> PagePrecedes (rhs / per_page , (lhs - 1 * per_page ) / per_page )
1429
+ || (1U << 31 ) % per_page != 0 ); /* See CommitTsPagePrecedes() */
1430
+ Assert (ctl -> PagePrecedes ((lhs + 1 * per_page ) / per_page , rhs / per_page )
1431
+ || (1U << 31 ) % per_page != 0 );
1432
+ Assert (ctl -> PagePrecedes ((lhs + 2 * per_page ) / per_page , rhs / per_page ));
1433
+ Assert (ctl -> PagePrecedes ((lhs + 3 * per_page ) / per_page , rhs / per_page ));
1434
+ Assert (!ctl -> PagePrecedes (rhs / per_page , (lhs + per_page ) / per_page ));
1435
+
1436
+ /*
1437
+ * GetNewTransactionId() has assigned the last XID it can safely use, and
1438
+ * that XID is in the *LAST* page of the second segment. We must not
1439
+ * delete that segment.
1440
+ */
1441
+ newestPage = 2 * SLRU_PAGES_PER_SEGMENT - 1 ;
1442
+ newestXact = newestPage * per_page + offset ;
1443
+ Assert (newestXact / per_page == newestPage );
1444
+ oldestXact = newestXact + 1 ;
1445
+ oldestXact -= 1U << 31 ;
1446
+ oldestPage = oldestXact / per_page ;
1447
+ Assert (!SlruMayDeleteSegment (ctl ,
1448
+ (newestPage -
1449
+ newestPage % SLRU_PAGES_PER_SEGMENT ),
1450
+ oldestPage ));
1451
+
1452
+ /*
1453
+ * GetNewTransactionId() has assigned the last XID it can safely use, and
1454
+ * that XID is in the *FIRST* page of the second segment. We must not
1455
+ * delete that segment.
1456
+ */
1457
+ newestPage = SLRU_PAGES_PER_SEGMENT ;
1458
+ newestXact = newestPage * per_page + offset ;
1459
+ Assert (newestXact / per_page == newestPage );
1460
+ oldestXact = newestXact + 1 ;
1461
+ oldestXact -= 1U << 31 ;
1462
+ oldestPage = oldestXact / per_page ;
1463
+ Assert (!SlruMayDeleteSegment (ctl ,
1464
+ (newestPage -
1465
+ newestPage % SLRU_PAGES_PER_SEGMENT ),
1466
+ oldestPage ));
1467
+ }
1468
+
1469
+ /*
1470
+ * Unit-test a PagePrecedes function.
1471
+ *
1472
+ * This assumes every uint32 >= FirstNormalTransactionId is a valid key. It
1473
+ * assumes each value occupies a contiguous, fixed-size region of SLRU bytes.
1474
+ * (MultiXactMemberCtl separates flags from XIDs. AsyncCtl has
1475
+ * variable-length entries, no keys, and no random access. These unit tests
1476
+ * do not apply to them.)
1477
+ */
1478
+ void
1479
+ SlruPagePrecedesUnitTests (SlruCtl ctl , int per_page )
1480
+ {
1481
+ /* Test first, middle and last entries of a page. */
1482
+ SlruPagePrecedesTestOffset (ctl , per_page , 0 );
1483
+ SlruPagePrecedesTestOffset (ctl , per_page , per_page / 2 );
1484
+ SlruPagePrecedesTestOffset (ctl , per_page , per_page - 1 );
1485
+ }
1486
+ #endif
1487
+
1375
1488
/*
1376
1489
* SlruScanDirectory callback
1377
- * This callback reports true if there's any segment prior to the one
1378
- * containing the page passed as "data".
1490
+ * This callback reports true if there's any segment wholly prior to the
1491
+ * one containing the page passed as "data".
1379
1492
*/
1380
1493
bool
1381
1494
SlruScanDirCbReportPresence (SlruCtl ctl , char * filename , int segpage , void * data )
1382
1495
{
1383
1496
int cutoffPage = * (int * ) data ;
1384
1497
1385
- cutoffPage -= cutoffPage % SLRU_PAGES_PER_SEGMENT ;
1386
-
1387
- if (ctl -> PagePrecedes (segpage , cutoffPage ))
1498
+ if (SlruMayDeleteSegment (ctl , segpage , cutoffPage ))
1388
1499
return true; /* found one; don't iterate any more */
1389
1500
1390
1501
return false; /* keep going */
@@ -1399,7 +1510,7 @@ SlruScanDirCbDeleteCutoff(SlruCtl ctl, char *filename, int segpage, void *data)
1399
1510
{
1400
1511
int cutoffPage = * (int * ) data ;
1401
1512
1402
- if (ctl -> PagePrecedes ( segpage , cutoffPage ))
1513
+ if (SlruMayDeleteSegment ( ctl , segpage , cutoffPage ))
1403
1514
SlruInternalDeleteSegment (ctl , filename );
1404
1515
1405
1516
return false; /* keep going */
0 commit comments