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

Commit 547b963

Browse files
author
Amit Kapila
committed
Fix catalog lookup with the wrong snapshot during logical decoding.
Previously, we relied on HEAP2_NEW_CID records and XACT_INVALIDATION records to know if the transaction has modified the catalog, and that information is not serialized to snapshot. Therefore, after the restart, if the logical decoding decodes only the commit record of the transaction that has actually modified a catalog, we will miss adding its XID to the snapshot. Thus, we will end up looking at catalogs with the wrong snapshot. To fix this problem, this changes the snapshot builder so that it remembers the last-running-xacts list of the decoded RUNNING_XACTS record after restoring the previously serialized snapshot. Then, we mark the transaction as containing catalog changes if it's in the list of initial running transactions and its commit record has XACT_XINFO_HAS_INVALS. To avoid ABI breakage, we store the array of the initial running transactions in the static variables InitialRunningXacts and NInitialRunningXacts, instead of storing those in SnapBuild or ReorderBuffer. This approach has a false positive; we could end up adding the transaction that didn't change catalog to the snapshot since we cannot distinguish whether the transaction has catalog changes only by checking the COMMIT record. It doesn't have the information on which (sub) transaction has catalog changes, and XACT_XINFO_HAS_INVALS doesn't necessarily indicate that the transaction has catalog change. But that won't be a problem since we use snapshot built during decoding only to read system catalogs. On the master branch, we took a more future-proof approach by writing catalog modifying transactions to the serialized snapshot which avoids the above false positive. But we cannot backpatch it because of a change in the SnapBuild. Reported-by: Mike Oh Author: Masahiko Sawada Reviewed-by: Amit Kapila, Shi yu, Takamichi Osumi, Kyotaro Horiguchi, Bertrand Drouvot, Ahsan Hadi Backpatch-through: 10 Discussion: https://postgr.es/m/81D0D8B0-E7C4-4999-B616-1E5004DBDCD2%40amazon.com
1 parent 71caf3c commit 547b963

File tree

6 files changed

+227
-9
lines changed

6 files changed

+227
-9
lines changed

contrib/test_decoding/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ REGRESS = ddl xact rewrite toast permissions decoding_in_xact \
77
decoding_into_rel binary prepared replorigin time messages \
88
spill slot truncate
99
ISOLATION = mxact delayed_startup ondisk_startup concurrent_ddl_dml \
10-
oldest_xmin snapshot_transfer subxact_without_top
10+
oldest_xmin snapshot_transfer subxact_without_top catalog_change_snapshot
1111

1212
REGRESS_OPTS = --temp-config $(top_srcdir)/contrib/test_decoding/logical.conf
1313
ISOLATION_OPTS = --temp-config $(top_srcdir)/contrib/test_decoding/logical.conf
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Parsed test spec with 2 sessions
2+
3+
starting permutation: s0_init s0_begin s0_savepoint s0_truncate s1_checkpoint s1_get_changes s0_commit s0_begin s0_insert s1_checkpoint s1_get_changes s0_commit s1_get_changes
4+
step s0_init: SELECT 'init' FROM pg_create_logical_replication_slot('isolation_slot', 'test_decoding');
5+
?column?
6+
--------
7+
init
8+
(1 row)
9+
10+
step s0_begin: BEGIN;
11+
step s0_savepoint: SAVEPOINT sp1;
12+
step s0_truncate: TRUNCATE tbl1;
13+
step s1_checkpoint: CHECKPOINT;
14+
step s1_get_changes: SELECT data FROM pg_logical_slot_get_changes('isolation_slot', NULL, NULL, 'skip-empty-xacts', '1', 'include-xids', '0');
15+
data
16+
----
17+
(0 rows)
18+
19+
step s0_commit: COMMIT;
20+
step s0_begin: BEGIN;
21+
step s0_insert: INSERT INTO tbl1 VALUES (1);
22+
step s1_checkpoint: CHECKPOINT;
23+
step s1_get_changes: SELECT data FROM pg_logical_slot_get_changes('isolation_slot', NULL, NULL, 'skip-empty-xacts', '1', 'include-xids', '0');
24+
data
25+
---------------------------------------
26+
BEGIN
27+
table public.tbl1: TRUNCATE: (no-flags)
28+
COMMIT
29+
(3 rows)
30+
31+
step s0_commit: COMMIT;
32+
step s1_get_changes: SELECT data FROM pg_logical_slot_get_changes('isolation_slot', NULL, NULL, 'skip-empty-xacts', '1', 'include-xids', '0');
33+
data
34+
-------------------------------------------------------------
35+
BEGIN
36+
table public.tbl1: INSERT: val1[integer]:1 val2[integer]:null
37+
COMMIT
38+
(3 rows)
39+
40+
?column?
41+
--------
42+
stop
43+
(1 row)
44+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Test decoding only the commit record of the transaction that have
2+
# modified catalogs.
3+
setup
4+
{
5+
DROP TABLE IF EXISTS tbl1;
6+
CREATE TABLE tbl1 (val1 integer, val2 integer);
7+
}
8+
9+
teardown
10+
{
11+
DROP TABLE tbl1;
12+
SELECT 'stop' FROM pg_drop_replication_slot('isolation_slot');
13+
}
14+
15+
session "s0"
16+
setup { SET synchronous_commit=on; }
17+
step "s0_init" { SELECT 'init' FROM pg_create_logical_replication_slot('isolation_slot', 'test_decoding'); }
18+
step "s0_begin" { BEGIN; }
19+
step "s0_savepoint" { SAVEPOINT sp1; }
20+
step "s0_truncate" { TRUNCATE tbl1; }
21+
step "s0_insert" { INSERT INTO tbl1 VALUES (1); }
22+
step "s0_commit" { COMMIT; }
23+
24+
session "s1"
25+
setup { SET synchronous_commit=on; }
26+
step "s1_checkpoint" { CHECKPOINT; }
27+
step "s1_get_changes" { SELECT data FROM pg_logical_slot_get_changes('isolation_slot', NULL, NULL, 'skip-empty-xacts', '1', 'include-xids', '0'); }
28+
29+
# For the transaction that TRUNCATEd the table tbl1, the last decoding decodes
30+
# only its COMMIT record, because it starts from the RUNNING_XACTS record emitted
31+
# during the first checkpoint execution. This transaction must be marked as
32+
# containing catalog changes while decoding the COMMIT record and the decoding
33+
# of the INSERT record must read the pg_class with the correct historic snapshot.
34+
#
35+
# Note that in a case where bgwriter wrote the RUNNING_XACTS record between "s0_commit"
36+
# and "s0_begin", this doesn't happen as the decoding starts from the RUNNING_XACTS
37+
# record written by bgwriter. One might think we can either stop the bgwriter or
38+
# increase LOG_SNAPSHOT_INTERVAL_MS but it's not practical via tests.
39+
permutation "s0_init" "s0_begin" "s0_savepoint" "s0_truncate" "s1_checkpoint" "s1_get_changes" "s0_commit" "s0_begin" "s0_insert" "s1_checkpoint" "s1_get_changes" "s0_commit" "s1_get_changes"

src/backend/replication/logical/decode.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,20 @@ DecodeCommit(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
582582
if (!ctx->fast_forward)
583583
ReorderBufferAddInvalidations(ctx->reorder, xid, buf->origptr,
584584
parsed->nmsgs, parsed->msgs);
585-
ReorderBufferXidSetCatalogChanges(ctx->reorder, xid, buf->origptr);
585+
/*
586+
* If the COMMIT record has invalidation messages, it could have catalog
587+
* changes. It is possible that we didn't mark this transaction and
588+
* its subtransactions as containing catalog changes when the decoding
589+
* starts from a commit record without decoding the transaction's other
590+
* changes. Therefore, we ensure to mark such transactions as containing
591+
* catalog change.
592+
*
593+
* This must be done before SnapBuildCommitTxn() so that we can include
594+
* these transactions in the historic snapshot.
595+
*/
596+
SnapBuildXidSetCatalogChanges(ctx->snapshot_builder, xid,
597+
parsed->nsubxacts, parsed->subxacts,
598+
buf->origptr);
586599
}
587600

588601
SnapBuildCommitTxn(ctx->snapshot_builder, buf->origptr, xid,

src/backend/replication/logical/snapbuild.c

Lines changed: 126 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,38 @@ struct SnapBuild
252252
static ResourceOwner SavedResourceOwnerDuringExport = NULL;
253253
static bool ExportInProgress = false;
254254

255-
/* ->committed manipulation */
256-
static void SnapBuildPurgeCommittedTxn(SnapBuild *builder);
255+
/*
256+
* Array of transactions and subtransactions that were running when
257+
* the xl_running_xacts record that we decoded was written. The array is
258+
* sorted in xidComparator order. We remove xids from this array when
259+
* they become old enough to matter, and then it eventually becomes empty.
260+
* This array is allocated in builder->context so its lifetime is the same
261+
* as the snapshot builder.
262+
*
263+
* We normally rely on some WAL record types such as HEAP2_NEW_CID to know
264+
* if the transaction has changed the catalog. But it could happen that the
265+
* logical decoding decodes only the commit record of the transaction after
266+
* restoring the previously serialized snapshot in which case we will miss
267+
* adding the xid to the snapshot and end up looking at the catalogs with the
268+
* wrong snapshot.
269+
*
270+
* Now to avoid the above problem, if the COMMIT record of the xid listed in
271+
* InitialRunningXacts has XACT_XINFO_HAS_INVALS flag, we mark both the top
272+
* transaction and its substransactions as containing catalog changes.
273+
*
274+
* We could end up adding the transaction that didn't change catalog
275+
* to the snapshot since we cannot distinguish whether the transaction
276+
* has catalog changes only by checking the COMMIT record. It doesn't
277+
* have the information on which (sub) transaction has catalog changes,
278+
* and XACT_XINFO_HAS_INVALS doesn't necessarily indicate that the
279+
* transaction has catalog change. But that won't be a problem since we
280+
* use snapshot built during decoding only for reading system catalogs.
281+
*/
282+
static TransactionId *InitialRunningXacts = NULL;
283+
static int NInitialRunningXacts = 0;
284+
285+
/* ->committed and InitailRunningXacts manipulation */
286+
static void SnapBuildPurgeOlderTxn(SnapBuild *builder);
257287

258288
/* snapshot building/manipulation/distribution functions */
259289
static Snapshot SnapBuildBuildSnapshot(SnapBuild *builder);
@@ -890,12 +920,17 @@ SnapBuildAddCommittedTxn(SnapBuild *builder, TransactionId xid)
890920
}
891921

892922
/*
893-
* Remove knowledge about transactions we treat as committed that are smaller
894-
* than ->xmin. Those won't ever get checked via the ->committed array but via
895-
* the clog machinery, so we don't need to waste memory on them.
923+
* Remove knowledge about transactions we treat as committed and the initial
924+
* running transactions that are smaller than ->xmin. Those won't ever get
925+
* checked via the ->committed or InitialRunningXacts array, respectively.
926+
* The committed xids will get checked via the clog machinery.
927+
*
928+
* We can ideally remove the transaction from InitialRunningXacts array
929+
* once it is finished (committed/aborted) but that could be costly as we need
930+
* to maintain the xids order in the array.
896931
*/
897932
static void
898-
SnapBuildPurgeCommittedTxn(SnapBuild *builder)
933+
SnapBuildPurgeOlderTxn(SnapBuild *builder)
899934
{
900935
int off;
901936
TransactionId *workspace;
@@ -930,6 +965,49 @@ SnapBuildPurgeCommittedTxn(SnapBuild *builder)
930965
builder->committed.xcnt = surviving_xids;
931966

932967
pfree(workspace);
968+
969+
/* Quick exit if there is no initial running transactions */
970+
if (NInitialRunningXacts == 0)
971+
return;
972+
973+
/* bound check if there is at least one transaction to remove */
974+
if (!NormalTransactionIdPrecedes(InitialRunningXacts[0],
975+
builder->xmin))
976+
return;
977+
978+
/*
979+
* purge xids in InitialRunningXacts as well. The purged array must also
980+
* be sorted in xidComparator order.
981+
*/
982+
workspace =
983+
MemoryContextAlloc(builder->context,
984+
NInitialRunningXacts * sizeof(TransactionId));
985+
surviving_xids = 0;
986+
for (off = 0; off < NInitialRunningXacts; off++)
987+
{
988+
if (NormalTransactionIdPrecedes(InitialRunningXacts[off],
989+
builder->xmin))
990+
; /* remove */
991+
else
992+
workspace[surviving_xids++] = InitialRunningXacts[off];
993+
}
994+
995+
if (surviving_xids > 0)
996+
memcpy(InitialRunningXacts, workspace,
997+
sizeof(TransactionId) * surviving_xids);
998+
else
999+
{
1000+
pfree(InitialRunningXacts);
1001+
InitialRunningXacts = NULL;
1002+
}
1003+
1004+
elog(DEBUG3, "purged initial running transactions from %u to %u, oldest running xid %u",
1005+
(uint32) NInitialRunningXacts,
1006+
(uint32) surviving_xids,
1007+
builder->xmin);
1008+
1009+
NInitialRunningXacts = surviving_xids;
1010+
pfree(workspace);
9331011
}
9341012

9351013
/*
@@ -1137,7 +1215,7 @@ SnapBuildProcessRunningXacts(SnapBuild *builder, XLogRecPtr lsn, xl_running_xact
11371215
builder->xmin = running->oldestRunningXid;
11381216

11391217
/* Remove transactions we don't need to keep track off anymore */
1140-
SnapBuildPurgeCommittedTxn(builder);
1218+
SnapBuildPurgeOlderTxn(builder);
11411219

11421220
/*
11431221
* Advance the xmin limit for the current replication slot, to allow
@@ -1288,6 +1366,20 @@ SnapBuildFindSnapshot(SnapBuild *builder, XLogRecPtr lsn, xl_running_xacts *runn
12881366
else if (!builder->building_full_snapshot &&
12891367
SnapBuildRestore(builder, lsn))
12901368
{
1369+
int nxacts = running->subxcnt + running->xcnt;
1370+
Size sz = sizeof(TransactionId) * nxacts;
1371+
1372+
/*
1373+
* Remember the transactions and subtransactions that were running
1374+
* when xl_running_xacts record that we decoded was written. We use
1375+
* this later to identify the transactions have performed catalog
1376+
* changes. See SnapBuildXidSetCatalogChanges.
1377+
*/
1378+
NInitialRunningXacts = nxacts;
1379+
InitialRunningXacts = MemoryContextAlloc(builder->context, sz);
1380+
memcpy(InitialRunningXacts, running->xids, sz);
1381+
qsort(InitialRunningXacts, nxacts, sizeof(TransactionId), xidComparator);
1382+
12911383
/* there won't be any state to cleanup */
12921384
return false;
12931385
}
@@ -2030,3 +2122,30 @@ CheckPointSnapBuild(void)
20302122
}
20312123
FreeDir(snap_dir);
20322124
}
2125+
2126+
/*
2127+
* Mark the transaction as containing catalog changes. In addition, if the
2128+
* given xid is in the list of the initial running xacts, we mark its
2129+
* subtransactions as well. See comments for NInitialRunningXacts and
2130+
* InitialRunningXacts for additional info.
2131+
*/
2132+
void
2133+
SnapBuildXidSetCatalogChanges(SnapBuild *builder, TransactionId xid, int subxcnt,
2134+
TransactionId *subxacts, XLogRecPtr lsn)
2135+
{
2136+
ReorderBufferXidSetCatalogChanges(builder->reorder, xid, lsn);
2137+
2138+
/* Skip if there is no initial running xacts information */
2139+
if (NInitialRunningXacts == 0)
2140+
return;
2141+
2142+
if (bsearch(&xid, InitialRunningXacts, NInitialRunningXacts,
2143+
sizeof(TransactionId), xidComparator) != NULL)
2144+
{
2145+
for (int i = 0; i < subxcnt; i++)
2146+
{
2147+
ReorderBufferAssignChild(builder->reorder, xid, subxacts[i], lsn);
2148+
ReorderBufferXidSetCatalogChanges(builder->reorder, subxacts[i], lsn);
2149+
}
2150+
}
2151+
}

src/include/replication/snapbuild.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,7 @@ extern void SnapBuildProcessRunningXacts(SnapBuild *builder, XLogRecPtr lsn,
8888
struct xl_running_xacts *running);
8989
extern void SnapBuildSerializationPoint(SnapBuild *builder, XLogRecPtr lsn);
9090

91+
extern void SnapBuildXidSetCatalogChanges(SnapBuild *builder, TransactionId xid,
92+
int subxcnt, TransactionId *subxacts,
93+
XLogRecPtr lsn);
9194
#endif /* SNAPBUILD_H */

0 commit comments

Comments
 (0)