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

Commit6374618

Browse files
committed
Change snapshot type to be determined by enum rather than callback.
This is in preparation for allowing the same snapshot be used fordifferent table AMs. With the current callback based approach we wouldneed one callback for each supported AM, which clearly would not beextensible. Thus add a new Snapshot->snapshot_type field, and movethe dispatch into HeapTupleSatisfiesVisibility() (which is now afunction). Later work will then dispatch calls toHeapTupleSatisfiesVisibility() and other AMs visibility functionsdepending on the type of the table. The central SnapshotType enumalso seems like a good location to centralize documentation about theintended behaviour of various types of snapshots.As tqual.h isn't included by bufmgr.h any more (as HeapTupleSatisfies*isn't referenced by TestForOldSnapshot() anymore) a few files now needto include it directly.Author: Andres Freund, loosely based on earlier work by Haribabu KommiDiscussion:https://postgr.es/m/20180703070645.wchpu5muyto5n647@alap3.anarazel.dehttps://postgr.es/m/20160812231527.GA690404@alvherre.pgsql
1 parent8f9e934 commit6374618

File tree

10 files changed

+161
-82
lines changed

10 files changed

+161
-82
lines changed

‎contrib/amcheck/verify_nbtree.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include"storage/lmgr.h"
3737
#include"utils/memutils.h"
3838
#include"utils/snapmgr.h"
39+
#include"utils/tqual.h"
3940

4041

4142
PG_MODULE_MAGIC;

‎contrib/pg_visibility/pg_visibility.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include"storage/procarray.h"
2222
#include"storage/smgr.h"
2323
#include"utils/rel.h"
24+
#include"utils/snapmgr.h"
25+
#include"utils/tqual.h"
2426

2527
PG_MODULE_MAGIC;
2628

‎src/backend/access/nbtree/nbtsort.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#include"utils/rel.h"
7373
#include"utils/sortsupport.h"
7474
#include"utils/tuplesort.h"
75+
#include"utils/tqual.h"
7576

7677

7778
/* Magic numbers for parallel state sharing */

‎src/backend/replication/logical/snapbuild.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ static void
376376
SnapBuildFreeSnapshot(Snapshotsnap)
377377
{
378378
/* make sure we don't get passed an external snapshot */
379-
Assert(snap->satisfies==HeapTupleSatisfiesHistoricMVCC);
379+
Assert(snap->snapshot_type==SNAPSHOT_HISTORIC_MVCC);
380380

381381
/* make sure nobody modified our snapshot */
382382
Assert(snap->curcid==FirstCommandId);
@@ -434,7 +434,7 @@ void
434434
SnapBuildSnapDecRefcount(Snapshotsnap)
435435
{
436436
/* make sure we don't get passed an external snapshot */
437-
Assert(snap->satisfies==HeapTupleSatisfiesHistoricMVCC);
437+
Assert(snap->snapshot_type==SNAPSHOT_HISTORIC_MVCC);
438438

439439
/* make sure nobody modified our snapshot */
440440
Assert(snap->curcid==FirstCommandId);
@@ -476,7 +476,7 @@ SnapBuildBuildSnapshot(SnapBuild *builder)
476476

477477
snapshot=MemoryContextAllocZero(builder->context,ssize);
478478

479-
snapshot->satisfies=HeapTupleSatisfiesHistoricMVCC;
479+
snapshot->snapshot_type=SNAPSHOT_HISTORIC_MVCC;
480480

481481
/*
482482
* We misuse the original meaning of SnapshotData's xip and subxip fields

‎src/backend/utils/time/snapmgr.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ static volatile OldSnapshotControlData *oldSnapshotControl;
141141
* These SnapshotData structs are static to simplify memory allocation
142142
* (see the hack in GetSnapshotData to avoid repeated malloc/free).
143143
*/
144-
staticSnapshotDataCurrentSnapshotData= {HeapTupleSatisfiesMVCC};
145-
staticSnapshotDataSecondarySnapshotData= {HeapTupleSatisfiesMVCC};
146-
SnapshotDataCatalogSnapshotData= {HeapTupleSatisfiesMVCC};
144+
staticSnapshotDataCurrentSnapshotData= {SNAPSHOT_MVCC};
145+
staticSnapshotDataSecondarySnapshotData= {SNAPSHOT_MVCC};
146+
SnapshotDataCatalogSnapshotData= {SNAPSHOT_MVCC};
147147

148148
/* Pointers to valid snapshots */
149149
staticSnapshotCurrentSnapshot=NULL;
@@ -2046,7 +2046,7 @@ EstimateSnapshotSpace(Snapshot snap)
20462046
Sizesize;
20472047

20482048
Assert(snap!=InvalidSnapshot);
2049-
Assert(snap->satisfies==HeapTupleSatisfiesMVCC);
2049+
Assert(snap->snapshot_type==SNAPSHOT_MVCC);
20502050

20512051
/* We allocate any XID arrays needed in the same palloc block. */
20522052
size=add_size(sizeof(SerializedSnapshotData),
@@ -2143,7 +2143,7 @@ RestoreSnapshot(char *start_address)
21432143

21442144
/* Copy all required fields */
21452145
snapshot= (Snapshot)MemoryContextAlloc(TopTransactionContext,size);
2146-
snapshot->satisfies=HeapTupleSatisfiesMVCC;
2146+
snapshot->snapshot_type=SNAPSHOT_MVCC;
21472147
snapshot->xmin=serialized_snapshot.xmin;
21482148
snapshot->xmax=serialized_snapshot.xmax;
21492149
snapshot->xip=NULL;

‎src/backend/utils/time/tqual.c

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@
7878

7979

8080
/* Static variables representing various special snapshot semantics */
81-
SnapshotDataSnapshotSelfData= {HeapTupleSatisfiesSelf};
82-
SnapshotDataSnapshotAnyData= {HeapTupleSatisfiesAny};
81+
SnapshotDataSnapshotSelfData= {SNAPSHOT_SELF};
82+
SnapshotDataSnapshotAnyData= {SNAPSHOT_ANY};
8383

8484

8585
/*
@@ -152,10 +152,7 @@ HeapTupleSetHintBits(HeapTupleHeader tuple, Buffer buffer,
152152
* HeapTupleSatisfiesSelf
153153
*True iff heap tuple is valid "for itself".
154154
*
155-
*Here, we consider the effects of:
156-
*all committed transactions (as of the current instant)
157-
*previous commands of this transaction
158-
*changes made by the current command
155+
* See SNAPSHOT_MVCC's definition for the intended behaviour.
159156
*
160157
* Note:
161158
*Assumes heap tuple is valid.
@@ -172,7 +169,7 @@ HeapTupleSetHintBits(HeapTupleHeader tuple, Buffer buffer,
172169
*(Xmax != my-transaction &&the row was deleted by another transaction
173170
* Xmax is not committed)))that has not been committed
174171
*/
175-
bool
172+
staticbool
176173
HeapTupleSatisfiesSelf(HeapTuplehtup,Snapshotsnapshot,Bufferbuffer)
177174
{
178175
HeapTupleHeadertuple=htup->t_data;
@@ -342,7 +339,7 @@ HeapTupleSatisfiesSelf(HeapTuple htup, Snapshot snapshot, Buffer buffer)
342339
* HeapTupleSatisfiesAny
343340
*Dummy "satisfies" routine: any tuple satisfies SnapshotAny.
344341
*/
345-
bool
342+
staticbool
346343
HeapTupleSatisfiesAny(HeapTuplehtup,Snapshotsnapshot,Bufferbuffer)
347344
{
348345
return true;
@@ -352,6 +349,8 @@ HeapTupleSatisfiesAny(HeapTuple htup, Snapshot snapshot, Buffer buffer)
352349
* HeapTupleSatisfiesToast
353350
*True iff heap tuple is valid as a TOAST row.
354351
*
352+
* See SNAPSHOT_TOAST's definition for the intended behaviour.
353+
*
355354
* This is a simplified version that only checks for VACUUM moving conditions.
356355
* It's appropriate for TOAST usage because TOAST really doesn't want to do
357356
* its own time qual checks; if you can see the main table row that contains
@@ -362,7 +361,7 @@ HeapTupleSatisfiesAny(HeapTuple htup, Snapshot snapshot, Buffer buffer)
362361
* Among other things, this means you can't do UPDATEs of rows in a TOAST
363362
* table.
364363
*/
365-
bool
364+
staticbool
366365
HeapTupleSatisfiesToast(HeapTuplehtup,Snapshotsnapshot,
367366
Bufferbuffer)
368367
{
@@ -716,10 +715,7 @@ HeapTupleSatisfiesUpdate(HeapTuple htup, CommandId curcid,
716715
* HeapTupleSatisfiesDirty
717716
*True iff heap tuple is valid including effects of open transactions.
718717
*
719-
*Here, we consider the effects of:
720-
*all committed and in-progress transactions (as of the current instant)
721-
*previous commands of this transaction
722-
*changes made by the current command
718+
* See SNAPSHOT_DIRTY's definition for the intended behaviour.
723719
*
724720
* This is essentially like HeapTupleSatisfiesSelf as far as effects of
725721
* the current transaction and committed/aborted xacts are concerned.
@@ -735,7 +731,7 @@ HeapTupleSatisfiesUpdate(HeapTuple htup, CommandId curcid,
735731
* on the insertion without aborting the whole transaction, the associated
736732
* token is also returned in snapshot->speculativeToken.
737733
*/
738-
bool
734+
staticbool
739735
HeapTupleSatisfiesDirty(HeapTuplehtup,Snapshotsnapshot,
740736
Bufferbuffer)
741737
{
@@ -934,14 +930,7 @@ HeapTupleSatisfiesDirty(HeapTuple htup, Snapshot snapshot,
934930
* HeapTupleSatisfiesMVCC
935931
*True iff heap tuple is valid for the given MVCC snapshot.
936932
*
937-
*Here, we consider the effects of:
938-
*all transactions committed as of the time of the given snapshot
939-
*previous commands of this transaction
940-
*
941-
*Does _not_ include:
942-
*transactions shown as in-progress by the snapshot
943-
*transactions started after the snapshot was taken
944-
*changes made by the current command
933+
* See SNAPSHOT_MVCC's definition for the intended behaviour.
945934
*
946935
* Notice that here, we will not update the tuple status hint bits if the
947936
* inserting/deleting transaction is still running according to our snapshot,
@@ -959,7 +948,7 @@ HeapTupleSatisfiesDirty(HeapTuple htup, Snapshot snapshot,
959948
* inserting/deleting transaction was still running --- which was more cycles
960949
* and more contention on the PGXACT array.
961950
*/
962-
bool
951+
staticbool
963952
HeapTupleSatisfiesMVCC(HeapTuplehtup,Snapshotsnapshot,
964953
Bufferbuffer)
965954
{
@@ -1390,11 +1379,13 @@ HeapTupleSatisfiesVacuum(HeapTuple htup, TransactionId OldestXmin,
13901379
*True if tuple might be visible to some transaction; false if it's
13911380
*surely dead to everyone, ie, vacuumable.
13921381
*
1393-
*This is an interface to HeapTupleSatisfiesVacuum that meets the
1394-
*SnapshotSatisfiesFunc API, so it can be used through a Snapshot.
1382+
*See SNAPSHOT_TOAST's definition for the intended behaviour.
1383+
*
1384+
*This is an interface to HeapTupleSatisfiesVacuum that's callable via
1385+
*HeapTupleSatisfiesSnapshot, so it can be used through a Snapshot.
13951386
*snapshot->xmin must have been set up with the xmin horizon to use.
13961387
*/
1397-
bool
1388+
staticbool
13981389
HeapTupleSatisfiesNonVacuumable(HeapTuplehtup,Snapshotsnapshot,
13991390
Bufferbuffer)
14001391
{
@@ -1659,7 +1650,7 @@ TransactionIdInArray(TransactionId xid, TransactionId *xip, Size num)
16591650
* dangerous to do so as the semantics of doing so during timetravel are more
16601651
* complicated than when dealing "only" with the present.
16611652
*/
1662-
bool
1653+
staticbool
16631654
HeapTupleSatisfiesHistoricMVCC(HeapTuplehtup,Snapshotsnapshot,
16641655
Bufferbuffer)
16651656
{
@@ -1796,3 +1787,44 @@ HeapTupleSatisfiesHistoricMVCC(HeapTuple htup, Snapshot snapshot,
17961787
else
17971788
return true;
17981789
}
1790+
1791+
/*
1792+
* HeapTupleSatisfiesVisibility
1793+
*True iff heap tuple satisfies a time qual.
1794+
*
1795+
* Notes:
1796+
*Assumes heap tuple is valid, and buffer at least share locked.
1797+
*
1798+
*Hint bits in the HeapTuple's t_infomask may be updated as a side effect;
1799+
*if so, the indicated buffer is marked dirty.
1800+
*/
1801+
bool
1802+
HeapTupleSatisfiesVisibility(HeapTupletup,Snapshotsnapshot,Bufferbuffer)
1803+
{
1804+
switch (snapshot->snapshot_type)
1805+
{
1806+
caseSNAPSHOT_MVCC:
1807+
returnHeapTupleSatisfiesMVCC(tup,snapshot,buffer);
1808+
break;
1809+
caseSNAPSHOT_SELF:
1810+
returnHeapTupleSatisfiesSelf(tup,snapshot,buffer);
1811+
break;
1812+
caseSNAPSHOT_ANY:
1813+
returnHeapTupleSatisfiesAny(tup,snapshot,buffer);
1814+
break;
1815+
caseSNAPSHOT_TOAST:
1816+
returnHeapTupleSatisfiesToast(tup,snapshot,buffer);
1817+
break;
1818+
caseSNAPSHOT_DIRTY:
1819+
returnHeapTupleSatisfiesDirty(tup,snapshot,buffer);
1820+
break;
1821+
caseSNAPSHOT_HISTORIC_MVCC:
1822+
returnHeapTupleSatisfiesHistoricMVCC(tup,snapshot,buffer);
1823+
break;
1824+
caseSNAPSHOT_NON_VACUUMABLE:
1825+
returnHeapTupleSatisfiesNonVacuumable(tup,snapshot,buffer);
1826+
break;
1827+
}
1828+
1829+
return false;/* keep compiler quiet */
1830+
}

‎src/include/storage/bufmgr.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include"storage/relfilenode.h"
2121
#include"utils/relcache.h"
2222
#include"utils/snapmgr.h"
23-
#include"utils/tqual.h"
2423

2524
typedefvoid*Block;
2625

@@ -268,8 +267,8 @@ TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
268267

269268
if (old_snapshot_threshold >=0
270269
&& (snapshot)!=NULL
271-
&& ((snapshot)->satisfies==HeapTupleSatisfiesMVCC
272-
|| (snapshot)->satisfies==HeapTupleSatisfiesToast)
270+
&& ((snapshot)->snapshot_type==SNAPSHOT_MVCC
271+
|| (snapshot)->snapshot_type==SNAPSHOT_TOAST)
273272
&& !XLogRecPtrIsInvalid((snapshot)->lsn)
274273
&&PageGetLSN(page)> (snapshot)->lsn)
275274
TestForOldSnapshot_impl(snapshot,relation);

‎src/include/utils/snapshot.h

Lines changed: 80 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,89 @@
2020
#include"storage/buf.h"
2121

2222

23+
/*
24+
* The different snapshot types. We use SnapshotData structures to represent
25+
* both "regular" (MVCC) snapshots and "special" snapshots that have non-MVCC
26+
* semantics. The specific semantics of a snapshot are encoded by its type.
27+
*
28+
* The behaviour of each type of snapshot should be documented alongside its
29+
* enum value, best in terms that are not specific to an individual table AM.
30+
*
31+
* The reason the snapshot type rather than a callback as it used to be is
32+
* that that allows to use the same snapshot for different table AMs without
33+
* having one callback per AM.
34+
*/
35+
typedefenumSnapshotType
36+
{
37+
/*-------------------------------------------------------------------------
38+
* A tuple is visible iff the tuple is valid for the given MVCC snapshot.
39+
*
40+
* Here, we consider the effects of:
41+
* - all transactions committed as of the time of the given snapshot
42+
* - previous commands of this transaction
43+
*
44+
* Does _not_ include:
45+
* - transactions shown as in-progress by the snapshot
46+
* - transactions started after the snapshot was taken
47+
* - changes made by the current command
48+
* -------------------------------------------------------------------------
49+
*/
50+
SNAPSHOT_MVCC=0,
51+
52+
/*-------------------------------------------------------------------------
53+
* A tuple is visible iff the tuple is valid including effects of open
54+
* transactions.
55+
*
56+
* Here, we consider the effects of:
57+
* - all committed and in-progress transactions (as of the current instant)
58+
* - previous commands of this transaction
59+
* - changes made by the current command
60+
* -------------------------------------------------------------------------
61+
*/
62+
SNAPSHOT_SELF,
63+
64+
/*
65+
* Any tuple is visible.
66+
*/
67+
SNAPSHOT_ANY,
68+
69+
/*
70+
* A tuple is visible iff the tuple tuple is valid as a TOAST row.
71+
*/
72+
SNAPSHOT_TOAST,
73+
74+
/*-------------------------------------------------------------------------
75+
* A tuple is visible iff the tuple is valid including effects of open
76+
* transactions.
77+
*
78+
* Here, we consider the effects of:
79+
* - all committed and in-progress transactions (as of the current instant)
80+
* - previous commands of this transaction
81+
* - changes made by the current command
82+
* -------------------------------------------------------------------------
83+
*/
84+
SNAPSHOT_DIRTY,
85+
86+
/*
87+
* A tuple is visible iff it follows the rules of SNAPSHOT_MVCC, but
88+
* supports being called in timetravel context (for decoding catalog
89+
* contents in the context of logical decoding).
90+
*/
91+
SNAPSHOT_HISTORIC_MVCC,
92+
93+
/*
94+
* A tuple is visible iff the tuple might be visible to some transaction;
95+
* false if it's surely dead to everyone, ie, vacuumable.
96+
*
97+
* Snapshot.xmin must have been set up with the xmin horizon to use.
98+
*/
99+
SNAPSHOT_NON_VACUUMABLE
100+
}SnapshotType;
101+
23102
typedefstructSnapshotData*Snapshot;
24103

25104
#defineInvalidSnapshot((Snapshot) NULL)
26105

27-
/*
28-
* We use SnapshotData structures to represent both "regular" (MVCC)
29-
* snapshots and "special" snapshots that have non-MVCC semantics.
30-
* The specific semantics of a snapshot are encoded by the "satisfies"
31-
* function.
32-
*/
33-
typedefbool (*SnapshotSatisfiesFunc) (HeapTuplehtup,
34-
Snapshotsnapshot,Bufferbuffer);
35-
36106
/*
37107
* Struct representing all kind of possible snapshots.
38108
*
@@ -52,7 +122,7 @@ typedef bool (*SnapshotSatisfiesFunc) (HeapTuple htup,
52122
*/
53123
typedefstructSnapshotData
54124
{
55-
SnapshotSatisfiesFuncsatisfies;/*tuple test function */
125+
SnapshotTypesnapshot_type;/*type of snapshot */
56126

57127
/*
58128
* The remaining fields are used only for MVCC snapshots, and are normally

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp