Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit66087f7

Browse files
committed
Prevent excess SimpleLruTruncate() deletion.
Every core SLRU wraps around. With the exception of pg_notify, the wrappoint can fall in the middle of a page. Account for this in thePagePrecedes callback specification and in SimpleLruTruncate()'s use ofsaid callback. Update each callback implementation to fit the newspecification. This changes SerialPagePrecedesLogically() from thestyle of asyncQueuePagePrecedes() to the style of CLOGPagePrecedes().(Whereas pg_clog and pg_serial share a key space, pg_serial is nothinglike pg_notify.) The bug fixed here has the same symptoms and userfollowup steps as592a589. Back-patchto 9.5 (all supported versions).Reviewed by Andrey Borodin and (in earlier versions) by Tom Lane.Discussion:https://postgr.es/m/20190202083822.GC32531@gust.leadboat.com
1 parent943a113 commit66087f7

File tree

8 files changed

+312
-80
lines changed

8 files changed

+312
-80
lines changed

‎src/backend/access/transam/clog.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ CLOGShmemInit(void)
693693
ClogCtl->PagePrecedes=CLOGPagePrecedes;
694694
SimpleLruInit(ClogCtl,"clog",CLOGShmemBuffers(),CLOG_LSNS_PER_PAGE,
695695
CLogControlLock,"pg_xact",LWTRANCHE_CLOG_BUFFERS);
696+
SlruPagePrecedesUnitTests(ClogCtl,CLOG_XACTS_PER_PAGE);
696697
}
697698

698699
/*
@@ -926,13 +927,22 @@ TruncateCLOG(TransactionId oldestXact, Oid oldestxid_datoid)
926927

927928

928929
/*
929-
* Decidewhich of twoCLOG pagenumbers is "older" for truncation purposes.
930+
* Decidewhether aCLOG pagenumber is "older" for truncation purposes.
930931
*
931932
* We need to use comparison of TransactionIds here in order to do the right
932-
* thing with wraparound XID arithmetic. However, if we are asked about
933-
* page number zero, we don't want to hand InvalidTransactionId to
934-
* TransactionIdPrecedes: it'll get weird about permanent xact IDs. So,
935-
* offset both xids by FirstNormalTransactionId to avoid that.
933+
* thing with wraparound XID arithmetic. However, TransactionIdPrecedes()
934+
* would get weird about permanent xact IDs. So, offset both such that xid1,
935+
* xid2, and xid2 + CLOG_XACTS_PER_PAGE - 1 are all normal XIDs; this offset
936+
* is relevant to page 0 and to the page preceding page 0.
937+
*
938+
* The page containing oldestXact-2^31 is the important edge case. The
939+
* portion of that page equaling or following oldestXact-2^31 is expendable,
940+
* but the portion preceding oldestXact-2^31 is not. When oldestXact-2^31 is
941+
* the first XID of a page and segment, the entire page and segment is
942+
* expendable, and we could truncate the segment. Recognizing that case would
943+
* require making oldestXact, not just the page containing oldestXact,
944+
* available to this callback. The benefit would be rare and small, so we
945+
* don't optimize that edge case.
936946
*/
937947
staticbool
938948
CLOGPagePrecedes(intpage1,intpage2)
@@ -941,11 +951,12 @@ CLOGPagePrecedes(int page1, int page2)
941951
TransactionIdxid2;
942952

943953
xid1= ((TransactionId)page1)*CLOG_XACTS_PER_PAGE;
944-
xid1+=FirstNormalTransactionId;
954+
xid1+=FirstNormalTransactionId+1;
945955
xid2= ((TransactionId)page2)*CLOG_XACTS_PER_PAGE;
946-
xid2+=FirstNormalTransactionId;
956+
xid2+=FirstNormalTransactionId+1;
947957

948-
returnTransactionIdPrecedes(xid1,xid2);
958+
return (TransactionIdPrecedes(xid1,xid2)&&
959+
TransactionIdPrecedes(xid1,xid2+CLOG_XACTS_PER_PAGE-1));
949960
}
950961

951962

‎src/backend/access/transam/commit_ts.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ CommitTsShmemInit(void)
495495
SimpleLruInit(CommitTsCtl,"commit_timestamp",CommitTsShmemBuffers(),0,
496496
CommitTsControlLock,"pg_commit_ts",
497497
LWTRANCHE_COMMITTS_BUFFERS);
498+
SlruPagePrecedesUnitTests(CommitTsCtl,COMMIT_TS_XACTS_PER_PAGE);
498499

499500
commitTsShared=ShmemInitStruct("CommitTs shared",
500501
sizeof(CommitTimestampShared),
@@ -877,14 +878,27 @@ AdvanceOldestCommitTsXid(TransactionId oldestXact)
877878

878879

879880
/*
880-
* Decidewhich of twocommitTS pagenumbers is "older" for truncation
881-
*purposes.
881+
* Decidewhether acommitTS pagenumber is "older" for truncation purposes.
882+
*Analogous to CLOGPagePrecedes().
882883
*
883-
* We need to use comparison of TransactionIds here in order to do the right
884-
* thing with wraparound XID arithmetic. However, if we are asked about
885-
* page number zero, we don't want to hand InvalidTransactionId to
886-
* TransactionIdPrecedes: it'll get weird about permanent xact IDs. So,
887-
* offset both xids by FirstNormalTransactionId to avoid that.
884+
* At default BLCKSZ, (1 << 31) % COMMIT_TS_XACTS_PER_PAGE == 128. This
885+
* introduces differences compared to CLOG and the other SLRUs having (1 <<
886+
* 31) % per_page == 0. This function never tests exactly
887+
* TransactionIdPrecedes(x-2^31, x). When the system reaches xidStopLimit,
888+
* there are two possible counts of page boundaries between oldestXact and the
889+
* latest XID assigned, depending on whether oldestXact is within the first
890+
* 128 entries of its page. Since this function doesn't know the location of
891+
* oldestXact within page2, it returns false for one page that actually is
892+
* expendable. This is a wider (yet still negligible) version of the
893+
* truncation opportunity that CLOGPagePrecedes() cannot recognize.
894+
*
895+
* For the sake of a worked example, number entries with decimal values such
896+
* that page1==1 entries range from 1.0 to 1.999. Let N+0.15 be the number of
897+
* pages that 2^31 entries will span (N is an integer). If oldestXact=N+2.1,
898+
* then the final safe XID assignment leaves newestXact=1.95. We keep page 2,
899+
* because entry=2.85 is the border that toggles whether entries precede the
900+
* last entry of the oldestXact page. While page 2 is expendable at
901+
* oldestXact=N+2.1, it would be precious at oldestXact=N+2.9.
888902
*/
889903
staticbool
890904
CommitTsPagePrecedes(intpage1,intpage2)
@@ -893,11 +907,12 @@ CommitTsPagePrecedes(int page1, int page2)
893907
TransactionIdxid2;
894908

895909
xid1= ((TransactionId)page1)*COMMIT_TS_XACTS_PER_PAGE;
896-
xid1+=FirstNormalTransactionId;
910+
xid1+=FirstNormalTransactionId+1;
897911
xid2= ((TransactionId)page2)*COMMIT_TS_XACTS_PER_PAGE;
898-
xid2+=FirstNormalTransactionId;
912+
xid2+=FirstNormalTransactionId+1;
899913

900-
returnTransactionIdPrecedes(xid1,xid2);
914+
return (TransactionIdPrecedes(xid1,xid2)&&
915+
TransactionIdPrecedes(xid1,xid2+COMMIT_TS_XACTS_PER_PAGE-1));
901916
}
902917

903918

‎src/backend/access/transam/multixact.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,10 +1830,12 @@ MultiXactShmemInit(void)
18301830
"multixact_offset",NUM_MXACTOFFSET_BUFFERS,0,
18311831
MultiXactOffsetControlLock,"pg_multixact/offsets",
18321832
LWTRANCHE_MXACTOFFSET_BUFFERS);
1833+
SlruPagePrecedesUnitTests(MultiXactOffsetCtl,MULTIXACT_OFFSETS_PER_PAGE);
18331834
SimpleLruInit(MultiXactMemberCtl,
18341835
"multixact_member",NUM_MXACTMEMBER_BUFFERS,0,
18351836
MultiXactMemberControlLock,"pg_multixact/members",
18361837
LWTRANCHE_MXACTMEMBER_BUFFERS);
1838+
/* doesn't call SimpleLruTruncate() or meet criteria for unit tests */
18371839

18381840
/* Initialize our shared state struct */
18391841
MultiXactState=ShmemInitStruct("Shared MultiXact State",
@@ -2976,6 +2978,14 @@ TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB)
29762978
* truncate the members SLRU. So we first scan the directory to determine
29772979
* the earliest offsets page number that we can read without error.
29782980
*
2981+
* When nextMXact is less than one segment away from multiWrapLimit,
2982+
* SlruScanDirCbFindEarliest can find some early segment other than the
2983+
* actual earliest. (MultiXactOffsetPagePrecedes(EARLIEST, LATEST)
2984+
* returns false, because not all pairs of entries have the same answer.)
2985+
* That can also arise when an earlier truncation attempt failed unlink()
2986+
* or returned early from this function. The only consequence is
2987+
* returning early, which wastes space that we could have liberated.
2988+
*
29792989
* NB: It's also possible that the page that oldestMulti is on has already
29802990
* been truncated away, and we crashed before updating oldestMulti.
29812991
*/
@@ -3090,15 +3100,11 @@ TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB)
30903100
}
30913101

30923102
/*
3093-
* Decide which of two MultiXactOffset page numbers is "older" for truncation
3094-
* purposes.
3095-
*
3096-
* We need to use comparison of MultiXactId here in order to do the right
3097-
* thing with wraparound. However, if we are asked about page number zero, we
3098-
* don't want to hand InvalidMultiXactId to MultiXactIdPrecedes: it'll get
3099-
* weird. So, offset both multis by FirstMultiXactId to avoid that.
3100-
* (Actually, the current implementation doesn't do anything weird with
3101-
* InvalidMultiXactId, but there's no harm in leaving this code like this.)
3103+
* Decide whether a MultiXactOffset page number is "older" for truncation
3104+
* purposes. Analogous to CLOGPagePrecedes().
3105+
*
3106+
* Offsetting the values is optional, because MultiXactIdPrecedes() has
3107+
* translational symmetry.
31023108
*/
31033109
staticbool
31043110
MultiXactOffsetPagePrecedes(intpage1,intpage2)
@@ -3107,15 +3113,17 @@ MultiXactOffsetPagePrecedes(int page1, int page2)
31073113
MultiXactIdmulti2;
31083114

31093115
multi1= ((MultiXactId)page1)*MULTIXACT_OFFSETS_PER_PAGE;
3110-
multi1+=FirstMultiXactId;
3116+
multi1+=FirstMultiXactId+1;
31113117
multi2= ((MultiXactId)page2)*MULTIXACT_OFFSETS_PER_PAGE;
3112-
multi2+=FirstMultiXactId;
3118+
multi2+=FirstMultiXactId+1;
31133119

3114-
returnMultiXactIdPrecedes(multi1,multi2);
3120+
return (MultiXactIdPrecedes(multi1,multi2)&&
3121+
MultiXactIdPrecedes(multi1,
3122+
multi2+MULTIXACT_OFFSETS_PER_PAGE-1));
31153123
}
31163124

31173125
/*
3118-
* Decidewhich of twoMultiXactMember pagenumbers is "older" for truncation
3126+
* Decidewhether aMultiXactMember pagenumber is "older" for truncation
31193127
* purposes. There is no "invalid offset number" so use the numbers verbatim.
31203128
*/
31213129
staticbool
@@ -3127,7 +3135,9 @@ MultiXactMemberPagePrecedes(int page1, int page2)
31273135
offset1= ((MultiXactOffset)page1)*MULTIXACT_MEMBERS_PER_PAGE;
31283136
offset2= ((MultiXactOffset)page2)*MULTIXACT_MEMBERS_PER_PAGE;
31293137

3130-
returnMultiXactOffsetPrecedes(offset1,offset2);
3138+
return (MultiXactOffsetPrecedes(offset1,offset2)&&
3139+
MultiXactOffsetPrecedes(offset1,
3140+
offset2+MULTIXACT_MEMBERS_PER_PAGE-1));
31313141
}
31323142

31333143
/*

‎src/backend/access/transam/slru.c

Lines changed: 127 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,11 +1199,6 @@ SimpleLruTruncate(SlruCtl ctl, int cutoffPage)
11991199
SlruSharedshared=ctl->shared;
12001200
intslotno;
12011201

1202-
/*
1203-
* The cutoff point is the start of the segment containing cutoffPage.
1204-
*/
1205-
cutoffPage-=cutoffPage %SLRU_PAGES_PER_SEGMENT;
1206-
12071202
/*
12081203
* Scan shared memory and remove any pages preceding the cutoff page, to
12091204
* ensure we won't rewrite them later. (Since this is normally called in
@@ -1216,9 +1211,7 @@ restart:;
12161211

12171212
/*
12181213
* While we are holding the lock, make an important safety check: the
1219-
* planned cutoff point must be <= the current endpoint page. Otherwise we
1220-
* have already wrapped around, and proceeding with the truncation would
1221-
* risk removing the current segment.
1214+
* current endpoint page must not be eligible for removal.
12221215
*/
12231216
if (ctl->PagePrecedes(shared->latest_page_number,cutoffPage))
12241217
{
@@ -1250,8 +1243,11 @@ restart:;
12501243
* Hmm, we have (or may have) I/O operations acting on the page, so
12511244
* we've got to wait for them to finish and then start again. This is
12521245
* the same logic as in SlruSelectLRUPage. (XXX if page is dirty,
1253-
* wouldn't it be OK to just discard it without writing it? For now,
1254-
* keep the logic the same as it was.)
1246+
* wouldn't it be OK to just discard it without writing it?
1247+
* SlruMayDeleteSegment() uses a stricter qualification, so we might
1248+
* not delete this page in the end; even if we don't delete it, we
1249+
* won't have cause to read its data again. For now, keep the logic
1250+
* the same as it was.)
12551251
*/
12561252
if (shared->page_status[slotno]==SLRU_PAGE_VALID)
12571253
SlruInternalWritePage(ctl,slotno,NULL);
@@ -1341,19 +1337,134 @@ SlruDeleteSegment(SlruCtl ctl, int segno)
13411337
LWLockRelease(shared->ControlLock);
13421338
}
13431339

1340+
/*
1341+
* Determine whether a segment is okay to delete.
1342+
*
1343+
* segpage is the first page of the segment, and cutoffPage is the oldest (in
1344+
* PagePrecedes order) page in the SLRU containing still-useful data. Since
1345+
* every core PagePrecedes callback implements "wrap around", check the
1346+
* segment's first and last pages:
1347+
*
1348+
* first<cutoff && last<cutoff: yes
1349+
* first<cutoff && last>=cutoff: no; cutoff falls inside this segment
1350+
* first>=cutoff && last<cutoff: no; wrap point falls inside this segment
1351+
* first>=cutoff && last>=cutoff: no; every page of this segment is too young
1352+
*/
1353+
staticbool
1354+
SlruMayDeleteSegment(SlruCtlctl,intsegpage,intcutoffPage)
1355+
{
1356+
intseg_last_page=segpage+SLRU_PAGES_PER_SEGMENT-1;
1357+
1358+
Assert(segpage %SLRU_PAGES_PER_SEGMENT==0);
1359+
1360+
return (ctl->PagePrecedes(segpage,cutoffPage)&&
1361+
ctl->PagePrecedes(seg_last_page,cutoffPage));
1362+
}
1363+
1364+
#ifdefUSE_ASSERT_CHECKING
1365+
staticvoid
1366+
SlruPagePrecedesTestOffset(SlruCtlctl,intper_page,uint32offset)
1367+
{
1368+
TransactionIdlhs,
1369+
rhs;
1370+
intnewestPage,
1371+
oldestPage;
1372+
TransactionIdnewestXact,
1373+
oldestXact;
1374+
1375+
/*
1376+
* Compare an XID pair having undefined order (see RFC 1982), a pair at
1377+
* "opposite ends" of the XID space. TransactionIdPrecedes() treats each
1378+
* as preceding the other. If RHS is oldestXact, LHS is the first XID we
1379+
* must not assign.
1380+
*/
1381+
lhs=per_page+offset;/* skip first page to avoid non-normal XIDs */
1382+
rhs=lhs+ (1U <<31);
1383+
Assert(TransactionIdPrecedes(lhs,rhs));
1384+
Assert(TransactionIdPrecedes(rhs,lhs));
1385+
Assert(!TransactionIdPrecedes(lhs-1,rhs));
1386+
Assert(TransactionIdPrecedes(rhs,lhs-1));
1387+
Assert(TransactionIdPrecedes(lhs+1,rhs));
1388+
Assert(!TransactionIdPrecedes(rhs,lhs+1));
1389+
Assert(!TransactionIdFollowsOrEquals(lhs,rhs));
1390+
Assert(!TransactionIdFollowsOrEquals(rhs,lhs));
1391+
Assert(!ctl->PagePrecedes(lhs /per_page,lhs /per_page));
1392+
Assert(!ctl->PagePrecedes(lhs /per_page,rhs /per_page));
1393+
Assert(!ctl->PagePrecedes(rhs /per_page,lhs /per_page));
1394+
Assert(!ctl->PagePrecedes((lhs-per_page) /per_page,rhs /per_page));
1395+
Assert(ctl->PagePrecedes(rhs /per_page, (lhs-3*per_page) /per_page));
1396+
Assert(ctl->PagePrecedes(rhs /per_page, (lhs-2*per_page) /per_page));
1397+
Assert(ctl->PagePrecedes(rhs /per_page, (lhs-1*per_page) /per_page)
1398+
|| (1U <<31) %per_page!=0);/* See CommitTsPagePrecedes() */
1399+
Assert(ctl->PagePrecedes((lhs+1*per_page) /per_page,rhs /per_page)
1400+
|| (1U <<31) %per_page!=0);
1401+
Assert(ctl->PagePrecedes((lhs+2*per_page) /per_page,rhs /per_page));
1402+
Assert(ctl->PagePrecedes((lhs+3*per_page) /per_page,rhs /per_page));
1403+
Assert(!ctl->PagePrecedes(rhs /per_page, (lhs+per_page) /per_page));
1404+
1405+
/*
1406+
* GetNewTransactionId() has assigned the last XID it can safely use, and
1407+
* that XID is in the *LAST* page of the second segment. We must not
1408+
* delete that segment.
1409+
*/
1410+
newestPage=2*SLRU_PAGES_PER_SEGMENT-1;
1411+
newestXact=newestPage*per_page+offset;
1412+
Assert(newestXact /per_page==newestPage);
1413+
oldestXact=newestXact+1;
1414+
oldestXact-=1U <<31;
1415+
oldestPage=oldestXact /per_page;
1416+
Assert(!SlruMayDeleteSegment(ctl,
1417+
(newestPage-
1418+
newestPage %SLRU_PAGES_PER_SEGMENT),
1419+
oldestPage));
1420+
1421+
/*
1422+
* GetNewTransactionId() has assigned the last XID it can safely use, and
1423+
* that XID is in the *FIRST* page of the second segment. We must not
1424+
* delete that segment.
1425+
*/
1426+
newestPage=SLRU_PAGES_PER_SEGMENT;
1427+
newestXact=newestPage*per_page+offset;
1428+
Assert(newestXact /per_page==newestPage);
1429+
oldestXact=newestXact+1;
1430+
oldestXact-=1U <<31;
1431+
oldestPage=oldestXact /per_page;
1432+
Assert(!SlruMayDeleteSegment(ctl,
1433+
(newestPage-
1434+
newestPage %SLRU_PAGES_PER_SEGMENT),
1435+
oldestPage));
1436+
}
1437+
1438+
/*
1439+
* Unit-test a PagePrecedes function.
1440+
*
1441+
* This assumes every uint32 >= FirstNormalTransactionId is a valid key. It
1442+
* assumes each value occupies a contiguous, fixed-size region of SLRU bytes.
1443+
* (MultiXactMemberCtl separates flags from XIDs. AsyncCtl has
1444+
* variable-length entries, no keys, and no random access. These unit tests
1445+
* do not apply to them.)
1446+
*/
1447+
void
1448+
SlruPagePrecedesUnitTests(SlruCtlctl,intper_page)
1449+
{
1450+
/* Test first, middle and last entries of a page. */
1451+
SlruPagePrecedesTestOffset(ctl,per_page,0);
1452+
SlruPagePrecedesTestOffset(ctl,per_page,per_page /2);
1453+
SlruPagePrecedesTestOffset(ctl,per_page,per_page-1);
1454+
}
1455+
#endif
1456+
13441457
/*
13451458
* SlruScanDirectory callback
1346-
*This callback reports true if there's any segment prior to the one
1347-
*containing the page passed as "data".
1459+
*This callback reports true if there's any segmentwhollyprior to the
1460+
*onecontaining the page passed as "data".
13481461
*/
13491462
bool
13501463
SlruScanDirCbReportPresence(SlruCtlctl,char*filename,intsegpage,void*data)
13511464
{
13521465
intcutoffPage=*(int*)data;
13531466

1354-
cutoffPage-=cutoffPage %SLRU_PAGES_PER_SEGMENT;
1355-
1356-
if (ctl->PagePrecedes(segpage,cutoffPage))
1467+
if (SlruMayDeleteSegment(ctl,segpage,cutoffPage))
13571468
return true;/* found one; don't iterate any more */
13581469

13591470
return false;/* keep going */
@@ -1368,7 +1479,7 @@ SlruScanDirCbDeleteCutoff(SlruCtl ctl, char *filename, int segpage, void *data)
13681479
{
13691480
intcutoffPage=*(int*)data;
13701481

1371-
if (ctl->PagePrecedes(segpage,cutoffPage))
1482+
if (SlruMayDeleteSegment(ctl,segpage,cutoffPage))
13721483
SlruInternalDeleteSegment(ctl,filename);
13731484

13741485
return false;/* keep going */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp