Refactor to make use of a common function for GetSubscriptionRelations and GetSubscriptionNotReadyRelations.
Hi,
Most of the code is common between GetSubscriptionRelations and
GetSubscriptionNotReadyRelations. Added a parameter to
GetSubscriptionRelations which could provide the same functionality as
the existing GetSubscriptionRelations and
GetSubscriptionNotReadyRelations. Attached patch has the changes for
the same. Thoughts?
Regards,
Vignesh
Attachments:
v1-0001-Refactor-to-make-use-of-a-common-function-for-Get.patchtext/x-patch; charset=US-ASCII; name=v1-0001-Refactor-to-make-use-of-a-common-function-for-Get.patchDownload
From 5fb15291abb489cb4469f43afa95106e421002c0 Mon Sep 17 00:00:00 2001
From: Vigneshwaran C <vignesh21@gmail.com>
Date: Wed, 13 Jul 2022 11:54:59 +0530
Subject: [PATCH v1] Refactor to make use of a common function for
GetSubscriptionRelations and GetSubscriptionNotReadyRelations.
Most of the code is common between GetSubscriptionRelations and
GetSubscriptionNotReadyRelations. Added a parameter to GetSubscriptionRelations
which could provide the same functionality for GetSubscriptionRelations
and GetSubscriptionNotReadyRelations.
---
src/backend/catalog/pg_subscription.c | 68 +++------------------
src/backend/commands/subscriptioncmds.c | 4 +-
src/backend/replication/logical/tablesync.c | 2 +-
src/include/catalog/pg_subscription_rel.h | 3 +-
4 files changed, 13 insertions(+), 64 deletions(-)
diff --git a/src/backend/catalog/pg_subscription.c b/src/backend/catalog/pg_subscription.c
index 8856ce3b50..5757e2b338 100644
--- a/src/backend/catalog/pg_subscription.c
+++ b/src/backend/catalog/pg_subscription.c
@@ -525,65 +525,14 @@ HasSubscriptionRelations(Oid subid)
}
/*
- * Get all relations for subscription.
+ * Get the relations for subscription.
*
+ * If all_rels is true, return all the relations for subscription. If false,
+ * return all the relations for subscription that are not in a ready state.
* Returned list is palloc'ed in current memory context.
*/
List *
-GetSubscriptionRelations(Oid subid)
-{
- 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)
+GetSubscriptionRelations(Oid subid, bool all_rels)
{
List *res = NIL;
Relation rel;
@@ -599,10 +548,11 @@ GetSubscriptionNotReadyRelations(Oid subid)
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(subid));
- ScanKeyInit(&skey[nkeys++],
- Anum_pg_subscription_rel_srsubstate,
- BTEqualStrategyNumber, F_CHARNE,
- CharGetDatum(SUBREL_STATE_READY));
+ if (!all_rels)
+ ScanKeyInit(&skey[nkeys++],
+ Anum_pg_subscription_rel_srsubstate,
+ BTEqualStrategyNumber, F_CHARNE,
+ CharGetDatum(SUBREL_STATE_READY));
scan = systable_beginscan(rel, InvalidOid, false,
NULL, nkeys, skey);
diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c
index bdc1208724..f9563304af 100644
--- a/src/backend/commands/subscriptioncmds.c
+++ b/src/backend/commands/subscriptioncmds.c
@@ -785,7 +785,7 @@ AlterSubscription_refresh(Subscription *sub, bool copy_data,
pubrel_names = fetch_table_list(wrconn, sub->publications);
/* Get local table list. */
- subrel_states = GetSubscriptionRelations(sub->oid);
+ subrel_states = GetSubscriptionRelations(sub->oid, true);
/*
* Build qsorted array of local table oids for faster lookup. This can
@@ -1457,7 +1457,7 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel)
* the apply and tablesync workers and they can't restart because of
* exclusive lock on the subscription.
*/
- rstates = GetSubscriptionNotReadyRelations(subid);
+ rstates = GetSubscriptionRelations(subid, false);
foreach(lc, rstates)
{
SubscriptionRelState *rstate = (SubscriptionRelState *) lfirst(lc);
diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c
index 670c6fcada..4b5b388f6c 100644
--- a/src/backend/replication/logical/tablesync.c
+++ b/src/backend/replication/logical/tablesync.c
@@ -1479,7 +1479,7 @@ FetchTableStates(bool *started_tx)
}
/* Fetch all non-ready tables. */
- rstates = GetSubscriptionNotReadyRelations(MySubscription->oid);
+ rstates = GetSubscriptionRelations(MySubscription->oid, false);
/* Allocate the tracking info in a permanent memory context. */
oldctx = MemoryContextSwitchTo(CacheMemoryContext);
diff --git a/src/include/catalog/pg_subscription_rel.h b/src/include/catalog/pg_subscription_rel.h
index 9df99c3418..98bd11b40f 100644
--- a/src/include/catalog/pg_subscription_rel.h
+++ b/src/include/catalog/pg_subscription_rel.h
@@ -88,7 +88,6 @@ extern char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn);
extern void RemoveSubscriptionRel(Oid subid, Oid relid);
extern bool HasSubscriptionRelations(Oid subid);
-extern List *GetSubscriptionRelations(Oid subid);
-extern List *GetSubscriptionNotReadyRelations(Oid subid);
+extern List *GetSubscriptionRelations(Oid subid, bool all_rels);
#endif /* PG_SUBSCRIPTION_REL_H */
--
2.32.0
On Wed, Jul 13, 2022 at 12:22:06PM +0530, vignesh C wrote:
Most of the code is common between GetSubscriptionRelations and
GetSubscriptionNotReadyRelations. Added a parameter to
GetSubscriptionRelations which could provide the same functionality as
the existing GetSubscriptionRelations and
GetSubscriptionNotReadyRelations. Attached patch has the changes for
the same. Thoughts?
Right. Using all_rels to mean that we'd filter relations that are not
ready is a bit confusing, though. Perhaps this could use a bitmask as
argument.
--
Michael
On Wed, Jul 13, 2022 at 5:43 PM Michael Paquier <michael@paquier.xyz> wrote:
On Wed, Jul 13, 2022 at 12:22:06PM +0530, vignesh C wrote:
Most of the code is common between GetSubscriptionRelations and
GetSubscriptionNotReadyRelations. Added a parameter to
GetSubscriptionRelations which could provide the same functionality as
the existing GetSubscriptionRelations and
GetSubscriptionNotReadyRelations. Attached patch has the changes for
the same. Thoughts?Right. Using all_rels to mean that we'd filter relations that are not
ready is a bit confusing, though. Perhaps this could use a bitmask as
argument.
+1
(or some enum)
------
Kind Regards,
Peter Smith.
Fujitsu Australia.
On Wed, Jul 13, 2022 at 1:13 PM Michael Paquier <michael@paquier.xyz> wrote:
On Wed, Jul 13, 2022 at 12:22:06PM +0530, vignesh C wrote:
Most of the code is common between GetSubscriptionRelations and
GetSubscriptionNotReadyRelations. Added a parameter to
GetSubscriptionRelations which could provide the same functionality as
the existing GetSubscriptionRelations and
GetSubscriptionNotReadyRelations. Attached patch has the changes for
the same. Thoughts?Right. Using all_rels to mean that we'd filter relations that are not
ready is a bit confusing, though. Perhaps this could use a bitmask as
argument.
The attached v2 patch has the modified version which includes the
changes to make the argument as bitmask.
Regards,
Vignesh
Attachments:
v2-0001-Refactor-to-make-use-of-a-common-function-for-Get.patchtext/x-patch; charset=US-ASCII; name=v2-0001-Refactor-to-make-use-of-a-common-function-for-Get.patchDownload
From 1f67e4b5ff2d304323f262e8acf74d48893ceb06 Mon Sep 17 00:00:00 2001
From: Vigneshwaran C <vignesh21@gmail.com>
Date: Wed, 13 Jul 2022 11:54:59 +0530
Subject: [PATCH v2] Refactor to make use of a common function for
GetSubscriptionRelations and GetSubscriptionNotReadyRelations.
Most of the code is common between GetSubscriptionRelations and
GetSubscriptionNotReadyRelations. Added a parameter to GetSubscriptionRelations
which could provide the same functionality for GetSubscriptionRelations
and GetSubscriptionNotReadyRelations.
---
src/backend/catalog/pg_subscription.c | 71 ++++-----------------
src/backend/commands/subscriptioncmds.c | 5 +-
src/backend/replication/logical/tablesync.c | 3 +-
src/include/catalog/pg_subscription.h | 3 +
src/include/catalog/pg_subscription_rel.h | 3 +-
5 files changed, 20 insertions(+), 65 deletions(-)
diff --git a/src/backend/catalog/pg_subscription.c b/src/backend/catalog/pg_subscription.c
index 8856ce3b50..cd7dc00c79 100644
--- a/src/backend/catalog/pg_subscription.c
+++ b/src/backend/catalog/pg_subscription.c
@@ -525,65 +525,15 @@ HasSubscriptionRelations(Oid subid)
}
/*
- * Get all relations for subscription.
+ * Get the relations for subscription.
*
- * Returned list is palloc'ed in current memory context.
+ * If subrel_options is not set, return all the relations for subscription. If
+ * SUBSCRIPTION_REL_STATE_NOT_READY bit is set in subrel_options, return all
+ * the relations for subscription that are not in a ready state. Returned list
+ * is palloc'ed in current memory context.
*/
List *
-GetSubscriptionRelations(Oid subid)
-{
- 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)
+GetSubscriptionRelations(Oid subid, bits32 subrel_state_options)
{
List *res = NIL;
Relation rel;
@@ -599,10 +549,11 @@ GetSubscriptionNotReadyRelations(Oid subid)
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(subid));
- ScanKeyInit(&skey[nkeys++],
- Anum_pg_subscription_rel_srsubstate,
- BTEqualStrategyNumber, F_CHARNE,
- CharGetDatum(SUBREL_STATE_READY));
+ if ((subrel_state_options & SUBSCRIPTION_REL_STATE_NOT_READY) != 0)
+ ScanKeyInit(&skey[nkeys++],
+ Anum_pg_subscription_rel_srsubstate,
+ BTEqualStrategyNumber, F_CHARNE,
+ CharGetDatum(SUBREL_STATE_READY));
scan = systable_beginscan(rel, InvalidOid, false,
NULL, nkeys, skey);
diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c
index bdc1208724..d9fb235ce5 100644
--- a/src/backend/commands/subscriptioncmds.c
+++ b/src/backend/commands/subscriptioncmds.c
@@ -785,7 +785,7 @@ AlterSubscription_refresh(Subscription *sub, bool copy_data,
pubrel_names = fetch_table_list(wrconn, sub->publications);
/* Get local table list. */
- subrel_states = GetSubscriptionRelations(sub->oid);
+ subrel_states = GetSubscriptionRelations(sub->oid, 0);
/*
* Build qsorted array of local table oids for faster lookup. This can
@@ -1457,7 +1457,8 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel)
* the apply and tablesync workers and they can't restart because of
* exclusive lock on the subscription.
*/
- rstates = GetSubscriptionNotReadyRelations(subid);
+ rstates = GetSubscriptionRelations(subid,
+ SUBSCRIPTION_REL_STATE_NOT_READY);
foreach(lc, rstates)
{
SubscriptionRelState *rstate = (SubscriptionRelState *) lfirst(lc);
diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c
index 670c6fcada..d0bb6382c0 100644
--- a/src/backend/replication/logical/tablesync.c
+++ b/src/backend/replication/logical/tablesync.c
@@ -1479,7 +1479,8 @@ FetchTableStates(bool *started_tx)
}
/* Fetch all non-ready tables. */
- rstates = GetSubscriptionNotReadyRelations(MySubscription->oid);
+ rstates = GetSubscriptionRelations(MySubscription->oid,
+ SUBSCRIPTION_REL_STATE_NOT_READY);
/* Allocate the tracking info in a permanent memory context. */
oldctx = MemoryContextSwitchTo(CacheMemoryContext);
diff --git a/src/include/catalog/pg_subscription.h b/src/include/catalog/pg_subscription.h
index d1260f590c..5ff428bcba 100644
--- a/src/include/catalog/pg_subscription.h
+++ b/src/include/catalog/pg_subscription.h
@@ -31,6 +31,9 @@
#define LOGICALREP_TWOPHASE_STATE_PENDING 'p'
#define LOGICALREP_TWOPHASE_STATE_ENABLED 'e'
+/* flag bits for getting the relevant subscription relations */
+#define SUBSCRIPTION_REL_STATE_NOT_READY 0x01 /* NOT READY relations */
+
/* ----------------
* pg_subscription definition. cpp turns this into
* typedef struct FormData_pg_subscription
diff --git a/src/include/catalog/pg_subscription_rel.h b/src/include/catalog/pg_subscription_rel.h
index 9df99c3418..8f6c602860 100644
--- a/src/include/catalog/pg_subscription_rel.h
+++ b/src/include/catalog/pg_subscription_rel.h
@@ -88,7 +88,6 @@ extern char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn);
extern void RemoveSubscriptionRel(Oid subid, Oid relid);
extern bool HasSubscriptionRelations(Oid subid);
-extern List *GetSubscriptionRelations(Oid subid);
-extern List *GetSubscriptionNotReadyRelations(Oid subid);
+extern List *GetSubscriptionRelations(Oid subid, bits32 subrel_state_options);
#endif /* PG_SUBSCRIPTION_REL_H */
--
2.32.0
On Wed, Jul 13, 2022 at 7:55 PM vignesh C <vignesh21@gmail.com> wrote:
On Wed, Jul 13, 2022 at 1:13 PM Michael Paquier <michael@paquier.xyz> wrote:
On Wed, Jul 13, 2022 at 12:22:06PM +0530, vignesh C wrote:
Most of the code is common between GetSubscriptionRelations and
GetSubscriptionNotReadyRelations. Added a parameter to
GetSubscriptionRelations which could provide the same functionality as
the existing GetSubscriptionRelations and
GetSubscriptionNotReadyRelations. Attached patch has the changes for
the same. Thoughts?Right. Using all_rels to mean that we'd filter relations that are not
ready is a bit confusing, though. Perhaps this could use a bitmask as
argument.The attached v2 patch has the modified version which includes the
changes to make the argument as bitmask.
By using a bitmask I think there is an implication that the flags can
be combined...
Perhaps it is not a problem today, but later you may want more flags. e.g.
#define SUBSCRIPTION_REL_STATE_READY 0x02 /* READY relations */
then the bitmask idea falls apart because IIUC you have no intentions
to permit things like:
(SUBSCRIPTION_REL_STATE_NOT_READY | SUBSCRIPTION_REL_STATE_READY)
IMO using an enum might be a better choice for that parameter.
------
Kind Regards,
Peter Smith.
Fujitsu Australia
On Thu, Jul 14, 2022 at 4:34 AM Peter Smith <smithpb2250@gmail.com> wrote:
On Wed, Jul 13, 2022 at 7:55 PM vignesh C <vignesh21@gmail.com> wrote:
On Wed, Jul 13, 2022 at 1:13 PM Michael Paquier <michael@paquier.xyz> wrote:
On Wed, Jul 13, 2022 at 12:22:06PM +0530, vignesh C wrote:
Most of the code is common between GetSubscriptionRelations and
GetSubscriptionNotReadyRelations. Added a parameter to
GetSubscriptionRelations which could provide the same functionality as
the existing GetSubscriptionRelations and
GetSubscriptionNotReadyRelations. Attached patch has the changes for
the same. Thoughts?Right. Using all_rels to mean that we'd filter relations that are not
ready is a bit confusing, though. Perhaps this could use a bitmask as
argument.The attached v2 patch has the modified version which includes the
changes to make the argument as bitmask.By using a bitmask I think there is an implication that the flags can
be combined...Perhaps it is not a problem today, but later you may want more flags. e.g.
#define SUBSCRIPTION_REL_STATE_READY 0x02 /* READY relations */then the bitmask idea falls apart because IIUC you have no intentions
to permit things like:
(SUBSCRIPTION_REL_STATE_NOT_READY | SUBSCRIPTION_REL_STATE_READY)IMO using an enum might be a better choice for that parameter.
Changed it to enum so that it can be extended to support other
subscription relations like ready state subscription relations later
easily. The attached v3 patch has the changes for the same.
Regards,
Vignesh
Attachments:
v3-0001-Refactor-to-make-use-of-a-common-function-for-Get.patchtext/x-patch; charset=US-ASCII; name=v3-0001-Refactor-to-make-use-of-a-common-function-for-Get.patchDownload
From e0186ae685acb6334b711ea821b287be61d6cbd3 Mon Sep 17 00:00:00 2001
From: Vigneshwaran C <vignesh21@gmail.com>
Date: Wed, 13 Jul 2022 11:54:59 +0530
Subject: [PATCH v3] Refactor to make use of a common function for
GetSubscriptionRelations and GetSubscriptionNotReadyRelations.
Most of the code is common between GetSubscriptionRelations and
GetSubscriptionNotReadyRelations. Added a parameter to GetSubscriptionRelations
which could provide the same functionality for GetSubscriptionRelations
and GetSubscriptionNotReadyRelations.
---
src/backend/catalog/pg_subscription.c | 71 ++++-----------------
src/backend/commands/subscriptioncmds.c | 5 +-
src/backend/replication/logical/tablesync.c | 3 +-
src/include/catalog/pg_subscription_rel.h | 19 +++++-
src/tools/pgindent/typedefs.list | 1 +
5 files changed, 34 insertions(+), 65 deletions(-)
diff --git a/src/backend/catalog/pg_subscription.c b/src/backend/catalog/pg_subscription.c
index 8856ce3b50..46eedbc062 100644
--- a/src/backend/catalog/pg_subscription.c
+++ b/src/backend/catalog/pg_subscription.c
@@ -525,65 +525,15 @@ HasSubscriptionRelations(Oid subid)
}
/*
- * Get all relations for subscription.
+ * Get the relations for subscription.
*
- * Returned list is palloc'ed in current memory context.
+ * If subrel_options is SUBSCRIPTION_REL_ALL, return all the relations for
+ * subscription. If SUBSCRIPTION_REL_NOT_READY, return all the relations for
+ * subscription that are not in a ready state. Returned list is palloc'ed in
+ * current memory context.
*/
List *
-GetSubscriptionRelations(Oid subid)
-{
- 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)
+GetSubscriptionRelations(Oid subid, SubscriptionRelationState subrel_state)
{
List *res = NIL;
Relation rel;
@@ -599,10 +549,11 @@ GetSubscriptionNotReadyRelations(Oid subid)
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(subid));
- ScanKeyInit(&skey[nkeys++],
- Anum_pg_subscription_rel_srsubstate,
- BTEqualStrategyNumber, F_CHARNE,
- CharGetDatum(SUBREL_STATE_READY));
+ if (subrel_state == SUBSCRIPTION_REL_NOT_READY)
+ ScanKeyInit(&skey[nkeys++],
+ Anum_pg_subscription_rel_srsubstate,
+ BTEqualStrategyNumber, F_CHARNE,
+ CharGetDatum(SUBREL_STATE_READY));
scan = systable_beginscan(rel, InvalidOid, false,
NULL, nkeys, skey);
diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c
index bdc1208724..ac517dd539 100644
--- a/src/backend/commands/subscriptioncmds.c
+++ b/src/backend/commands/subscriptioncmds.c
@@ -785,7 +785,8 @@ AlterSubscription_refresh(Subscription *sub, bool copy_data,
pubrel_names = fetch_table_list(wrconn, sub->publications);
/* Get local table list. */
- subrel_states = GetSubscriptionRelations(sub->oid);
+ subrel_states = GetSubscriptionRelations(sub->oid,
+ SUBSCRIPTION_REL_ALL);
/*
* Build qsorted array of local table oids for faster lookup. This can
@@ -1457,7 +1458,7 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel)
* the apply and tablesync workers and they can't restart because of
* exclusive lock on the subscription.
*/
- rstates = GetSubscriptionNotReadyRelations(subid);
+ rstates = GetSubscriptionRelations(subid, SUBSCRIPTION_REL_NOT_READY);
foreach(lc, rstates)
{
SubscriptionRelState *rstate = (SubscriptionRelState *) lfirst(lc);
diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c
index 670c6fcada..b072d9c349 100644
--- a/src/backend/replication/logical/tablesync.c
+++ b/src/backend/replication/logical/tablesync.c
@@ -1479,7 +1479,8 @@ FetchTableStates(bool *started_tx)
}
/* Fetch all non-ready tables. */
- rstates = GetSubscriptionNotReadyRelations(MySubscription->oid);
+ rstates = GetSubscriptionRelations(MySubscription->oid,
+ SUBSCRIPTION_REL_NOT_READY);
/* Allocate the tracking info in a permanent memory context. */
oldctx = MemoryContextSwitchTo(CacheMemoryContext);
diff --git a/src/include/catalog/pg_subscription_rel.h b/src/include/catalog/pg_subscription_rel.h
index 9df99c3418..a4a45997b6 100644
--- a/src/include/catalog/pg_subscription_rel.h
+++ b/src/include/catalog/pg_subscription_rel.h
@@ -80,6 +80,21 @@ typedef struct SubscriptionRelState
char state;
} SubscriptionRelState;
+/*---------
+ * Expected values for subrel_state parameter of GetSubscriptionRelations(),
+ * which allows callers to specify which relations of the subscriptions they
+ * expect to see.
+ *
+ * SUBSCRIPTION_REL_ALL: all the subscription relations
+ * SUBSCRIPTION_REL_NOT_READY: all the subscription relations that are not
+ * in ready state.
+ */
+typedef enum SubscriptionRelationState
+{
+ SUBSCRIPTION_REL_ALL,
+ SUBSCRIPTION_REL_NOT_READY
+} SubscriptionRelationState;
+
extern void AddSubscriptionRelState(Oid subid, Oid relid, char state,
XLogRecPtr sublsn);
extern void UpdateSubscriptionRelState(Oid subid, Oid relid, char state,
@@ -88,7 +103,7 @@ extern char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn);
extern void RemoveSubscriptionRel(Oid subid, Oid relid);
extern bool HasSubscriptionRelations(Oid subid);
-extern List *GetSubscriptionRelations(Oid subid);
-extern List *GetSubscriptionNotReadyRelations(Oid subid);
+extern List *GetSubscriptionRelations(Oid subid,
+ SubscriptionRelationState subrel_state);
#endif /* PG_SUBSCRIPTION_REL_H */
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 34a76ceb60..d2ff829735 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2641,6 +2641,7 @@ SubscriptingRefState
Subscription
SubscriptionInfo
SubscriptionRelState
+SubscriptionRelationState
SupportRequestCost
SupportRequestIndexCondition
SupportRequestRows
--
2.32.0
On Thu, Jul 14, 2022 at 4:34 AM Peter Smith <smithpb2250@gmail.com> wrote:
On Wed, Jul 13, 2022 at 7:55 PM vignesh C <vignesh21@gmail.com> wrote:
On Wed, Jul 13, 2022 at 1:13 PM Michael Paquier <michael@paquier.xyz> wrote:
On Wed, Jul 13, 2022 at 12:22:06PM +0530, vignesh C wrote:
Most of the code is common between GetSubscriptionRelations and
GetSubscriptionNotReadyRelations. Added a parameter to
GetSubscriptionRelations which could provide the same functionality as
the existing GetSubscriptionRelations and
GetSubscriptionNotReadyRelations. Attached patch has the changes for
the same. Thoughts?Right. Using all_rels to mean that we'd filter relations that are not
ready is a bit confusing, though. Perhaps this could use a bitmask as
argument.The attached v2 patch has the modified version which includes the
changes to make the argument as bitmask.By using a bitmask I think there is an implication that the flags can
be combined...Perhaps it is not a problem today, but later you may want more flags. e.g.
#define SUBSCRIPTION_REL_STATE_READY 0x02 /* READY relations */then the bitmask idea falls apart because IIUC you have no intentions
to permit things like:
(SUBSCRIPTION_REL_STATE_NOT_READY | SUBSCRIPTION_REL_STATE_READY)
I think this will be an invalid combination if caller ever used it.
However, one might need to use a combination like
(SUBSCRIPTION_REL_STATE_READY | SUBSCRIPTION_REL_STATE_DONE). For such
cases, I feel the bitmask idea will be better.
--
With Regards,
Amit Kapila.
On Wed, Jul 20, 2022 at 02:32:44PM +0530, Amit Kapila wrote:
On Thu, Jul 14, 2022 at 4:34 AM Peter Smith <smithpb2250@gmail.com> wrote:
By using a bitmask I think there is an implication that the flags can
be combined...Perhaps it is not a problem today, but later you may want more flags. e.g.
#define SUBSCRIPTION_REL_STATE_READY 0x02 /* READY relations */then the bitmask idea falls apart because IIUC you have no intentions
to permit things like:
(SUBSCRIPTION_REL_STATE_NOT_READY | SUBSCRIPTION_REL_STATE_READY)I think this will be an invalid combination if caller ever used it.
However, one might need to use a combination like
(SUBSCRIPTION_REL_STATE_READY | SUBSCRIPTION_REL_STATE_DONE). For such
cases, I feel the bitmask idea will be better.
It feels unnatural to me to have a flag saying "not-ready" and one
saying "ready", while we could have a flag saying "ready" that can be
combined with a second flag to decide if the contents of srsubstate
should be matched or *not* matched with the states expected by the
caller. This could be extended to more state values, for example.
I am not sure if we actually need this much as I have no idea if
future features would use it, so please take my suggestion lightly :)
--
Michael
On Thu, Jul 21, 2022 at 7:10 AM Michael Paquier <michael@paquier.xyz> wrote:
On Wed, Jul 20, 2022 at 02:32:44PM +0530, Amit Kapila wrote:
On Thu, Jul 14, 2022 at 4:34 AM Peter Smith <smithpb2250@gmail.com> wrote:
By using a bitmask I think there is an implication that the flags can
be combined...Perhaps it is not a problem today, but later you may want more flags. e.g.
#define SUBSCRIPTION_REL_STATE_READY 0x02 /* READY relations */then the bitmask idea falls apart because IIUC you have no intentions
to permit things like:
(SUBSCRIPTION_REL_STATE_NOT_READY | SUBSCRIPTION_REL_STATE_READY)I think this will be an invalid combination if caller ever used it.
However, one might need to use a combination like
(SUBSCRIPTION_REL_STATE_READY | SUBSCRIPTION_REL_STATE_DONE). For such
cases, I feel the bitmask idea will be better.It feels unnatural to me to have a flag saying "not-ready" and one
saying "ready", while we could have a flag saying "ready" that can be
combined with a second flag to decide if the contents of srsubstate
should be matched or *not* matched with the states expected by the
caller. This could be extended to more state values, for example.I am not sure if we actually need this much as I have no idea if
future features would use it, so please take my suggestion lightly :)
Yeah, it is not very clear to me either. I think this won't be
difficult to change one or another way depending on future needs. At
this stage, it appeared to me that bitmask is a better way to
represent this information but if you and other feels using enum is a
better idea then I am fine with that as well.
--
With Regards,
Amit Kapila.
On Thu, Jul 21, 2022 at 09:54:05AM +0530, Amit Kapila wrote:
Yeah, it is not very clear to me either. I think this won't be
difficult to change one or another way depending on future needs. At
this stage, it appeared to me that bitmask is a better way to
represent this information but if you and other feels using enum is a
better idea then I am fine with that as well.
Please don't get me wrong :)
I favor a bitmask over an enum here, as you do, with a clean
layer for those flags.
--
Michael
On Thu, Jul 21, 2022 at 10:03 AM Michael Paquier <michael@paquier.xyz> wrote:
On Thu, Jul 21, 2022 at 09:54:05AM +0530, Amit Kapila wrote:
Yeah, it is not very clear to me either. I think this won't be
difficult to change one or another way depending on future needs. At
this stage, it appeared to me that bitmask is a better way to
represent this information but if you and other feels using enum is a
better idea then I am fine with that as well.Please don't get me wrong :)
I favor a bitmask over an enum here, as you do, with a clean
layer for those flags.
Okay, let's see what Peter Smith has to say about this as he was in
favor of using enum here?
--
With Regards,
Amit Kapila.
On Thu, Jul 21, 2022 at 10:11 PM Amit Kapila <amit.kapila16@gmail.com> wrote:
On Thu, Jul 21, 2022 at 10:03 AM Michael Paquier <michael@paquier.xyz> wrote:
On Thu, Jul 21, 2022 at 09:54:05AM +0530, Amit Kapila wrote:
Yeah, it is not very clear to me either. I think this won't be
difficult to change one or another way depending on future needs. At
this stage, it appeared to me that bitmask is a better way to
represent this information but if you and other feels using enum is a
better idea then I am fine with that as well.Please don't get me wrong :)
I favor a bitmask over an enum here, as you do, with a clean
layer for those flags.Okay, let's see what Peter Smith has to say about this as he was in
favor of using enum here?
I was in favour of enum mostly because I thought the bitmask of an
earlier patch was mis-used; IMO each bit should only be for
representing something as "on/set". So a bit for
SUBSCRIPTION_REL_STATE_READY makes sense, but a bit for
SUBSCRIPTION_REL_STATE_NOT_READY seemed strange/backwards to me. YMMV.
So using a bitmask is fine, except I thought it should be implemented
so that one of the bits is for a "NOT" modifier (IIUC this is kind of
similar to what Michael [1]/messages/by-id/Ytiuj4hLykTvBF46@paquier.xyz suggested above?). So "Not READY" would be
(SUBSCRIPTION_REL_STATE_MOD_NOT | SUBSCRIPTION_REL_STATE_READY)
Also, it may be better to add the bit constants for every one of the
current states, even if you are not needing to use all of them just
yet. In fact, I thought this patch probably can implement the fully
capable common function (i.e. capable of multiple keys etc) right now,
so there will be no need to revisit it again in the future.
------
[1]: /messages/by-id/Ytiuj4hLykTvBF46@paquier.xyz
Kind Regards,
Peter Smith.
Fujitsu Australia
On Fri, Jul 22, 2022 at 3:47 AM Peter Smith <smithpb2250@gmail.com> wrote:
I was in favour of enum mostly because I thought the bitmask of an
earlier patch was mis-used; IMO each bit should only be for
representing something as "on/set". So a bit for
SUBSCRIPTION_REL_STATE_READY makes sense, but a bit for
SUBSCRIPTION_REL_STATE_NOT_READY seemed strange/backwards to me. YMMV.So using a bitmask is fine, except I thought it should be implemented
so that one of the bits is for a "NOT" modifier (IIUC this is kind of
similar to what Michael [1] suggested above?). So "Not READY" would be
(SUBSCRIPTION_REL_STATE_MOD_NOT | SUBSCRIPTION_REL_STATE_READY)
Hmm, I think that sounds more complicated than what I expected. I
suggest let's go with a simple idea of using a boolean not_ready which
will decide whether to use the additional key to search. I feel we can
extend it by using a bitmask or enum when we have a clear need for
more states.
Also, it may be better to add the bit constants for every one of the
current states, even if you are not needing to use all of them just
yet. In fact, I thought this patch probably can implement the fully
capable common function (i.e. capable of multiple keys etc) right now,
so there will be no need to revisit it again in the future.
I don't know whether we need to go that far. Say for a year or so if
we don't have such a use case arising which appears to be quite likely
then one can question the need for additional defines.
--
With Regards,
Amit Kapila.
At Fri, 22 Jul 2022 11:11:23 +0530, Amit Kapila <amit.kapila16@gmail.com> wrote in
Hmm, I think that sounds more complicated than what I expected. I
suggest let's go with a simple idea of using a boolean not_ready which
will decide whether to use the additional key to search. I feel we can
extend it by using a bitmask or enum when we have a clear need for
more states.
FWIW, I vote for (only_)not_ready after rading through the thread..
regards.
--
Kyotaro Horiguchi
NTT Open Source Software Center
On Fri, Jul 22, 2022 at 11:11 AM Amit Kapila <amit.kapila16@gmail.com> wrote:
On Fri, Jul 22, 2022 at 3:47 AM Peter Smith <smithpb2250@gmail.com> wrote:
I was in favour of enum mostly because I thought the bitmask of an
earlier patch was mis-used; IMO each bit should only be for
representing something as "on/set". So a bit for
SUBSCRIPTION_REL_STATE_READY makes sense, but a bit for
SUBSCRIPTION_REL_STATE_NOT_READY seemed strange/backwards to me. YMMV.So using a bitmask is fine, except I thought it should be implemented
so that one of the bits is for a "NOT" modifier (IIUC this is kind of
similar to what Michael [1] suggested above?). So "Not READY" would be
(SUBSCRIPTION_REL_STATE_MOD_NOT | SUBSCRIPTION_REL_STATE_READY)Hmm, I think that sounds more complicated than what I expected. I
suggest let's go with a simple idea of using a boolean not_ready which
will decide whether to use the additional key to search. I feel we can
extend it by using a bitmask or enum when we have a clear need for
more states.
Thanks for the comments, i have modified it by changing it to a
boolean parameter. The attached v4 patch has the changes for the same.
Regards,
Vignesh
Attachments:
v4-0001-Refactor-to-make-use-of-a-common-function-for-Get.patchtext/x-patch; charset=US-ASCII; name=v4-0001-Refactor-to-make-use-of-a-common-function-for-Get.patchDownload
From e5edbf8dd47bbfcd7d8fcf3add8acab502e48665 Mon Sep 17 00:00:00 2001
From: Vigneshwaran C <vignesh21@gmail.com>
Date: Wed, 13 Jul 2022 11:54:59 +0530
Subject: [PATCH v4] Refactor to make use of a common function for
GetSubscriptionRelations and GetSubscriptionNotReadyRelations.
Most of the code is common between GetSubscriptionRelations and
GetSubscriptionNotReadyRelations. Added a parameter to GetSubscriptionRelations
which could provide the same functionality for GetSubscriptionRelations
and GetSubscriptionNotReadyRelations.
Author: Vignesh C
Reviewed-By: Michael Paquier, Peter Smith, Amit Kapila, Kyotaro Horiguchi
Discussion: https://www.postgresql.org/message-id/flat/CALDaNm0eW-9g4G_EzHebnFT5zZoasWCS_EzZQ5BgnLZny9S%3Dpg%40mail.gmail.com
---
src/backend/catalog/pg_subscription.c | 70 +++------------------
src/backend/commands/subscriptioncmds.c | 4 +-
src/backend/replication/logical/tablesync.c | 2 +-
src/include/catalog/pg_subscription_rel.h | 3 +-
4 files changed, 14 insertions(+), 65 deletions(-)
diff --git a/src/backend/catalog/pg_subscription.c b/src/backend/catalog/pg_subscription.c
index 33ae3da8ae..3d81449382 100644
--- a/src/backend/catalog/pg_subscription.c
+++ b/src/backend/catalog/pg_subscription.c
@@ -533,65 +533,14 @@ HasSubscriptionRelations(Oid subid)
}
/*
- * Get all relations for subscription.
+ * Get the relations for subscription.
*
- * Returned list is palloc'ed in current memory context.
+ * If only_not_ready is false, return all the relations for subscription. If
+ * true, return all the relations for subscription that are not in a ready
+ * state. Returned list is palloc'ed in current memory context.
*/
List *
-GetSubscriptionRelations(Oid subid)
-{
- 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)
+GetSubscriptionRelations(Oid subid, bool only_not_ready)
{
List *res = NIL;
Relation rel;
@@ -607,10 +556,11 @@ GetSubscriptionNotReadyRelations(Oid subid)
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(subid));
- ScanKeyInit(&skey[nkeys++],
- Anum_pg_subscription_rel_srsubstate,
- BTEqualStrategyNumber, F_CHARNE,
- CharGetDatum(SUBREL_STATE_READY));
+ if (only_not_ready)
+ ScanKeyInit(&skey[nkeys++],
+ Anum_pg_subscription_rel_srsubstate,
+ BTEqualStrategyNumber, F_CHARNE,
+ CharGetDatum(SUBREL_STATE_READY));
scan = systable_beginscan(rel, InvalidOid, false,
NULL, nkeys, skey);
diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c
index bd0cc0848d..f73dfb6067 100644
--- a/src/backend/commands/subscriptioncmds.c
+++ b/src/backend/commands/subscriptioncmds.c
@@ -814,7 +814,7 @@ AlterSubscription_refresh(Subscription *sub, bool copy_data,
pubrel_names = fetch_table_list(wrconn, sub->publications);
/* 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
@@ -1494,7 +1494,7 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel)
* the apply and tablesync workers and they can't restart because of
* exclusive lock on the subscription.
*/
- rstates = GetSubscriptionNotReadyRelations(subid);
+ rstates = GetSubscriptionRelations(subid, true);
foreach(lc, rstates)
{
SubscriptionRelState *rstate = (SubscriptionRelState *) lfirst(lc);
diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c
index 670c6fcada..6a01ffd273 100644
--- a/src/backend/replication/logical/tablesync.c
+++ b/src/backend/replication/logical/tablesync.c
@@ -1479,7 +1479,7 @@ FetchTableStates(bool *started_tx)
}
/* Fetch all non-ready tables. */
- rstates = GetSubscriptionNotReadyRelations(MySubscription->oid);
+ rstates = GetSubscriptionRelations(MySubscription->oid, true);
/* Allocate the tracking info in a permanent memory context. */
oldctx = MemoryContextSwitchTo(CacheMemoryContext);
diff --git a/src/include/catalog/pg_subscription_rel.h b/src/include/catalog/pg_subscription_rel.h
index 9df99c3418..e835c42ef7 100644
--- a/src/include/catalog/pg_subscription_rel.h
+++ b/src/include/catalog/pg_subscription_rel.h
@@ -88,7 +88,6 @@ extern char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn);
extern void RemoveSubscriptionRel(Oid subid, Oid relid);
extern bool HasSubscriptionRelations(Oid subid);
-extern List *GetSubscriptionRelations(Oid subid);
-extern List *GetSubscriptionNotReadyRelations(Oid subid);
+extern List *GetSubscriptionRelations(Oid subid, bool only_not_ready);
#endif /* PG_SUBSCRIPTION_REL_H */
--
2.32.0
On Sun, Jul 24, 2022 at 09:52:16PM +0530, vignesh C wrote:
Thanks for the comments, i have modified it by changing it to a
boolean parameter. The attached v4 patch has the changes for the same.
Okay, thanks for the patch. This looks good to me, so let's do as
Amit suggests. I'll apply that if there are no objections.
--
Michael
On Mon, Jul 25, 2022 at 11:08 AM Michael Paquier <michael@paquier.xyz> wrote:
On Sun, Jul 24, 2022 at 09:52:16PM +0530, vignesh C wrote:
Thanks for the comments, i have modified it by changing it to a
boolean parameter. The attached v4 patch has the changes for the same.Okay, thanks for the patch. This looks good to me, so let's do as
Amit suggests. I'll apply that if there are no objections.
--
OK. I have no objections to just passing a boolean, but here are a
couple of other small review comments for the v4-0001 patch:
======
1. src/backend/catalog/pg_subscription.c
@@ -533,65 +533,14 @@ HasSubscriptionRelations(Oid subid)
}
/*
- * Get all relations for subscription.
+ * Get the relations for subscription.
*
- * Returned list is palloc'ed in current memory context.
+ * If only_not_ready is false, return all the relations for subscription. If
+ * true, return all the relations for subscription that are not in a ready
+ * state. Returned list is palloc'ed in current memory context.
*/
The function comment was describing the new boolean parameter in a
kind of backwards way. It seems more natural to emphasise what true
means.
SUGGESTION
Get the relations for the subscription.
If only_not_ready is true, return only the relations that are not in a
ready state, otherwise return all the subscription relations. The
returned list is palloc'ed in the current memory context.
====
2. <General - calling code>
Perhaps this suggestion is overkill, but given that the param is not
going to be a bitmask or enum anymore, IMO it means the calls are no
longer very self-explanatory.The calling code will be more readable if
the patch introduced some descriptive wrapper functions. e.g.
List *
GetSubscriptionAllRelations(Oid subid)
{
return GetSubscriptionRelations(subid, false);
}
List *
GetSubscriptionNotReadyRelations(Oid subid)
{
return GetSubscriptionRelations(subid, true);
}
------
Kind Regards,
Peter Smith.
Fujitsu Australia
On Mon, Jul 25, 2022 at 8:34 AM Peter Smith <smithpb2250@gmail.com> wrote:
On Mon, Jul 25, 2022 at 11:08 AM Michael Paquier <michael@paquier.xyz> wrote:
On Sun, Jul 24, 2022 at 09:52:16PM +0530, vignesh C wrote:
Thanks for the comments, i have modified it by changing it to a
boolean parameter. The attached v4 patch has the changes for the same.Okay, thanks for the patch. This looks good to me, so let's do as
Amit suggests. I'll apply that if there are no objections.
--OK. I have no objections to just passing a boolean, but here are a
couple of other small review comments for the v4-0001 patch:======
1. src/backend/catalog/pg_subscription.c
@@ -533,65 +533,14 @@ HasSubscriptionRelations(Oid subid)
}/* - * Get all relations for subscription. + * Get the relations for subscription. * - * Returned list is palloc'ed in current memory context. + * If only_not_ready is false, return all the relations for subscription. If + * true, return all the relations for subscription that are not in a ready + * state. Returned list is palloc'ed in current memory context. */The function comment was describing the new boolean parameter in a
kind of backwards way. It seems more natural to emphasise what true
means.SUGGESTION
Get the relations for the subscription.If only_not_ready is true, return only the relations that are not in a
ready state, otherwise return all the subscription relations. The
returned list is palloc'ed in the current memory context.
This suggestion sounds good. Also, I don't much like "only" in the
parameter name. I think the explanation makes it clear.
====
2. <General - calling code>
Perhaps this suggestion is overkill, but given that the param is not
going to be a bitmask or enum anymore, IMO it means the calls are no
longer very self-explanatory.
I am not sure how much this will be helpful. This sounds like overkill.
--
With Regards,
Amit Kapila.
On Mon, Jul 25, 2022 at 8:34 AM Peter Smith <smithpb2250@gmail.com> wrote:
On Mon, Jul 25, 2022 at 11:08 AM Michael Paquier <michael@paquier.xyz> wrote:
On Sun, Jul 24, 2022 at 09:52:16PM +0530, vignesh C wrote:
Thanks for the comments, i have modified it by changing it to a
boolean parameter. The attached v4 patch has the changes for the same.Okay, thanks for the patch. This looks good to me, so let's do as
Amit suggests. I'll apply that if there are no objections.
--OK. I have no objections to just passing a boolean, but here are a
couple of other small review comments for the v4-0001 patch:======
1. src/backend/catalog/pg_subscription.c
@@ -533,65 +533,14 @@ HasSubscriptionRelations(Oid subid)
}/* - * Get all relations for subscription. + * Get the relations for subscription. * - * Returned list is palloc'ed in current memory context. + * If only_not_ready is false, return all the relations for subscription. If + * true, return all the relations for subscription that are not in a ready + * state. Returned list is palloc'ed in current memory context. */The function comment was describing the new boolean parameter in a
kind of backwards way. It seems more natural to emphasise what true
means.SUGGESTION
Get the relations for the subscription.If only_not_ready is true, return only the relations that are not in a
ready state, otherwise return all the subscription relations. The
returned list is palloc'ed in the current memory context.
Modified
====
2. <General - calling code>
Perhaps this suggestion is overkill, but given that the param is not
going to be a bitmask or enum anymore, IMO it means the calls are no
longer very self-explanatory.The calling code will be more readable if
the patch introduced some descriptive wrapper functions. e.g.List *
GetSubscriptionAllRelations(Oid subid)
{
return GetSubscriptionRelations(subid, false);
}List *
GetSubscriptionNotReadyRelations(Oid subid)
{
return GetSubscriptionRelations(subid, true);
}
I feel this would be an overkill, I did not make any changes for this.
Thanks for the comments, the attached v5 patch has the changes for the same.
Regards,
Vignesh
Attachments:
v5-0001-Refactor-to-make-use-of-a-common-function-for-Get.patchapplication/x-patch; name=v5-0001-Refactor-to-make-use-of-a-common-function-for-Get.patchDownload
From 4172eeaef8f7890e6ff0148de65332264d039075 Mon Sep 17 00:00:00 2001
From: Vigneshwaran C <vignesh21@gmail.com>
Date: Wed, 13 Jul 2022 11:54:59 +0530
Subject: [PATCH v5] Refactor to make use of a common function for
GetSubscriptionRelations and GetSubscriptionNotReadyRelations.
Most of the code is common between GetSubscriptionRelations and
GetSubscriptionNotReadyRelations. Added a parameter to GetSubscriptionRelations
which could provide the same functionality for GetSubscriptionRelations
and GetSubscriptionNotReadyRelations.
Author: Vignesh C
Reviewed-By: Michael Paquier, Peter Smith, Amit Kapila, Kyotaro Horiguchi
Discussion: https://www.postgresql.org/message-id/flat/CALDaNm0eW-9g4G_EzHebnFT5zZoasWCS_EzZQ5BgnLZny9S%3Dpg%40mail.gmail.com
---
src/backend/catalog/pg_subscription.c | 70 +++------------------
src/backend/commands/subscriptioncmds.c | 4 +-
src/backend/replication/logical/tablesync.c | 2 +-
src/include/catalog/pg_subscription_rel.h | 3 +-
4 files changed, 14 insertions(+), 65 deletions(-)
diff --git a/src/backend/catalog/pg_subscription.c b/src/backend/catalog/pg_subscription.c
index 33ae3da8ae..a60fbaca26 100644
--- a/src/backend/catalog/pg_subscription.c
+++ b/src/backend/catalog/pg_subscription.c
@@ -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 subscription relations. The returned list is
+ * palloc'ed in the current memory context.
*/
List *
-GetSubscriptionRelations(Oid subid)
-{
- 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)
+GetSubscriptionRelations(Oid subid, bool not_ready)
{
List *res = NIL;
Relation rel;
@@ -607,10 +556,11 @@ GetSubscriptionNotReadyRelations(Oid subid)
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(subid));
- ScanKeyInit(&skey[nkeys++],
- Anum_pg_subscription_rel_srsubstate,
- BTEqualStrategyNumber, F_CHARNE,
- CharGetDatum(SUBREL_STATE_READY));
+ if (not_ready)
+ ScanKeyInit(&skey[nkeys++],
+ Anum_pg_subscription_rel_srsubstate,
+ BTEqualStrategyNumber, F_CHARNE,
+ CharGetDatum(SUBREL_STATE_READY));
scan = systable_beginscan(rel, InvalidOid, false,
NULL, nkeys, skey);
diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c
index bd0cc0848d..f73dfb6067 100644
--- a/src/backend/commands/subscriptioncmds.c
+++ b/src/backend/commands/subscriptioncmds.c
@@ -814,7 +814,7 @@ AlterSubscription_refresh(Subscription *sub, bool copy_data,
pubrel_names = fetch_table_list(wrconn, sub->publications);
/* 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
@@ -1494,7 +1494,7 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel)
* the apply and tablesync workers and they can't restart because of
* exclusive lock on the subscription.
*/
- rstates = GetSubscriptionNotReadyRelations(subid);
+ rstates = GetSubscriptionRelations(subid, true);
foreach(lc, rstates)
{
SubscriptionRelState *rstate = (SubscriptionRelState *) lfirst(lc);
diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c
index 670c6fcada..6a01ffd273 100644
--- a/src/backend/replication/logical/tablesync.c
+++ b/src/backend/replication/logical/tablesync.c
@@ -1479,7 +1479,7 @@ FetchTableStates(bool *started_tx)
}
/* Fetch all non-ready tables. */
- rstates = GetSubscriptionNotReadyRelations(MySubscription->oid);
+ rstates = GetSubscriptionRelations(MySubscription->oid, true);
/* Allocate the tracking info in a permanent memory context. */
oldctx = MemoryContextSwitchTo(CacheMemoryContext);
diff --git a/src/include/catalog/pg_subscription_rel.h b/src/include/catalog/pg_subscription_rel.h
index 9df99c3418..8e88de7b2b 100644
--- a/src/include/catalog/pg_subscription_rel.h
+++ b/src/include/catalog/pg_subscription_rel.h
@@ -88,7 +88,6 @@ extern char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn);
extern void RemoveSubscriptionRel(Oid subid, Oid relid);
extern bool HasSubscriptionRelations(Oid subid);
-extern List *GetSubscriptionRelations(Oid subid);
-extern List *GetSubscriptionNotReadyRelations(Oid subid);
+extern List *GetSubscriptionRelations(Oid subid, bool not_ready);
#endif /* PG_SUBSCRIPTION_REL_H */
--
2.32.0
On Mon, Jul 25, 2022 at 10:22 AM Amit Kapila <amit.kapila16@gmail.com> wrote:
On Mon, Jul 25, 2022 at 8:34 AM Peter Smith <smithpb2250@gmail.com> wrote:
On Mon, Jul 25, 2022 at 11:08 AM Michael Paquier <michael@paquier.xyz> wrote:
On Sun, Jul 24, 2022 at 09:52:16PM +0530, vignesh C wrote:
Thanks for the comments, i have modified it by changing it to a
boolean parameter. The attached v4 patch has the changes for the same.Okay, thanks for the patch. This looks good to me, so let's do as
Amit suggests. I'll apply that if there are no objections.
--OK. I have no objections to just passing a boolean, but here are a
couple of other small review comments for the v4-0001 patch:======
1. src/backend/catalog/pg_subscription.c
@@ -533,65 +533,14 @@ HasSubscriptionRelations(Oid subid)
}/* - * Get all relations for subscription. + * Get the relations for subscription. * - * Returned list is palloc'ed in current memory context. + * If only_not_ready is false, return all the relations for subscription. If + * true, return all the relations for subscription that are not in a ready + * state. Returned list is palloc'ed in current memory context. */The function comment was describing the new boolean parameter in a
kind of backwards way. It seems more natural to emphasise what true
means.SUGGESTION
Get the relations for the subscription.If only_not_ready is true, return only the relations that are not in a
ready state, otherwise return all the subscription relations. The
returned list is palloc'ed in the current memory context.This suggestion sounds good. Also, I don't much like "only" in the
parameter name. I think the explanation makes it clear.
I have changed the parameter name to not_ready. The v5 patch attached
at [1]/messages/by-id/CALDaNm2sQD-bwMKavLyiogMBBrg3fx5PTaV5RyV8UiczR9K_tw@mail.gmail.com has the changes for the same.
[1]: /messages/by-id/CALDaNm2sQD-bwMKavLyiogMBBrg3fx5PTaV5RyV8UiczR9K_tw@mail.gmail.com
Regards,
Vignesh
On Wed, Jul 27, 2022 at 08:47:46AM +0530, vignesh C wrote:
I feel this would be an overkill, I did not make any changes for this.
Agreed. Using an extra layer of wrappers for that seems a bit too
much, so I have applied your v5 with a slight tweak to the comment.
--
Michael
On Wed, Jul 27, 2022 at 4:22 PM Michael Paquier <michael@paquier.xyz> wrote:
On Wed, Jul 27, 2022 at 08:47:46AM +0530, vignesh C wrote:
I feel this would be an overkill, I did not make any changes for this.
Agreed. Using an extra layer of wrappers for that seems a bit too
much, so I have applied your v5 with a slight tweak to the comment.
Thanks for pushing this patch.
Regards,
Vignesh