Skip to content

Commit a9155ef

Browse files
author
Amit Kapila
committed
Fix the missing table sync due to improper invalidation handling.
We missed performing table sync if the invalidation happened while the non-ready tables list was being prepared. This occurs because the sync state was set to valid at the end of non-ready table list preparation irrespective of the invalidations processed while the list is being prepared. Fix it by changing the boolean variable to a tri-state enum and by setting table state to valid only if no invalidations have occurred while the list is being prepared. Reprted-by: Alexander Lakhin Diagnosed-by: Alexander Lakhin Author: Vignesh C Reviewed-by: Hou Zhijie, Alexander Lakhin, Ajin Cherian, Amit Kapila Backpatch-through: 15 Discussion: https://postgr.es/m/711a6afe-edb7-1211-cc27-1bef8239eec7@gmail.com
1 parent cd0678a commit a9155ef

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/backend/replication/logical/tablesync.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,14 @@
122122
#include "utils/syscache.h"
123123
#include "utils/usercontext.h"
124124

125-
static bool table_states_valid = false;
125+
typedef enum
126+
{
127+
SYNC_TABLE_STATE_NEEDS_REBUILD,
128+
SYNC_TABLE_STATE_REBUILD_STARTED,
129+
SYNC_TABLE_STATE_VALID,
130+
} SyncingTablesState;
131+
132+
static SyncingTablesState table_states_validity = SYNC_TABLE_STATE_NEEDS_REBUILD;
126133
static List *table_states_not_ready = NIL;
127134
static bool FetchTableStates(bool *started_tx);
128135

@@ -272,7 +279,7 @@ wait_for_worker_state_change(char expected_state)
272279
void
273280
invalidate_syncing_table_states(Datum arg, int cacheid, uint32 hashvalue)
274281
{
275-
table_states_valid = false;
282+
table_states_validity = SYNC_TABLE_STATE_NEEDS_REBUILD;
276283
}
277284

278285
/*
@@ -1556,13 +1563,15 @@ FetchTableStates(bool *started_tx)
15561563

15571564
*started_tx = false;
15581565

1559-
if (!table_states_valid)
1566+
if (table_states_validity != SYNC_TABLE_STATE_VALID)
15601567
{
15611568
MemoryContext oldctx;
15621569
List *rstates;
15631570
ListCell *lc;
15641571
SubscriptionRelState *rstate;
15651572

1573+
table_states_validity = SYNC_TABLE_STATE_REBUILD_STARTED;
1574+
15661575
/* Clean the old lists. */
15671576
list_free_deep(table_states_not_ready);
15681577
table_states_not_ready = NIL;
@@ -1596,7 +1605,15 @@ FetchTableStates(bool *started_tx)
15961605
has_subrels = (table_states_not_ready != NIL) ||
15971606
HasSubscriptionRelations(MySubscription->oid);
15981607

1599-
table_states_valid = true;
1608+
/*
1609+
* If the subscription relation cache has been invalidated since we
1610+
* entered this routine, we still use and return the relations we just
1611+
* finished constructing, to avoid infinite loops, but we leave the
1612+
* table states marked as stale so that we'll rebuild it again on next
1613+
* access. Otherwise, we mark the table states as valid.
1614+
*/
1615+
if (table_states_validity == SYNC_TABLE_STATE_REBUILD_STARTED)
1616+
table_states_validity = SYNC_TABLE_STATE_VALID;
16001617
}
16011618

16021619
return has_subrels;

src/tools/pgindent/typedefs.list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2676,6 +2676,7 @@ SupportRequestSelectivity
26762676
SupportRequestSimplify
26772677
SupportRequestWFuncMonotonic
26782678
Syn
2679+
SyncingTablesState
26792680
SyncOps
26802681
SyncRepConfigData
26812682
SyncRepStandbyData

0 commit comments

Comments
 (0)