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

Skip to content

Commit ce3049b

Browse files
committed
Refactor code in charge of grabbing the relations of a subscription
GetSubscriptionRelations() and GetSubscriptionNotReadyRelations() share mostly the same code, which scans pg_subscription_rel and fetches all the relations of a given subscription. The only difference is that the second routine looks for all the relations not in a ready state. This commit refactors the code to use a single routine, shaving a bit of code. Author: Vignesh C Reviewed-By: Kyotaro Horiguchi, Amit Kapila, Michael Paquier, Peter Smith Discussion: https://postgr.es/m/CALDaNm0eW-9g4G_EzHebnFT5zZoasWCS_EzZQ5BgnLZny9S=pg@mail.gmail.com
1 parent d0b193c commit ce3049b

File tree

4 files changed

+14
-65
lines changed

4 files changed

+14
-65
lines changed

src/backend/catalog/pg_subscription.c

+10-60
Original file line numberDiff line numberDiff line change
@@ -533,65 +533,14 @@ HasSubscriptionRelations(Oid subid)
533533
}
534534

535535
/*
536-
* Get all relations for subscription.
536+
* Get the relations for the subscription.
537537
*
538-
* Returned list is palloc'ed in current memory context.
538+
* If not_ready is true, return only the relations that are not in a ready
539+
* state, otherwise return all the relations of the subscription. The
540+
* returned list is palloc'ed in the current memory context.
539541
*/
540542
List *
541-
GetSubscriptionRelations(Oid subid)
542-
{
543-
List *res = NIL;
544-
Relation rel;
545-
HeapTuple tup;
546-
ScanKeyData skey[1];
547-
SysScanDesc scan;
548-
549-
rel = table_open(SubscriptionRelRelationId, AccessShareLock);
550-
551-
ScanKeyInit(&skey[0],
552-
Anum_pg_subscription_rel_srsubid,
553-
BTEqualStrategyNumber, F_OIDEQ,
554-
ObjectIdGetDatum(subid));
555-
556-
scan = systable_beginscan(rel, InvalidOid, false,
557-
NULL, 1, skey);
558-
559-
while (HeapTupleIsValid(tup = systable_getnext(scan)))
560-
{
561-
Form_pg_subscription_rel subrel;
562-
SubscriptionRelState *relstate;
563-
Datum d;
564-
bool isnull;
565-
566-
subrel = (Form_pg_subscription_rel) GETSTRUCT(tup);
567-
568-
relstate = (SubscriptionRelState *) palloc(sizeof(SubscriptionRelState));
569-
relstate->relid = subrel->srrelid;
570-
relstate->state = subrel->srsubstate;
571-
d = SysCacheGetAttr(SUBSCRIPTIONRELMAP, tup,
572-
Anum_pg_subscription_rel_srsublsn, &isnull);
573-
if (isnull)
574-
relstate->lsn = InvalidXLogRecPtr;
575-
else
576-
relstate->lsn = DatumGetLSN(d);
577-
578-
res = lappend(res, relstate);
579-
}
580-
581-
/* Cleanup */
582-
systable_endscan(scan);
583-
table_close(rel, AccessShareLock);
584-
585-
return res;
586-
}
587-
588-
/*
589-
* Get all relations for subscription that are not in a ready state.
590-
*
591-
* Returned list is palloc'ed in current memory context.
592-
*/
593-
List *
594-
GetSubscriptionNotReadyRelations(Oid subid)
543+
GetSubscriptionRelations(Oid subid, bool not_ready)
595544
{
596545
List *res = NIL;
597546
Relation rel;
@@ -607,10 +556,11 @@ GetSubscriptionNotReadyRelations(Oid subid)
607556
BTEqualStrategyNumber, F_OIDEQ,
608557
ObjectIdGetDatum(subid));
609558

610-
ScanKeyInit(&skey[nkeys++],
611-
Anum_pg_subscription_rel_srsubstate,
612-
BTEqualStrategyNumber, F_CHARNE,
613-
CharGetDatum(SUBREL_STATE_READY));
559+
if (not_ready)
560+
ScanKeyInit(&skey[nkeys++],
561+
Anum_pg_subscription_rel_srsubstate,
562+
BTEqualStrategyNumber, F_CHARNE,
563+
CharGetDatum(SUBREL_STATE_READY));
614564

615565
scan = systable_beginscan(rel, InvalidOid, false,
616566
NULL, nkeys, skey);

src/backend/commands/subscriptioncmds.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ AlterSubscription_refresh(Subscription *sub, bool copy_data,
814814
pubrel_names = fetch_table_list(wrconn, sub->publications);
815815

816816
/* Get local table list. */
817-
subrel_states = GetSubscriptionRelations(sub->oid);
817+
subrel_states = GetSubscriptionRelations(sub->oid, false);
818818

819819
/*
820820
* Build qsorted array of local table oids for faster lookup. This can
@@ -1494,7 +1494,7 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel)
14941494
* the apply and tablesync workers and they can't restart because of
14951495
* exclusive lock on the subscription.
14961496
*/
1497-
rstates = GetSubscriptionNotReadyRelations(subid);
1497+
rstates = GetSubscriptionRelations(subid, true);
14981498
foreach(lc, rstates)
14991499
{
15001500
SubscriptionRelState *rstate = (SubscriptionRelState *) lfirst(lc);

src/backend/replication/logical/tablesync.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1479,7 +1479,7 @@ FetchTableStates(bool *started_tx)
14791479
}
14801480

14811481
/* Fetch all non-ready tables. */
1482-
rstates = GetSubscriptionNotReadyRelations(MySubscription->oid);
1482+
rstates = GetSubscriptionRelations(MySubscription->oid, true);
14831483

14841484
/* Allocate the tracking info in a permanent memory context. */
14851485
oldctx = MemoryContextSwitchTo(CacheMemoryContext);

src/include/catalog/pg_subscription_rel.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ extern char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn);
8888
extern void RemoveSubscriptionRel(Oid subid, Oid relid);
8989

9090
extern bool HasSubscriptionRelations(Oid subid);
91-
extern List *GetSubscriptionRelations(Oid subid);
92-
extern List *GetSubscriptionNotReadyRelations(Oid subid);
91+
extern List *GetSubscriptionRelations(Oid subid, bool not_ready);
9392

9493
#endif /* PG_SUBSCRIPTION_REL_H */

0 commit comments

Comments
 (0)