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