Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf0718c

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 1446612 commit bf0718c

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
@@ -51,7 +51,7 @@ regresscheck-install-force: | submake-regress submake-test_decoding temp-install
5151
$(REGRESSCHECKS)
5252

5353
ISOLATIONCHECKS=mxact delayed_startup ondisk_startup concurrent_ddl_dml \
54-
oldest_xmin snapshot_transfer subxact_without_top
54+
oldest_xmin snapshot_transfer subxact_without_top catalog_change_snapshot
5555

5656
isolationcheck: | submake-isolation submake-test_decoding temp-install
5757
$(pg_isolation_regress_check) \
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
(0 rows)
27+
28+
step s0_commit: COMMIT;
29+
step s1_get_changes: SELECT data FROM pg_logical_slot_get_changes('isolation_slot', NULL, NULL, 'skip-empty-xacts', '1', 'include-xids', '0');
30+
data
31+
-------------------------------------------------------------
32+
BEGIN
33+
table public.tbl1: INSERT: val1[integer]:1 val2[integer]:null
34+
COMMIT
35+
(3 rows)
36+
37+
?column?
38+
--------
39+
stop
40+
(1 row)
41+
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: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,21 @@ DecodeCommit(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
561561
{
562562
ReorderBufferAddInvalidations(ctx->reorder, xid, buf->origptr,
563563
parsed->nmsgs, parsed->msgs);
564-
ReorderBufferXidSetCatalogChanges(ctx->reorder, xid, buf->origptr);
564+
565+
/*
566+
* If the COMMIT record has invalidation messages, it could have catalog
567+
* changes. It is possible that we didn't mark this transaction and
568+
* its subtransactions as containing catalog changes when the decoding
569+
* starts from a commit record without decoding the transaction's other
570+
* changes. Therefore, we ensure to mark such transactions as containing
571+
* catalog change.
572+
*
573+
* This must be done before SnapBuildCommitTxn() so that we can include
574+
* these transactions in the historic snapshot.
575+
*/
576+
SnapBuildXidSetCatalogChanges(ctx->snapshot_builder, xid,
577+
parsed->nsubxacts, parsed->subxacts,
578+
buf->origptr);
565579
}
566580

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

src/backend/replication/logical/snapbuild.c

Lines changed: 128 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,38 @@ struct SnapBuild
258258
static ResourceOwner SavedResourceOwnerDuringExport = NULL;
259259
static bool ExportInProgress = false;
260260

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

264294
/* snapshot building/manipulation/distribution functions */
265295
static Snapshot SnapBuildBuildSnapshot(SnapBuild *builder);
@@ -896,12 +926,17 @@ SnapBuildAddCommittedTxn(SnapBuild *builder, TransactionId xid)
896926
}
897927

898928
/*
899-
* Remove knowledge about transactions we treat as committed that are smaller
900-
* than ->xmin. Those won't ever get checked via the ->committed array but via
901-
* the clog machinery, so we don't need to waste memory on them.
929+
* Remove knowledge about transactions we treat as committed and the initial
930+
* running transactions that are smaller than ->xmin. Those won't ever get
931+
* checked via the ->committed or InitialRunningXacts array, respectively.
932+
* The committed xids will get checked via the clog machinery.
933+
*
934+
* We can ideally remove the transaction from InitialRunningXacts array
935+
* once it is finished (committed/aborted) but that could be costly as we need
936+
* to maintain the xids order in the array.
902937
*/
903938
static void
904-
SnapBuildPurgeCommittedTxn(SnapBuild *builder)
939+
SnapBuildPurgeOlderTxn(SnapBuild *builder)
905940
{
906941
int off;
907942
TransactionId *workspace;
@@ -936,6 +971,49 @@ SnapBuildPurgeCommittedTxn(SnapBuild *builder)
936971
builder->committed.xcnt = surviving_xids;
937972

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

9411019
/*
@@ -1143,7 +1221,7 @@ SnapBuildProcessRunningXacts(SnapBuild *builder, XLogRecPtr lsn, xl_running_xact
11431221
builder->xmin = running->oldestRunningXid;
11441222

11451223
/* Remove transactions we don't need to keep track off anymore */
1146-
SnapBuildPurgeCommittedTxn(builder);
1224+
SnapBuildPurgeOlderTxn(builder);
11471225

11481226
/*
11491227
* Advance the xmin limit for the current replication slot, to allow
@@ -1294,6 +1372,20 @@ SnapBuildFindSnapshot(SnapBuild *builder, XLogRecPtr lsn, xl_running_xacts *runn
12941372
else if (!builder->building_full_snapshot &&
12951373
SnapBuildRestore(builder, lsn))
12961374
{
1375+
int nxacts = running->subxcnt + running->xcnt;
1376+
Size sz = sizeof(TransactionId) * nxacts;
1377+
1378+
/*
1379+
* Remember the transactions and subtransactions that were running
1380+
* when xl_running_xacts record that we decoded was written. We use
1381+
* this later to identify the transactions have performed catalog
1382+
* changes. See SnapBuildXidSetCatalogChanges.
1383+
*/
1384+
NInitialRunningXacts = nxacts;
1385+
InitialRunningXacts = MemoryContextAlloc(builder->context, sz);
1386+
memcpy(InitialRunningXacts, running->xids, sz);
1387+
qsort(InitialRunningXacts, nxacts, sizeof(TransactionId), xidComparator);
1388+
12971389
/* there won't be any state to cleanup */
12981390
return false;
12991391
}
@@ -1997,3 +2089,32 @@ CheckPointSnapBuild(void)
19972089
}
19982090
FreeDir(snap_dir);
19992091
}
2092+
2093+
/*
2094+
* Mark the transaction as containing catalog changes. In addition, if the
2095+
* given xid is in the list of the initial running xacts, we mark its
2096+
* subtransactions as well. See comments for NInitialRunningXacts and
2097+
* InitialRunningXacts for additional info.
2098+
*/
2099+
void
2100+
SnapBuildXidSetCatalogChanges(SnapBuild *builder, TransactionId xid, int subxcnt,
2101+
TransactionId *subxacts, XLogRecPtr lsn)
2102+
{
2103+
ReorderBufferXidSetCatalogChanges(builder->reorder, xid, lsn);
2104+
2105+
/* Skip if there is no initial running xacts information */
2106+
if (NInitialRunningXacts == 0)
2107+
return;
2108+
2109+
if (bsearch(&xid, InitialRunningXacts, NInitialRunningXacts,
2110+
sizeof(TransactionId), xidComparator) != NULL)
2111+
{
2112+
int i;
2113+
2114+
for (i = 0; i < subxcnt; i++)
2115+
{
2116+
ReorderBufferAssignChild(builder->reorder, xid, subxacts[i], lsn);
2117+
ReorderBufferXidSetCatalogChanges(builder->reorder, subxacts[i], lsn);
2118+
}
2119+
}
2120+
}

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)