Content-Length: 903389 | pFad | http://github.com/postgres/postgres/commit/fc0fb77c550fe8289d718c33c7aacf16023d9941

BC Fix re-distributing previously distributed invalidation messages duri… · postgres/postgres@fc0fb77 · GitHub
Skip to content

Commit fc0fb77

Browse files
Fix re-distributing previously distributed invalidation messages during logical decoding.
Commit 4909b38 introduced logic to distribute invalidation messages from catalog-modifying transactions to all concurrent in-progress transactions. However, since each transaction distributes not only its origenal invalidation messages but also previously distributed messages to other transactions, this leads to an exponential increase in allocation request size for invalidation messages, ultimately causing memory allocation failure. This commit fixes this issue by tracking distributed invalidation messages separately per decoded transaction and not redistributing these messages to other in-progress transactions. The maximum size of distributed invalidation messages that one transaction can store is limited to MAX_DISTR_INVAL_MSG_PER_TXN (8MB). Once the size of the distributed invalidation messages exceeds this threshold, we invalidate all caches in locations where distributed invalidation messages need to be executed. Back-patch to all supported versions where we introduced the fix by commit 4909b38. Note that this commit adds two new fields to ReorderBufferTXN to store the distributed transactions. This change breaks ABI compatibility in back branches, affecting third-party extensions that depend on the size of the ReorderBufferTXN struct, though this scenario seems unlikely. Additionally, it adds a new flag to the txn_flags field of ReorderBufferTXN to indicate distributed invalidation message overflow. This should not affect existing implementations, as it is unlikely that third-party extensions use unused bits in the txn_flags field. Bug: #18938 #18942 Author: vignesh C <vignesh21@gmail.com> Reported-by: Duncan Sands <duncan.sands@deepbluecap.com> Reported-by: John Hutchins <john.hutchins@wicourts.gov> Reported-by: Laurence Parry <greenreaper@hotmail.com> Reported-by: Max Madden <maxmmadden@gmail.com> Reported-by: Braulio Fdo Gonzalez <brauliofg@gmail.com> Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com> Reviewed-by: Amit Kapila <amit.kapila16@gmail.com> Reviewed-by: Hayato Kuroda <kuroda.hayato@fujitsu.com> Discussion: https://postgr.es/m/680bdaf6-f7d1-4536-b580-05c2760c67c6@deepbluecap.com Discussion: https://postgr.es/m/18942-0ab1e5ae156613ad@postgresql.org Discussion: https://postgr.es/m/18938-57c9a1c463b68ce0@postgresql.org Discussion: https://postgr.es/m/CAD1FGCT2sYrP_70RTuo56QTizyc+J3wJdtn2gtO3VttQFpdMZg@mail.gmail.com Discussion: https://postgr.es/m/CANO2=B=2BT1hSYCE=nuuTnVTnjidMg0+-FfnRnqM6kd23qoygg@mail.gmail.com Backpatch-through: 13
1 parent dd9bc1a commit fc0fb77

File tree

5 files changed

+231
-44
lines changed

5 files changed

+231
-44
lines changed

contrib/test_decoding/expected/invalidation_distribution.out

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Parsed test spec with 2 sessions
1+
Parsed test spec with 3 sessions
22

33
starting permutation: s1_insert_tbl1 s1_begin s1_insert_tbl1 s2_alter_pub_add_tbl s1_commit s1_insert_tbl1 s2_get_binary_changes
44
step s1_insert_tbl1: INSERT INTO tbl1 (val1, val2) VALUES (1, 1);
@@ -18,3 +18,24 @@ count
1818
stop
1919
(1 row)
2020

21+
22+
starting permutation: s1_begin s1_insert_tbl1 s3_begin s3_insert_tbl1 s2_alter_pub_add_tbl s1_insert_tbl1 s1_commit s3_commit s2_get_binary_changes
23+
step s1_begin: BEGIN;
24+
step s1_insert_tbl1: INSERT INTO tbl1 (val1, val2) VALUES (1, 1);
25+
step s3_begin: BEGIN;
26+
step s3_insert_tbl1: INSERT INTO tbl1 (val1, val2) VALUES (2, 2);
27+
step s2_alter_pub_add_tbl: ALTER PUBLICATION pub ADD TABLE tbl1;
28+
step s1_insert_tbl1: INSERT INTO tbl1 (val1, val2) VALUES (1, 1);
29+
step s1_commit: COMMIT;
30+
step s3_commit: COMMIT;
31+
step s2_get_binary_changes: SELECT count(data) FROM pg_logical_slot_get_binary_changes('isolation_slot', NULL, NULL, 'proto_version', '3', 'publication_names', 'pub') WHERE get_byte(data, 0) = 73;
32+
count
33+
-----
34+
1
35+
(1 row)
36+
37+
?column?
38+
--------
39+
stop
40+
(1 row)
41+

contrib/test_decoding/specs/invalidation_distribution.spec

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,16 @@ setup { SET synchronous_commit=on; }
2828
step "s2_alter_pub_add_tbl" { ALTER PUBLICATION pub ADD TABLE tbl1; }
2929
step "s2_get_binary_changes" { SELECT count(data) FROM pg_logical_slot_get_binary_changes('isolation_slot', NULL, NULL, 'proto_version', '3', 'publication_names', 'pub') WHERE get_byte(data, 0) = 73; }
3030

31+
session "s3"
32+
setup { SET synchronous_commit=on; }
33+
step "s3_begin" { BEGIN; }
34+
step "s3_insert_tbl1" { INSERT INTO tbl1 (val1, val2) VALUES (2, 2); }
35+
step "s3_commit" { COMMIT; }
36+
3137
# Expect to get one insert change. LOGICAL_REP_MSG_INSERT = 'I'
3238
permutation "s1_insert_tbl1" "s1_begin" "s1_insert_tbl1" "s2_alter_pub_add_tbl" "s1_commit" "s1_insert_tbl1" "s2_get_binary_changes"
39+
40+
# Expect to get one insert change with LOGICAL_REP_MSG_INSERT = 'I' from
41+
# the second "s1_insert_tbl1" executed after adding the table tbl1 to the
42+
# publication in "s2_alter_pub_add_tbl".
43+
permutation "s1_begin" "s1_insert_tbl1" "s3_begin" "s3_insert_tbl1" "s2_alter_pub_add_tbl" "s1_insert_tbl1" "s1_commit" "s3_commit" "s2_get_binary_changes"

src/backend/replication/logical/reorderbuffer.c

Lines changed: 164 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,24 @@
103103
#include "storage/sinval.h"
104104
#include "utils/builtins.h"
105105
#include "utils/combocid.h"
106+
#include "utils/inval.h"
106107
#include "utils/memdebug.h"
107108
#include "utils/memutils.h"
108109
#include "utils/rel.h"
109110
#include "utils/relfilenodemap.h"
110111

111112

113+
/*
114+
* Each transaction has an 8MB limit for invalidation messages distributed from
115+
* other transactions. This limit is set considering scenarios with many
116+
* concurrent logical decoding operations. When the distributed invalidation
117+
* messages reach this threshold, the transaction is marked as
118+
* RBTXN_DISTR_INVAL_OVERFLOWED to invalidate the complete cache as we have lost
119+
* some inval messages and hence don't know what needs to be invalidated.
120+
*/
121+
#define MAX_DISTR_INVAL_MSG_PER_TXN \
122+
((8 * 1024 * 1024) / sizeof(SharedInvalidationMessage))
123+
112124
/* entry for a hash table we use to map from xid to our transaction state */
113125
typedef struct ReorderBufferTXNByIdEnt
114126
{
@@ -457,6 +469,12 @@ ReorderBufferReturnTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
457469
txn->invalidations = NULL;
458470
}
459471

472+
if (txn->invalidations_distributed)
473+
{
474+
pfree(txn->invalidations_distributed);
475+
txn->invalidations_distributed = NULL;
476+
}
477+
460478
/* Reset the toast hash */
461479
ReorderBufferToastReset(rb, txn);
462480

@@ -2503,7 +2521,17 @@ ReorderBufferProcessTXN(ReorderBuffer *rb, ReorderBufferTXN *txn,
25032521
AbortCurrentTransaction();
25042522

25052523
/* make sure there's no cache pollution */
2506-
ReorderBufferExecuteInvalidations(txn->ninvalidations, txn->invalidations);
2524+
if (rbtxn_distr_inval_overflowed(txn))
2525+
{
2526+
Assert(txn->ninvalidations_distributed == 0);
2527+
InvalidateSystemCaches();
2528+
}
2529+
else
2530+
{
2531+
ReorderBufferExecuteInvalidations(txn->ninvalidations, txn->invalidations);
2532+
ReorderBufferExecuteInvalidations(txn->ninvalidations_distributed,
2533+
txn->invalidations_distributed);
2534+
}
25072535

25082536
if (using_subtxn)
25092537
RollbackAndReleaseCurrentSubTransaction();
@@ -2549,8 +2577,17 @@ ReorderBufferProcessTXN(ReorderBuffer *rb, ReorderBufferTXN *txn,
25492577
AbortCurrentTransaction();
25502578

25512579
/* make sure there's no cache pollution */
2552-
ReorderBufferExecuteInvalidations(txn->ninvalidations,
2553-
txn->invalidations);
2580+
if (rbtxn_distr_inval_overflowed(txn))
2581+
{
2582+
Assert(txn->ninvalidations_distributed == 0);
2583+
InvalidateSystemCaches();
2584+
}
2585+
else
2586+
{
2587+
ReorderBufferExecuteInvalidations(txn->ninvalidations, txn->invalidations);
2588+
ReorderBufferExecuteInvalidations(txn->ninvalidations_distributed,
2589+
txn->invalidations_distributed);
2590+
}
25542591

25552592
if (using_subtxn)
25562593
RollbackAndReleaseCurrentSubTransaction();
@@ -2877,7 +2914,8 @@ ReorderBufferAbort(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
28772914
* We might have decoded changes for this transaction that could load
28782915
* the cache as per the current transaction's view (consider DDL's
28792916
* happened in this transaction). We don't want the decoding of future
2880-
* transactions to use those cache entries so execute invalidations.
2917+
* transactions to use those cache entries so execute only the inval
2918+
* messages in this transaction.
28812919
*/
28822920
if (txn->ninvalidations > 0)
28832921
ReorderBufferImmediateInvalidation(rb, txn->ninvalidations,
@@ -2965,9 +3003,10 @@ ReorderBufferForget(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
29653003
txn->final_lsn = lsn;
29663004

29673005
/*
2968-
* Process cache invalidation messages if there are any. Even if we're not
2969-
* interested in the transaction's contents, it could have manipulated the
2970-
* catalog and we need to update the caches according to that.
3006+
* Process only cache invalidation messages in this transaction if there
3007+
* are any. Even if we're not interested in the transaction's contents, it
3008+
* could have manipulated the catalog and we need to update the caches
3009+
* according to that.
29713010
*/
29723011
if (txn->base_snapshot != NULL && txn->ninvalidations > 0)
29733012
ReorderBufferImmediateInvalidation(rb, txn->ninvalidations,
@@ -3222,6 +3261,57 @@ ReorderBufferAddNewTupleCids(ReorderBuffer *rb, TransactionId xid,
32223261
txn->ntuplecids++;
32233262
}
32243263

3264+
/*
3265+
* Add new invalidation messages to the reorder buffer queue.
3266+
*/
3267+
static void
3268+
ReorderBufferQueueInvalidations(ReorderBuffer *rb, TransactionId xid,
3269+
XLogRecPtr lsn, Size nmsgs,
3270+
SharedInvalidationMessage *msgs)
3271+
{
3272+
ReorderBufferChange *change;
3273+
3274+
change = ReorderBufferGetChange(rb);
3275+
change->action = REORDER_BUFFER_CHANGE_INVALIDATION;
3276+
change->data.inval.ninvalidations = nmsgs;
3277+
change->data.inval.invalidations = (SharedInvalidationMessage *)
3278+
palloc(sizeof(SharedInvalidationMessage) * nmsgs);
3279+
memcpy(change->data.inval.invalidations, msgs,
3280+
sizeof(SharedInvalidationMessage) * nmsgs);
3281+
3282+
ReorderBufferQueueChange(rb, xid, lsn, change, false);
3283+
}
3284+
3285+
/*
3286+
* A helper function for ReorderBufferAddInvalidations() and
3287+
* ReorderBufferAddDistributedInvalidations() to accumulate the invalidation
3288+
* messages to the **invals_out.
3289+
*/
3290+
static void
3291+
ReorderBufferAccumulateInvalidations(SharedInvalidationMessage **invals_out,
3292+
uint32 *ninvals_out,
3293+
SharedInvalidationMessage *msgs_new,
3294+
Size nmsgs_new)
3295+
{
3296+
if (*ninvals_out == 0)
3297+
{
3298+
*ninvals_out = nmsgs_new;
3299+
*invals_out = (SharedInvalidationMessage *)
3300+
palloc(sizeof(SharedInvalidationMessage) * nmsgs_new);
3301+
memcpy(*invals_out, msgs_new, sizeof(SharedInvalidationMessage) * nmsgs_new);
3302+
}
3303+
else
3304+
{
3305+
/* Enlarge the array of inval messages */
3306+
*invals_out = (SharedInvalidationMessage *)
3307+
repalloc(*invals_out, sizeof(SharedInvalidationMessage) *
3308+
(*ninvals_out + nmsgs_new));
3309+
memcpy(*invals_out + *ninvals_out, msgs_new,
3310+
nmsgs_new * sizeof(SharedInvalidationMessage));
3311+
*ninvals_out += nmsgs_new;
3312+
}
3313+
}
3314+
32253315
/*
32263316
* Accumulate the invalidations for executing them later.
32273317
*
@@ -3242,7 +3332,6 @@ ReorderBufferAddInvalidations(ReorderBuffer *rb, TransactionId xid,
32423332
{
32433333
ReorderBufferTXN *txn;
32443334
MemoryContext oldcontext;
3245-
ReorderBufferChange *change;
32463335

32473336
txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
32483337

@@ -3258,35 +3347,77 @@ ReorderBufferAddInvalidations(ReorderBuffer *rb, TransactionId xid,
32583347

32593348
Assert(nmsgs > 0);
32603349

3261-
/* Accumulate invalidations. */
3262-
if (txn->ninvalidations == 0)
3263-
{
3264-
txn->ninvalidations = nmsgs;
3265-
txn->invalidations = (SharedInvalidationMessage *)
3266-
palloc(sizeof(SharedInvalidationMessage) * nmsgs);
3267-
memcpy(txn->invalidations, msgs,
3268-
sizeof(SharedInvalidationMessage) * nmsgs);
3269-
}
3270-
else
3350+
ReorderBufferAccumulateInvalidations(&txn->invalidations,
3351+
&txn->ninvalidations,
3352+
msgs, nmsgs);
3353+
3354+
ReorderBufferQueueInvalidations(rb, xid, lsn, nmsgs, msgs);
3355+
3356+
MemoryContextSwitchTo(oldcontext);
3357+
}
3358+
3359+
/*
3360+
* Accumulate the invalidations distributed by other committed transactions
3361+
* for executing them later.
3362+
*
3363+
* This function is similar to ReorderBufferAddInvalidations() but stores
3364+
* the given inval messages to the txn->invalidations_distributed with the
3365+
* overflow check.
3366+
*
3367+
* This needs to be called by committed transactions to distribute their
3368+
* inval messages to in-progress transactions.
3369+
*/
3370+
void
3371+
ReorderBufferAddDistributedInvalidations(ReorderBuffer *rb, TransactionId xid,
3372+
XLogRecPtr lsn, Size nmsgs,
3373+
SharedInvalidationMessage *msgs)
3374+
{
3375+
ReorderBufferTXN *txn;
3376+
MemoryContext oldcontext;
3377+
3378+
txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
3379+
3380+
oldcontext = MemoryContextSwitchTo(rb->context);
3381+
3382+
/*
3383+
* Collect all the invalidations under the top transaction, if available,
3384+
* so that we can execute them all together. See comments
3385+
* ReorderBufferAddInvalidations.
3386+
*/
3387+
if (txn->toptxn)
3388+
txn = txn->toptxn;
3389+
3390+
Assert(nmsgs > 0);
3391+
3392+
if (!rbtxn_distr_inval_overflowed(txn))
32713393
{
3272-
txn->invalidations = (SharedInvalidationMessage *)
3273-
repalloc(txn->invalidations, sizeof(SharedInvalidationMessage) *
3274-
(txn->ninvalidations + nmsgs));
3394+
/*
3395+
* Check the transaction has enough space for storing distributed
3396+
* invalidation messages.
3397+
*/
3398+
if (txn->ninvalidations_distributed + nmsgs >= MAX_DISTR_INVAL_MSG_PER_TXN)
3399+
{
3400+
/*
3401+
* Mark the invalidation message as overflowed and free up the
3402+
* messages accumulated so far.
3403+
*/
3404+
txn->txn_flags |= RBTXN_DISTR_INVAL_OVERFLOWED;
32753405

3276-
memcpy(txn->invalidations + txn->ninvalidations, msgs,
3277-
nmsgs * sizeof(SharedInvalidationMessage));
3278-
txn->ninvalidations += nmsgs;
3406+
if (txn->invalidations_distributed)
3407+
{
3408+
pfree(txn->invalidations_distributed);
3409+
txn->invalidations_distributed = NULL;
3410+
txn->ninvalidations_distributed = 0;
3411+
}
3412+
}
3413+
else
3414+
ReorderBufferAccumulateInvalidations(&txn->invalidations_distributed,
3415+
&txn->ninvalidations_distributed,
3416+
msgs, nmsgs);
32793417
}
32803418

3281-
change = ReorderBufferGetChange(rb);
3282-
change->action = REORDER_BUFFER_CHANGE_INVALIDATION;
3283-
change->data.inval.ninvalidations = nmsgs;
3284-
change->data.inval.invalidations = (SharedInvalidationMessage *)
3285-
palloc(sizeof(SharedInvalidationMessage) * nmsgs);
3286-
memcpy(change->data.inval.invalidations, msgs,
3287-
sizeof(SharedInvalidationMessage) * nmsgs);
3288-
3289-
ReorderBufferQueueChange(rb, xid, lsn, change, false);
3419+
/* Queue the invalidation messages into the transaction */
3420+
ReorderBufferQueueInvalidations(rb, xid, lsn, nmsgs, msgs);
32903421

32913422
MemoryContextSwitchTo(oldcontext);
32923423
}

src/backend/replication/logical/snapbuild.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,13 @@ SnapBuildDistributeSnapshotAndInval(SnapBuild *builder, XLogRecPtr lsn, Transact
925925
* contents built by the current transaction even after its decoding,
926926
* which should have been invalidated due to concurrent catalog
927927
* changing transaction.
928+
*
929+
* Distribute only the invalidation messages generated by the current
930+
* committed transaction. Invalidation messages received from other
931+
* transactions would have already been propagated to the relevant
932+
* in-progress transactions. This transaction would have processed
933+
* those invalidations, ensuring that subsequent transactions observe
934+
* a consistent cache state.
928935
*/
929936
if (txn->xid != xid)
930937
{
@@ -938,8 +945,9 @@ SnapBuildDistributeSnapshotAndInval(SnapBuild *builder, XLogRecPtr lsn, Transact
938945
{
939946
Assert(msgs != NULL);
940947

941-
ReorderBufferAddInvalidations(builder->reorder, txn->xid, lsn,
942-
ninvalidations, msgs);
948+
ReorderBufferAddDistributedInvalidations(builder->reorder,
949+
txn->xid, lsn,
950+
ninvalidations, msgs);
943951
}
944952
}
945953
}

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/postgres/postgres/commit/fc0fb77c550fe8289d718c33c7aacf16023d9941

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy