diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2024-06-17 14:30:59 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2024-06-17 14:30:59 -0400 |
commit | 92c49d1062f7094f56b80c603233fa4a0ffe2f8b (patch) | |
tree | 7f3a9297b4b7605ed252bafbaefea515685f0434 /src/include/access/spgist_private.h | |
parent | ba26d156636c84a9674e49dbdfe788b6291985f2 (diff) | |
download | postgresql-92c49d1062f7094f56b80c603233fa4a0ffe2f8b.tar.gz postgresql-92c49d1062f7094f56b80c603233fa4a0ffe2f8b.zip |
Fix insertion of SP-GiST REDIRECT tuples during REINDEX CONCURRENTLY.
Reconstruction of an SP-GiST index by REINDEX CONCURRENTLY may
insert some REDIRECT tuples. This will typically happen in
a transaction that lacks an XID, which leads either to assertion
failure in spgFormDeadTuple or to insertion of a REDIRECT tuple
with zero xid. The latter's not good either, since eventually
VACUUM will apply GlobalVisTestIsRemovableXid() to the zero xid,
resulting in either an assertion failure or a garbage answer.
In practice, since REINDEX CONCURRENTLY locks out index scans
till it's done, it doesn't matter whether it inserts REDIRECTs
or PLACEHOLDERs; and likewise it doesn't matter how soon VACUUM
reduces such a REDIRECT to a PLACEHOLDER. So in non-assert builds
there's no observable problem here, other than perhaps a little
index bloat. But it's not behaving as intended.
To fix, remove the failing Assert in spgFormDeadTuple, acknowledging
that we might sometimes insert a zero XID; and guard VACUUM's
GlobalVisTestIsRemovableXid() call with a test for valid XID,
ensuring that we'll reduce such a REDIRECT the first time VACUUM
sees it. (Versions before v14 use TransactionIdPrecedes here,
which won't fail on zero xid, so they really have no bug at all
in non-assert builds.)
Another solution could be to not create REDIRECTs at all during
REINDEX CONCURRENTLY, making the relevant code paths treat that
case like index build (which likewise knows that no concurrent
index scans can be happening). That would allow restoring the
Assert in spgFormDeadTuple, but we'd still need the VACUUM change
because redirection tuples with zero xid may be out there already.
But there doesn't seem to be a nice way for spginsert() to tell that
it's being called in REINDEX CONCURRENTLY without some API changes,
so we'll leave that as a possible future improvement.
In HEAD, also rename the SpGistState.myXid field to redirectXid,
which seems less misleading (since it might not in fact be our
transaction's XID) and is certainly less uninformatively generic.
Per bug #18499 from Alexander Lakhin. Back-patch to all supported
branches.
Discussion: https://postgr.es/m/18499-8a519c280f956480@postgresql.org
Diffstat (limited to 'src/include/access/spgist_private.h')
-rw-r--r-- | src/include/access/spgist_private.h | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/src/include/access/spgist_private.h b/src/include/access/spgist_private.h index 2e9c757b302..e7cbe10a89b 100644 --- a/src/include/access/spgist_private.h +++ b/src/include/access/spgist_private.h @@ -157,7 +157,7 @@ typedef struct SpGistState char *deadTupleStorage; /* workspace for spgFormDeadTuple */ - TransactionId myXid; /* XID to use when creating a redirect tuple */ + TransactionId redirectXid; /* XID to use when creating a redirect tuple */ bool isBuild; /* true if doing index build */ } SpGistState; @@ -421,7 +421,8 @@ typedef struct SpGistLeafTupleData * field, to satisfy some Asserts that we make when replacing a leaf tuple * with a dead tuple. * We don't use t_info, but it's needed to align the pointer field. - * pointer and xid are only valid when tupstate = REDIRECT. + * pointer and xid are only valid when tupstate = REDIRECT, and in some + * cases xid can be InvalidTransactionId even then; see initSpGistState. */ typedef struct SpGistDeadTupleData { @@ -464,7 +465,7 @@ typedef SpGistDeadTupleData *SpGistDeadTuple; #define STORE_STATE(s, d) \ do { \ - (d).myXid = (s)->myXid; \ + (d).redirectXid = (s)->redirectXid; \ (d).isBuild = (s)->isBuild; \ } while(0) |