@@ -1182,11 +1182,6 @@ SimpleLruTruncate(SlruCtl ctl, int cutoffPage)
1182
1182
SlruShared shared = ctl -> shared ;
1183
1183
int slotno ;
1184
1184
1185
- /*
1186
- * The cutoff point is the start of the segment containing cutoffPage.
1187
- */
1188
- cutoffPage -= cutoffPage % SLRU_PAGES_PER_SEGMENT ;
1189
-
1190
1185
/*
1191
1186
* Scan shared memory and remove any pages preceding the cutoff page, to
1192
1187
* ensure we won't rewrite them later. (Since this is normally called in
@@ -1199,9 +1194,7 @@ restart:;
1199
1194
1200
1195
/*
1201
1196
* While we are holding the lock, make an important safety check: the
1202
- * planned cutoff point must be <= the current endpoint page. Otherwise we
1203
- * have already wrapped around, and proceeding with the truncation would
1204
- * risk removing the current segment.
1197
+ * current endpoint page must not be eligible for removal.
1205
1198
*/
1206
1199
if (ctl -> PagePrecedes (shared -> latest_page_number , cutoffPage ))
1207
1200
{
@@ -1233,8 +1226,11 @@ restart:;
1233
1226
* Hmm, we have (or may have) I/O operations acting on the page, so
1234
1227
* we've got to wait for them to finish and then start again. This is
1235
1228
* the same logic as in SlruSelectLRUPage. (XXX if page is dirty,
1236
- * wouldn't it be OK to just discard it without writing it? For now,
1237
- * keep the logic the same as it was.)
1229
+ * wouldn't it be OK to just discard it without writing it?
1230
+ * SlruMayDeleteSegment() uses a stricter qualification, so we might
1231
+ * not delete this page in the end; even if we don't delete it, we
1232
+ * won't have cause to read its data again. For now, keep the logic
1233
+ * the same as it was.)
1238
1234
*/
1239
1235
if (shared -> page_status [slotno ] == SLRU_PAGE_VALID )
1240
1236
SlruInternalWritePage (ctl , slotno , NULL );
@@ -1324,19 +1320,134 @@ SlruDeleteSegment(SlruCtl ctl, int segno)
1324
1320
LWLockRelease (shared -> ControlLock );
1325
1321
}
1326
1322
1323
+ /*
1324
+ * Determine whether a segment is okay to delete.
1325
+ *
1326
+ * segpage is the first page of the segment, and cutoffPage is the oldest (in
1327
+ * PagePrecedes order) page in the SLRU containing still-useful data. Since
1328
+ * every core PagePrecedes callback implements "wrap around", check the
1329
+ * segment's first and last pages:
1330
+ *
1331
+ * first<cutoff && last<cutoff: yes
1332
+ * first<cutoff && last>=cutoff: no; cutoff falls inside this segment
1333
+ * first>=cutoff && last<cutoff: no; wrap point falls inside this segment
1334
+ * first>=cutoff && last>=cutoff: no; every page of this segment is too young
1335
+ */
1336
+ static bool
1337
+ SlruMayDeleteSegment (SlruCtl ctl , int segpage , int cutoffPage )
1338
+ {
1339
+ int seg_last_page = segpage + SLRU_PAGES_PER_SEGMENT - 1 ;
1340
+
1341
+ Assert (segpage % SLRU_PAGES_PER_SEGMENT == 0 );
1342
+
1343
+ return (ctl -> PagePrecedes (segpage , cutoffPage ) &&
1344
+ ctl -> PagePrecedes (seg_last_page , cutoffPage ));
1345
+ }
1346
+
1347
+ #ifdef USE_ASSERT_CHECKING
1348
+ static void
1349
+ SlruPagePrecedesTestOffset (SlruCtl ctl , int per_page , uint32 offset )
1350
+ {
1351
+ TransactionId lhs ,
1352
+ rhs ;
1353
+ int newestPage ,
1354
+ oldestPage ;
1355
+ TransactionId newestXact ,
1356
+ oldestXact ;
1357
+
1358
+ /*
1359
+ * Compare an XID pair having undefined order (see RFC 1982), a pair at
1360
+ * "opposite ends" of the XID space. TransactionIdPrecedes() treats each
1361
+ * as preceding the other. If RHS is oldestXact, LHS is the first XID we
1362
+ * must not assign.
1363
+ */
1364
+ lhs = per_page + offset ; /* skip first page to avoid non-normal XIDs */
1365
+ rhs = lhs + (1U << 31 );
1366
+ Assert (TransactionIdPrecedes (lhs , rhs ));
1367
+ Assert (TransactionIdPrecedes (rhs , lhs ));
1368
+ Assert (!TransactionIdPrecedes (lhs - 1 , rhs ));
1369
+ Assert (TransactionIdPrecedes (rhs , lhs - 1 ));
1370
+ Assert (TransactionIdPrecedes (lhs + 1 , rhs ));
1371
+ Assert (!TransactionIdPrecedes (rhs , lhs + 1 ));
1372
+ Assert (!TransactionIdFollowsOrEquals (lhs , rhs ));
1373
+ Assert (!TransactionIdFollowsOrEquals (rhs , lhs ));
1374
+ Assert (!ctl -> PagePrecedes (lhs / per_page , lhs / per_page ));
1375
+ Assert (!ctl -> PagePrecedes (lhs / per_page , rhs / per_page ));
1376
+ Assert (!ctl -> PagePrecedes (rhs / per_page , lhs / per_page ));
1377
+ Assert (!ctl -> PagePrecedes ((lhs - per_page ) / per_page , rhs / per_page ));
1378
+ Assert (ctl -> PagePrecedes (rhs / per_page , (lhs - 3 * per_page ) / per_page ));
1379
+ Assert (ctl -> PagePrecedes (rhs / per_page , (lhs - 2 * per_page ) / per_page ));
1380
+ Assert (ctl -> PagePrecedes (rhs / per_page , (lhs - 1 * per_page ) / per_page )
1381
+ || (1U << 31 ) % per_page != 0 ); /* See CommitTsPagePrecedes() */
1382
+ Assert (ctl -> PagePrecedes ((lhs + 1 * per_page ) / per_page , rhs / per_page )
1383
+ || (1U << 31 ) % per_page != 0 );
1384
+ Assert (ctl -> PagePrecedes ((lhs + 2 * per_page ) / per_page , rhs / per_page ));
1385
+ Assert (ctl -> PagePrecedes ((lhs + 3 * per_page ) / per_page , rhs / per_page ));
1386
+ Assert (!ctl -> PagePrecedes (rhs / per_page , (lhs + per_page ) / per_page ));
1387
+
1388
+ /*
1389
+ * GetNewTransactionId() has assigned the last XID it can safely use, and
1390
+ * that XID is in the *LAST* page of the second segment. We must not
1391
+ * delete that segment.
1392
+ */
1393
+ newestPage = 2 * SLRU_PAGES_PER_SEGMENT - 1 ;
1394
+ newestXact = newestPage * per_page + offset ;
1395
+ Assert (newestXact / per_page == newestPage );
1396
+ oldestXact = newestXact + 1 ;
1397
+ oldestXact -= 1U << 31 ;
1398
+ oldestPage = oldestXact / per_page ;
1399
+ Assert (!SlruMayDeleteSegment (ctl ,
1400
+ (newestPage -
1401
+ newestPage % SLRU_PAGES_PER_SEGMENT ),
1402
+ oldestPage ));
1403
+
1404
+ /*
1405
+ * GetNewTransactionId() has assigned the last XID it can safely use, and
1406
+ * that XID is in the *FIRST* page of the second segment. We must not
1407
+ * delete that segment.
1408
+ */
1409
+ newestPage = SLRU_PAGES_PER_SEGMENT ;
1410
+ newestXact = newestPage * per_page + offset ;
1411
+ Assert (newestXact / per_page == newestPage );
1412
+ oldestXact = newestXact + 1 ;
1413
+ oldestXact -= 1U << 31 ;
1414
+ oldestPage = oldestXact / per_page ;
1415
+ Assert (!SlruMayDeleteSegment (ctl ,
1416
+ (newestPage -
1417
+ newestPage % SLRU_PAGES_PER_SEGMENT ),
1418
+ oldestPage ));
1419
+ }
1420
+
1421
+ /*
1422
+ * Unit-test a PagePrecedes function.
1423
+ *
1424
+ * This assumes every uint32 >= FirstNormalTransactionId is a valid key. It
1425
+ * assumes each value occupies a contiguous, fixed-size region of SLRU bytes.
1426
+ * (MultiXactMemberCtl separates flags from XIDs. AsyncCtl has
1427
+ * variable-length entries, no keys, and no random access. These unit tests
1428
+ * do not apply to them.)
1429
+ */
1430
+ void
1431
+ SlruPagePrecedesUnitTests (SlruCtl ctl , int per_page )
1432
+ {
1433
+ /* Test first, middle and last entries of a page. */
1434
+ SlruPagePrecedesTestOffset (ctl , per_page , 0 );
1435
+ SlruPagePrecedesTestOffset (ctl , per_page , per_page / 2 );
1436
+ SlruPagePrecedesTestOffset (ctl , per_page , per_page - 1 );
1437
+ }
1438
+ #endif
1439
+
1327
1440
/*
1328
1441
* SlruScanDirectory callback
1329
- * This callback reports true if there's any segment prior to the one
1330
- * containing the page passed as "data".
1442
+ * This callback reports true if there's any segment wholly prior to the
1443
+ * one containing the page passed as "data".
1331
1444
*/
1332
1445
bool
1333
1446
SlruScanDirCbReportPresence (SlruCtl ctl , char * filename , int segpage , void * data )
1334
1447
{
1335
1448
int cutoffPage = * (int * ) data ;
1336
1449
1337
- cutoffPage -= cutoffPage % SLRU_PAGES_PER_SEGMENT ;
1338
-
1339
- if (ctl -> PagePrecedes (segpage , cutoffPage ))
1450
+ if (SlruMayDeleteSegment (ctl , segpage , cutoffPage ))
1340
1451
return true; /* found one; don't iterate any more */
1341
1452
1342
1453
return false; /* keep going */
@@ -1351,7 +1462,7 @@ SlruScanDirCbDeleteCutoff(SlruCtl ctl, char *filename, int segpage, void *data)
1351
1462
{
1352
1463
int cutoffPage = * (int * ) data ;
1353
1464
1354
- if (ctl -> PagePrecedes ( segpage , cutoffPage ))
1465
+ if (SlruMayDeleteSegment ( ctl , segpage , cutoffPage ))
1355
1466
SlruInternalDeleteSegment (ctl , filename );
1356
1467
1357
1468
return false; /* keep going */
0 commit comments