Skip to content

Commit b05fe7b

Browse files
committed
Review logical replication tablesync code
Most importantly, remove optimization in LogicalRepSyncTableStart that skips the normal walrcv_startstreaming/endstreaming dance. The optimization is not critically important for production uses anyway, since it only fires in cases with no activity, and saves an uninteresting amount of work even then. Critically, it obscures bugs by hiding the interesting code path from test cases. Also: in GetSubscriptionRelState, remove pointless relation open; access pg_subscription_rel->srsubstate with GETSTRUCT as is typical rather than SysCacheGetAttr; remove unused 'missing_ok' argument. In wait_for_relation_state_change, use explicit catalog snapshot invalidation rather than obscurely (and expensively) through GetLatestSnapshot. In various places: sprinkle comments more liberally and rewrite a number of them. Other cosmetic code improvements. No backpatch, since no bug is being fixed here. Author: Álvaro Herrera <[email protected]> Reviewed-by: Petr Jelínek <[email protected]> Discussion: https://postgr.es/m/[email protected]
1 parent c5b097f commit b05fe7b

File tree

5 files changed

+131
-178
lines changed

5 files changed

+131
-178
lines changed

src/backend/catalog/pg_subscription.c

+7-20
Original file line numberDiff line numberDiff line change
@@ -328,43 +328,31 @@ UpdateSubscriptionRelState(Oid subid, Oid relid, char state,
328328
/*
329329
* Get state of subscription table.
330330
*
331-
* Returns SUBREL_STATE_UNKNOWN when not found and missing_ok is true.
331+
* Returns SUBREL_STATE_UNKNOWN when the table is not in the subscription.
332332
*/
333333
char
334-
GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn,
335-
bool missing_ok)
334+
GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn)
336335
{
337-
Relation rel;
338336
HeapTuple tup;
339337
char substate;
340338
bool isnull;
341339
Datum d;
342340

343-
rel = table_open(SubscriptionRelRelationId, AccessShareLock);
344-
345341
/* Try finding the mapping. */
346342
tup = SearchSysCache2(SUBSCRIPTIONRELMAP,
347343
ObjectIdGetDatum(relid),
348344
ObjectIdGetDatum(subid));
349345

350346
if (!HeapTupleIsValid(tup))
351347
{
352-
if (missing_ok)
353-
{
354-
table_close(rel, AccessShareLock);
355-
*sublsn = InvalidXLogRecPtr;
356-
return SUBREL_STATE_UNKNOWN;
357-
}
358-
359-
elog(ERROR, "subscription table %u in subscription %u does not exist",
360-
relid, subid);
348+
*sublsn = InvalidXLogRecPtr;
349+
return SUBREL_STATE_UNKNOWN;
361350
}
362351

363352
/* Get the state. */
364-
d = SysCacheGetAttr(SUBSCRIPTIONRELMAP, tup,
365-
Anum_pg_subscription_rel_srsubstate, &isnull);
366-
Assert(!isnull);
367-
substate = DatumGetChar(d);
353+
substate = ((Form_pg_subscription_rel) GETSTRUCT(tup))->srsubstate;
354+
355+
/* Get the LSN */
368356
d = SysCacheGetAttr(SUBSCRIPTIONRELMAP, tup,
369357
Anum_pg_subscription_rel_srsublsn, &isnull);
370358
if (isnull)
@@ -374,7 +362,6 @@ GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn,
374362

375363
/* Cleanup */
376364
ReleaseSysCache(tup);
377-
table_close(rel, AccessShareLock);
378365

379366
return substate;
380367
}

src/backend/replication/logical/relation.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,7 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
437437
if (entry->state != SUBREL_STATE_READY)
438438
entry->state = GetSubscriptionRelState(MySubscription->oid,
439439
entry->localreloid,
440-
&entry->statelsn,
441-
true);
440+
&entry->statelsn);
442441

443442
return entry;
444443
}

0 commit comments

Comments
 (0)