Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit be843ce

Browse files
committed
Fix CREATE INDEX CONCURRENTLY for simultaneous prepared transactions.
In a cluster having used CREATE INDEX CONCURRENTLY while having enabled prepared transactions, queries that use the resulting index can silently fail to find rows. Fix this for future CREATE INDEX CONCURRENTLY by making it wait for prepared transactions like it waits for ordinary transactions. This expands the VirtualTransactionId structure domain to admit prepared transactions. It may be necessary to reindex to recover from past occurrences. Back-patch to 9.5 (all supported versions). Andrey Borodin, reviewed (in earlier versions) by Tom Lane and Michael Paquier. Discussion: https://postgr.es/m/2E712143-97F7-4890-B470-4A35142ABC82@yandex-team.ru
1 parent bd63661 commit be843ce

File tree

7 files changed

+98
-37
lines changed

7 files changed

+98
-37
lines changed

src/backend/storage/lmgr/lmgr.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -904,8 +904,7 @@ WaitForLockersMultiple(List *locktags, LOCKMODE lockmode, bool progress)
904904

905905
/*
906906
* Note: GetLockConflicts() never reports our own xid, hence we need not
907-
* check for that. Also, prepared xacts are not reported, which is fine
908-
* since they certainly aren't going to do anything anymore.
907+
* check for that. Also, prepared xacts are reported and awaited.
909908
*/
910909

911910
/* Finally wait for each such transaction to complete */

src/backend/storage/lmgr/lock.c

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2813,9 +2813,7 @@ FastPathGetRelationLockEntry(LOCALLOCK *locallock)
28132813
* so use of this function has to be thought about carefully.
28142814
*
28152815
* Note we never include the current xact's vxid in the result array,
2816-
* since an xact never blocks itself. Also, prepared transactions are
2817-
* ignored, which is a bit more debatable but is appropriate for current
2818-
* uses of the result.
2816+
* since an xact never blocks itself.
28192817
*/
28202818
VirtualTransactionId *
28212819
GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
@@ -2840,19 +2838,21 @@ GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
28402838

28412839
/*
28422840
* Allocate memory to store results, and fill with InvalidVXID. We only
2843-
* need enough space for MaxBackends + a terminator, since prepared xacts
2844-
* don't count. InHotStandby allocate once in TopMemoryContext.
2841+
* need enough space for MaxBackends + max_prepared_xacts + a terminator.
2842+
* InHotStandby allocate once in TopMemoryContext.
28452843
*/
28462844
if (InHotStandby)
28472845
{
28482846
if (vxids == NULL)
28492847
vxids = (VirtualTransactionId *)
28502848
MemoryContextAlloc(TopMemoryContext,
2851-
sizeof(VirtualTransactionId) * (MaxBackends + 1));
2849+
sizeof(VirtualTransactionId) *
2850+
(MaxBackends + max_prepared_xacts + 1));
28522851
}
28532852
else
28542853
vxids = (VirtualTransactionId *)
2855-
palloc0(sizeof(VirtualTransactionId) * (MaxBackends + 1));
2854+
palloc0(sizeof(VirtualTransactionId) *
2855+
(MaxBackends + max_prepared_xacts + 1));
28562856

28572857
/* Compute hash code and partition lock, and look up conflicting modes. */
28582858
hashcode = LockTagHashCode(locktag);
@@ -2927,13 +2927,9 @@ GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
29272927
/* Conflict! */
29282928
GET_VXID_FROM_PGPROC(vxid, *proc);
29292929

2930-
/*
2931-
* If we see an invalid VXID, then either the xact has already
2932-
* committed (or aborted), or it's a prepared xact. In either
2933-
* case we may ignore it.
2934-
*/
29352930
if (VirtualTransactionIdIsValid(vxid))
29362931
vxids[count++] = vxid;
2932+
/* else, xact already committed or aborted */
29372933

29382934
/* No need to examine remaining slots. */
29392935
break;
@@ -2992,11 +2988,6 @@ GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
29922988

29932989
GET_VXID_FROM_PGPROC(vxid, *proc);
29942990

2995-
/*
2996-
* If we see an invalid VXID, then either the xact has already
2997-
* committed (or aborted), or it's a prepared xact. In either
2998-
* case we may ignore it.
2999-
*/
30002991
if (VirtualTransactionIdIsValid(vxid))
30012992
{
30022993
int i;
@@ -3008,6 +2999,7 @@ GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
30082999
if (i >= fast_count)
30093000
vxids[count++] = vxid;
30103001
}
3002+
/* else, xact already committed or aborted */
30113003
}
30123004
}
30133005

@@ -3017,7 +3009,7 @@ GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
30173009

30183010
LWLockRelease(partitionLock);
30193011

3020-
if (count > MaxBackends) /* should never happen */
3012+
if (count > MaxBackends + max_prepared_xacts) /* should never happen */
30213013
elog(PANIC, "too many conflicting locks found");
30223014

30233015
vxids[count].backendId = InvalidBackendId;
@@ -4375,6 +4367,21 @@ VirtualXactLock(VirtualTransactionId vxid, bool wait)
43754367

43764368
Assert(VirtualTransactionIdIsValid(vxid));
43774369

4370+
if (VirtualTransactionIdIsPreparedXact(vxid))
4371+
{
4372+
LockAcquireResult lar;
4373+
4374+
/*
4375+
* Prepared transactions don't hold vxid locks. The
4376+
* LocalTransactionId is always a normal, locked XID.
4377+
*/
4378+
SET_LOCKTAG_TRANSACTION(tag, vxid.localTransactionId);
4379+
lar = LockAcquire(&tag, ShareLock, false, !wait);
4380+
if (lar != LOCKACQUIRE_NOT_AVAIL)
4381+
LockRelease(&tag, ShareLock, false);
4382+
return lar != LOCKACQUIRE_NOT_AVAIL;
4383+
}
4384+
43784385
SET_LOCKTAG_VIRTUALTRANSACTION(tag, vxid);
43794386

43804387
/*

src/include/storage/lock.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ extern bool Debug_deadlocks;
4747

4848
/*
4949
* Top-level transactions are identified by VirtualTransactionIDs comprising
50-
* the BackendId of the backend running the xact, plus a locally-assigned
51-
* LocalTransactionId. These are guaranteed unique over the short term,
52-
* but will be reused after a database restart; hence they should never
53-
* be stored on disk.
50+
* PGPROC fields backendId and lxid. For prepared transactions, the
51+
* LocalTransactionId is an ordinary XID. These are guaranteed unique over
52+
* the short term, but will be reused after a database restart or XID
53+
* wraparound; hence they should never be stored on disk.
5454
*
5555
* Note that struct VirtualTransactionId can not be assumed to be atomically
5656
* assignable as a whole. However, type LocalTransactionId is assumed to
@@ -62,15 +62,16 @@ extern bool Debug_deadlocks;
6262
*/
6363
typedef struct
6464
{
65-
BackendId backendId; /* determined at backend startup */
66-
LocalTransactionId localTransactionId; /* backend-local transaction id */
65+
BackendId backendId; /* backendId from PGPROC */
66+
LocalTransactionId localTransactionId; /* lxid from PGPROC */
6767
} VirtualTransactionId;
6868

6969
#define InvalidLocalTransactionId 0
7070
#define LocalTransactionIdIsValid(lxid) ((lxid) != InvalidLocalTransactionId)
7171
#define VirtualTransactionIdIsValid(vxid) \
72-
(((vxid).backendId != InvalidBackendId) && \
73-
LocalTransactionIdIsValid((vxid).localTransactionId))
72+
(LocalTransactionIdIsValid((vxid).localTransactionId))
73+
#define VirtualTransactionIdIsPreparedXact(vxid) \
74+
((vxid).backendId == InvalidBackendId)
7475
#define VirtualTransactionIdEquals(vxid1, vxid2) \
7576
((vxid1).backendId == (vxid2).backendId && \
7677
(vxid1).localTransactionId == (vxid2).localTransactionId)

src/test/isolation/Makefile

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,11 @@ installcheck: all
5555
check: all
5656
$(pg_isolation_regress_check) --schedule=$(srcdir)/isolation_schedule
5757

58-
# Versions of the check tests that include the prepared_transactions test
59-
# It only makes sense to run these if set up to use prepared transactions,
60-
# via TEMP_CONFIG for the check case, or via the postgresql.conf for the
61-
# installcheck case.
58+
# Non-default tests. It only makes sense to run these if set up to use
59+
# prepared transactions, via TEMP_CONFIG for the check case, or via the
60+
# postgresql.conf for the installcheck case.
6261
installcheck-prepared-txns: all temp-install
63-
$(pg_isolation_regress_installcheck) --schedule=$(srcdir)/isolation_schedule prepared-transactions
62+
$(pg_isolation_regress_installcheck) --schedule=$(srcdir)/isolation_schedule prepared-transactions prepared-transactions-cic
6463

6564
check-prepared-txns: all temp-install
66-
$(pg_isolation_regress_check) --schedule=$(srcdir)/isolation_schedule prepared-transactions
65+
$(pg_isolation_regress_check) --schedule=$(srcdir)/isolation_schedule prepared-transactions prepared-transactions-cic

src/test/isolation/README

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ you can do something like
2323
./pg_isolation_regress fk-contention fk-deadlock
2424
(look into the specs/ subdirectory to see the available tests).
2525

26-
The prepared-transactions test requires the server's
27-
max_prepared_transactions parameter to be set to at least 3; therefore it
28-
is not run by default. To include it in the test run, use
26+
Certain tests require the server's max_prepared_transactions parameter to be
27+
set to at least 3; therefore they are not run by default. To include them in
28+
the test run, use
2929
make check-prepared-txns
3030
or
3131
make installcheck-prepared-txns
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Parsed test spec with 2 sessions
2+
3+
starting permutation: w1 p1 cic2 c1 r2
4+
step w1: BEGIN; INSERT INTO cic_test VALUES (1);
5+
step p1: PREPARE TRANSACTION 's1';
6+
step cic2:
7+
CREATE INDEX CONCURRENTLY on cic_test(a);
8+
9+
ERROR: canceling statement due to lock timeout
10+
step c1: COMMIT PREPARED 's1';
11+
step r2:
12+
SET enable_seqscan to off;
13+
SET enable_bitmapscan to off;
14+
SELECT * FROM cic_test WHERE a = 1;
15+
16+
a
17+
18+
1
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This test verifies that CREATE INDEX CONCURRENTLY interacts with prepared
2+
# transactions correctly.
3+
setup
4+
{
5+
CREATE TABLE cic_test (a int);
6+
}
7+
8+
teardown
9+
{
10+
DROP TABLE cic_test;
11+
}
12+
13+
14+
# Sessions for CREATE INDEX CONCURRENTLY test
15+
session "s1"
16+
step "w1" { BEGIN; INSERT INTO cic_test VALUES (1); }
17+
step "p1" { PREPARE TRANSACTION 's1'; }
18+
step "c1" { COMMIT PREPARED 's1'; }
19+
20+
session "s2"
21+
# The isolation tester never recognizes that a lock of s1 blocks s2, because a
22+
# prepared transaction's locks have no pid associated. While there's a slight
23+
# chance of timeout while waiting for an autovacuum-held lock, that wouldn't
24+
# change the output. Hence, no timeout is too short.
25+
setup { SET lock_timeout = 10; }
26+
step "cic2"
27+
{
28+
CREATE INDEX CONCURRENTLY on cic_test(a);
29+
}
30+
step "r2"
31+
{
32+
SET enable_seqscan to off;
33+
SET enable_bitmapscan to off;
34+
SELECT * FROM cic_test WHERE a = 1;
35+
}
36+
37+
permutation "w1" "p1" "cic2" "c1" "r2"

0 commit comments

Comments
 (0)