mirror of
https://github.com/postgres/postgres.git
synced 2025-05-02 11:44:50 +03:00
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
This commit is contained in:
parent
d0b193c0fa
commit
ce3049b021
@ -533,65 +533,14 @@ HasSubscriptionRelations(Oid subid)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Get all relations for subscription.
|
* Get the relations for the subscription.
|
||||||
*
|
*
|
||||||
* Returned list is palloc'ed in current memory context.
|
* If not_ready is true, return only the relations that are not in a ready
|
||||||
|
* state, otherwise return all the relations of the subscription. The
|
||||||
|
* returned list is palloc'ed in the current memory context.
|
||||||
*/
|
*/
|
||||||
List *
|
List *
|
||||||
GetSubscriptionRelations(Oid subid)
|
GetSubscriptionRelations(Oid subid, bool not_ready)
|
||||||
{
|
|
||||||
List *res = NIL;
|
|
||||||
Relation rel;
|
|
||||||
HeapTuple tup;
|
|
||||||
ScanKeyData skey[1];
|
|
||||||
SysScanDesc scan;
|
|
||||||
|
|
||||||
rel = table_open(SubscriptionRelRelationId, AccessShareLock);
|
|
||||||
|
|
||||||
ScanKeyInit(&skey[0],
|
|
||||||
Anum_pg_subscription_rel_srsubid,
|
|
||||||
BTEqualStrategyNumber, F_OIDEQ,
|
|
||||||
ObjectIdGetDatum(subid));
|
|
||||||
|
|
||||||
scan = systable_beginscan(rel, InvalidOid, false,
|
|
||||||
NULL, 1, skey);
|
|
||||||
|
|
||||||
while (HeapTupleIsValid(tup = systable_getnext(scan)))
|
|
||||||
{
|
|
||||||
Form_pg_subscription_rel subrel;
|
|
||||||
SubscriptionRelState *relstate;
|
|
||||||
Datum d;
|
|
||||||
bool isnull;
|
|
||||||
|
|
||||||
subrel = (Form_pg_subscription_rel) GETSTRUCT(tup);
|
|
||||||
|
|
||||||
relstate = (SubscriptionRelState *) palloc(sizeof(SubscriptionRelState));
|
|
||||||
relstate->relid = subrel->srrelid;
|
|
||||||
relstate->state = subrel->srsubstate;
|
|
||||||
d = SysCacheGetAttr(SUBSCRIPTIONRELMAP, tup,
|
|
||||||
Anum_pg_subscription_rel_srsublsn, &isnull);
|
|
||||||
if (isnull)
|
|
||||||
relstate->lsn = InvalidXLogRecPtr;
|
|
||||||
else
|
|
||||||
relstate->lsn = DatumGetLSN(d);
|
|
||||||
|
|
||||||
res = lappend(res, relstate);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Cleanup */
|
|
||||||
systable_endscan(scan);
|
|
||||||
table_close(rel, AccessShareLock);
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Get all relations for subscription that are not in a ready state.
|
|
||||||
*
|
|
||||||
* Returned list is palloc'ed in current memory context.
|
|
||||||
*/
|
|
||||||
List *
|
|
||||||
GetSubscriptionNotReadyRelations(Oid subid)
|
|
||||||
{
|
{
|
||||||
List *res = NIL;
|
List *res = NIL;
|
||||||
Relation rel;
|
Relation rel;
|
||||||
@ -607,6 +556,7 @@ GetSubscriptionNotReadyRelations(Oid subid)
|
|||||||
BTEqualStrategyNumber, F_OIDEQ,
|
BTEqualStrategyNumber, F_OIDEQ,
|
||||||
ObjectIdGetDatum(subid));
|
ObjectIdGetDatum(subid));
|
||||||
|
|
||||||
|
if (not_ready)
|
||||||
ScanKeyInit(&skey[nkeys++],
|
ScanKeyInit(&skey[nkeys++],
|
||||||
Anum_pg_subscription_rel_srsubstate,
|
Anum_pg_subscription_rel_srsubstate,
|
||||||
BTEqualStrategyNumber, F_CHARNE,
|
BTEqualStrategyNumber, F_CHARNE,
|
||||||
|
@ -814,7 +814,7 @@ AlterSubscription_refresh(Subscription *sub, bool copy_data,
|
|||||||
pubrel_names = fetch_table_list(wrconn, sub->publications);
|
pubrel_names = fetch_table_list(wrconn, sub->publications);
|
||||||
|
|
||||||
/* Get local table list. */
|
/* Get local table list. */
|
||||||
subrel_states = GetSubscriptionRelations(sub->oid);
|
subrel_states = GetSubscriptionRelations(sub->oid, false);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Build qsorted array of local table oids for faster lookup. This can
|
* Build qsorted array of local table oids for faster lookup. This can
|
||||||
@ -1494,7 +1494,7 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel)
|
|||||||
* the apply and tablesync workers and they can't restart because of
|
* the apply and tablesync workers and they can't restart because of
|
||||||
* exclusive lock on the subscription.
|
* exclusive lock on the subscription.
|
||||||
*/
|
*/
|
||||||
rstates = GetSubscriptionNotReadyRelations(subid);
|
rstates = GetSubscriptionRelations(subid, true);
|
||||||
foreach(lc, rstates)
|
foreach(lc, rstates)
|
||||||
{
|
{
|
||||||
SubscriptionRelState *rstate = (SubscriptionRelState *) lfirst(lc);
|
SubscriptionRelState *rstate = (SubscriptionRelState *) lfirst(lc);
|
||||||
|
@ -1479,7 +1479,7 @@ FetchTableStates(bool *started_tx)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Fetch all non-ready tables. */
|
/* Fetch all non-ready tables. */
|
||||||
rstates = GetSubscriptionNotReadyRelations(MySubscription->oid);
|
rstates = GetSubscriptionRelations(MySubscription->oid, true);
|
||||||
|
|
||||||
/* Allocate the tracking info in a permanent memory context. */
|
/* Allocate the tracking info in a permanent memory context. */
|
||||||
oldctx = MemoryContextSwitchTo(CacheMemoryContext);
|
oldctx = MemoryContextSwitchTo(CacheMemoryContext);
|
||||||
|
@ -88,7 +88,6 @@ extern char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn);
|
|||||||
extern void RemoveSubscriptionRel(Oid subid, Oid relid);
|
extern void RemoveSubscriptionRel(Oid subid, Oid relid);
|
||||||
|
|
||||||
extern bool HasSubscriptionRelations(Oid subid);
|
extern bool HasSubscriptionRelations(Oid subid);
|
||||||
extern List *GetSubscriptionRelations(Oid subid);
|
extern List *GetSubscriptionRelations(Oid subid, bool not_ready);
|
||||||
extern List *GetSubscriptionNotReadyRelations(Oid subid);
|
|
||||||
|
|
||||||
#endif /* PG_SUBSCRIPTION_REL_H */
|
#endif /* PG_SUBSCRIPTION_REL_H */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user