Skip to content

Commit 6374618

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 for different table AMs. With the current callback based approach we would need one callback for each supported AM, which clearly would not be extensible. Thus add a new Snapshot->snapshot_type field, and move the dispatch into HeapTupleSatisfiesVisibility() (which is now a function). Later work will then dispatch calls to HeapTupleSatisfiesVisibility() and other AMs visibility functions depending on the type of the table. The central SnapshotType enum also seems like a good location to centralize documentation about the intended 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 need to include it directly. Author: Andres Freund, loosely based on earlier work by Haribabu Kommi Discussion: https://postgr.es/m/[email protected] https://postgr.es/m/[email protected]
1 parent 8f9e934 commit 6374618

File tree

10 files changed

+161
-82
lines changed

10 files changed

+161
-82
lines changed

contrib/amcheck/verify_nbtree.c

+1
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

+2
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

+1
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

+3-3
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ static void
376376
SnapBuildFreeSnapshot(Snapshot snap)
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(Snapshot snap)
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

+5-5
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-
static SnapshotData CurrentSnapshotData = {HeapTupleSatisfiesMVCC};
145-
static SnapshotData SecondarySnapshotData = {HeapTupleSatisfiesMVCC};
146-
SnapshotData CatalogSnapshotData = {HeapTupleSatisfiesMVCC};
144+
static SnapshotData CurrentSnapshotData = {SNAPSHOT_MVCC};
145+
static SnapshotData SecondarySnapshotData = {SNAPSHOT_MVCC};
146+
SnapshotData CatalogSnapshotData = {SNAPSHOT_MVCC};
147147

148148
/* Pointers to valid snapshots */
149149
static Snapshot CurrentSnapshot = NULL;
@@ -2046,7 +2046,7 @@ EstimateSnapshotSpace(Snapshot snap)
20462046
Size size;
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

+59-27
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@
7878

7979

8080
/* Static variables representing various special snapshot semantics */
81-
SnapshotData SnapshotSelfData = {HeapTupleSatisfiesSelf};
82-
SnapshotData SnapshotAnyData = {HeapTupleSatisfiesAny};
81+
SnapshotData SnapshotSelfData = {SNAPSHOT_SELF};
82+
SnapshotData SnapshotAnyData = {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+
static bool
176173
HeapTupleSatisfiesSelf(HeapTuple htup, Snapshot snapshot, Buffer buffer)
177174
{
178175
HeapTupleHeader tuple = 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+
static bool
346343
HeapTupleSatisfiesAny(HeapTuple htup, Snapshot snapshot, Buffer buffer)
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+
static bool
366365
HeapTupleSatisfiesToast(HeapTuple htup, Snapshot snapshot,
367366
Buffer buffer)
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+
static bool
739735
HeapTupleSatisfiesDirty(HeapTuple htup, Snapshot snapshot,
740736
Buffer buffer)
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+
static bool
963952
HeapTupleSatisfiesMVCC(HeapTuple htup, Snapshot snapshot,
964953
Buffer buffer)
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+
static bool
13981389
HeapTupleSatisfiesNonVacuumable(HeapTuple htup, Snapshot snapshot,
13991390
Buffer buffer)
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+
static bool
16631654
HeapTupleSatisfiesHistoricMVCC(HeapTuple htup, Snapshot snapshot,
16641655
Buffer buffer)
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(HeapTuple tup, Snapshot snapshot, Buffer buffer)
1803+
{
1804+
switch (snapshot->snapshot_type)
1805+
{
1806+
case SNAPSHOT_MVCC:
1807+
return HeapTupleSatisfiesMVCC(tup, snapshot, buffer);
1808+
break;
1809+
case SNAPSHOT_SELF:
1810+
return HeapTupleSatisfiesSelf(tup, snapshot, buffer);
1811+
break;
1812+
case SNAPSHOT_ANY:
1813+
return HeapTupleSatisfiesAny(tup, snapshot, buffer);
1814+
break;
1815+
case SNAPSHOT_TOAST:
1816+
return HeapTupleSatisfiesToast(tup, snapshot, buffer);
1817+
break;
1818+
case SNAPSHOT_DIRTY:
1819+
return HeapTupleSatisfiesDirty(tup, snapshot, buffer);
1820+
break;
1821+
case SNAPSHOT_HISTORIC_MVCC:
1822+
return HeapTupleSatisfiesHistoricMVCC(tup, snapshot, buffer);
1823+
break;
1824+
case SNAPSHOT_NON_VACUUMABLE:
1825+
return HeapTupleSatisfiesNonVacuumable(tup, snapshot, buffer);
1826+
break;
1827+
}
1828+
1829+
return false; /* keep compiler quiet */
1830+
}

src/include/storage/bufmgr.h

+2-3
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
typedef void *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

+80-10
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+
typedef enum SnapshotType
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
typedef struct SnapshotData *Snapshot;
24103

25104
#define InvalidSnapshot ((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-
typedef bool (*SnapshotSatisfiesFunc) (HeapTuple htup,
34-
Snapshot snapshot, Buffer buffer);
35-
36106
/*
37107
* Struct representing all kind of possible snapshots.
38108
*
@@ -52,7 +122,7 @@ typedef bool (*SnapshotSatisfiesFunc) (HeapTuple htup,
52122
*/
53123
typedef struct SnapshotData
54124
{
55-
SnapshotSatisfiesFunc satisfies; /* tuple test function */
125+
SnapshotType snapshot_type; /* type of snapshot */
56126

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

0 commit comments

Comments
 (0)