Skip to content

Commit 08691ea

Browse files
committed
Rework some code handling pg_subscription data in psql and pg_dump
This commit fixes some inconsistencies found in the frontend code when dealing with subscription catalog data. The following changes are done: - pg_subscription.h gains a EXPOSE_TO_CLIENT_CODE, so as more content defined in pg_subscription.h becomes available in pg_subscription_d.h for the frontend. - In psql's describe.c, substream can be switched to use CppAsString2() with its three LOGICALREP_STREAM_* values, with pg_subscription_d.h included. - pg_dump.c included pg_subscription.h, which is a header that should only be used in the backend code. The code is updated to use pg_subscription_d.h instead. - pg_dump stored all the data from pg_subscription in SubscriptionInfo with only strings, and a good chunk of them are boolean and char values. Using strings is not necessary, complicates the code (see for example two_phase_disabled[] removed here), and is inconsistent with the way other catalogs' data is handled. The fields of SubscriptionInfo are reordered to match with the order in its catalog, while on it. Reviewed-by: Hayato Kuroda Discussion: https://postgr.es/m/[email protected]
1 parent 75818b3 commit 08691ea

File tree

4 files changed

+58
-56
lines changed

4 files changed

+58
-56
lines changed

src/bin/pg_dump/pg_dump.c

+22-25
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
#include "catalog/pg_default_acl_d.h"
5151
#include "catalog/pg_largeobject_d.h"
5252
#include "catalog/pg_proc_d.h"
53-
#include "catalog/pg_subscription.h"
53+
#include "catalog/pg_subscription_d.h"
5454
#include "catalog/pg_type_d.h"
5555
#include "common/connect.h"
5656
#include "common/int.h"
@@ -4968,20 +4968,20 @@ getSubscriptions(Archive *fout)
49684968
i_oid = PQfnumber(res, "oid");
49694969
i_subname = PQfnumber(res, "subname");
49704970
i_subowner = PQfnumber(res, "subowner");
4971+
i_subenabled = PQfnumber(res, "subenabled");
49714972
i_subbinary = PQfnumber(res, "subbinary");
49724973
i_substream = PQfnumber(res, "substream");
49734974
i_subtwophasestate = PQfnumber(res, "subtwophasestate");
49744975
i_subdisableonerr = PQfnumber(res, "subdisableonerr");
49754976
i_subpasswordrequired = PQfnumber(res, "subpasswordrequired");
49764977
i_subrunasowner = PQfnumber(res, "subrunasowner");
4978+
i_subfailover = PQfnumber(res, "subfailover");
49774979
i_subconninfo = PQfnumber(res, "subconninfo");
49784980
i_subslotname = PQfnumber(res, "subslotname");
49794981
i_subsynccommit = PQfnumber(res, "subsynccommit");
49804982
i_subpublications = PQfnumber(res, "subpublications");
49814983
i_suborigin = PQfnumber(res, "suborigin");
49824984
i_suboriginremotelsn = PQfnumber(res, "suboriginremotelsn");
4983-
i_subenabled = PQfnumber(res, "subenabled");
4984-
i_subfailover = PQfnumber(res, "subfailover");
49854985

49864986
subinfo = pg_malloc(ntups * sizeof(SubscriptionInfo));
49874987

@@ -4995,18 +4995,20 @@ getSubscriptions(Archive *fout)
49954995
subinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_subname));
49964996
subinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_subowner));
49974997

4998+
subinfo[i].subenabled =
4999+
(strcmp(PQgetvalue(res, i, i_subenabled), "t") == 0);
49985000
subinfo[i].subbinary =
4999-
pg_strdup(PQgetvalue(res, i, i_subbinary));
5000-
subinfo[i].substream =
5001-
pg_strdup(PQgetvalue(res, i, i_substream));
5002-
subinfo[i].subtwophasestate =
5003-
pg_strdup(PQgetvalue(res, i, i_subtwophasestate));
5001+
(strcmp(PQgetvalue(res, i, i_subbinary), "t") == 0);
5002+
subinfo[i].substream = *(PQgetvalue(res, i, i_substream));
5003+
subinfo[i].subtwophasestate = *(PQgetvalue(res, i, i_subtwophasestate));
50045004
subinfo[i].subdisableonerr =
5005-
pg_strdup(PQgetvalue(res, i, i_subdisableonerr));
5005+
(strcmp(PQgetvalue(res, i, i_subdisableonerr), "t") == 0);
50065006
subinfo[i].subpasswordrequired =
5007-
pg_strdup(PQgetvalue(res, i, i_subpasswordrequired));
5007+
(strcmp(PQgetvalue(res, i, i_subpasswordrequired), "t") == 0);
50085008
subinfo[i].subrunasowner =
5009-
pg_strdup(PQgetvalue(res, i, i_subrunasowner));
5009+
(strcmp(PQgetvalue(res, i, i_subrunasowner), "t") == 0);
5010+
subinfo[i].subfailover =
5011+
(strcmp(PQgetvalue(res, i, i_subfailover), "t") == 0);
50105012
subinfo[i].subconninfo =
50115013
pg_strdup(PQgetvalue(res, i, i_subconninfo));
50125014
if (PQgetisnull(res, i, i_subslotname))
@@ -5024,10 +5026,6 @@ getSubscriptions(Archive *fout)
50245026
else
50255027
subinfo[i].suboriginremotelsn =
50265028
pg_strdup(PQgetvalue(res, i, i_suboriginremotelsn));
5027-
subinfo[i].subenabled =
5028-
pg_strdup(PQgetvalue(res, i, i_subenabled));
5029-
subinfo[i].subfailover =
5030-
pg_strdup(PQgetvalue(res, i, i_subfailover));
50315029

50325030
/* Decide whether we want to dump it */
50335031
selectDumpableObject(&(subinfo[i].dobj), fout);
@@ -5208,7 +5206,6 @@ dumpSubscription(Archive *fout, const SubscriptionInfo *subinfo)
52085206
char **pubnames = NULL;
52095207
int npubnames = 0;
52105208
int i;
5211-
char two_phase_disabled[] = {LOGICALREP_TWOPHASE_STATE_DISABLED, '\0'};
52125209

52135210
/* Do nothing if not dumping schema */
52145211
if (!dopt->dumpSchema)
@@ -5245,29 +5242,29 @@ dumpSubscription(Archive *fout, const SubscriptionInfo *subinfo)
52455242
else
52465243
appendPQExpBufferStr(query, "NONE");
52475244

5248-
if (strcmp(subinfo->subbinary, "t") == 0)
5245+
if (subinfo->subbinary)
52495246
appendPQExpBufferStr(query, ", binary = true");
52505247

5251-
if (strcmp(subinfo->substream, "t") == 0)
5248+
if (subinfo->substream == LOGICALREP_STREAM_ON)
52525249
appendPQExpBufferStr(query, ", streaming = on");
5253-
else if (strcmp(subinfo->substream, "p") == 0)
5250+
else if (subinfo->substream == LOGICALREP_STREAM_PARALLEL)
52545251
appendPQExpBufferStr(query, ", streaming = parallel");
52555252
else
52565253
appendPQExpBufferStr(query, ", streaming = off");
52575254

5258-
if (strcmp(subinfo->subtwophasestate, two_phase_disabled) != 0)
5255+
if (subinfo->subtwophasestate != LOGICALREP_TWOPHASE_STATE_DISABLED)
52595256
appendPQExpBufferStr(query, ", two_phase = on");
52605257

5261-
if (strcmp(subinfo->subdisableonerr, "t") == 0)
5258+
if (subinfo->subdisableonerr)
52625259
appendPQExpBufferStr(query, ", disable_on_error = true");
52635260

5264-
if (strcmp(subinfo->subpasswordrequired, "t") != 0)
5261+
if (!subinfo->subpasswordrequired)
52655262
appendPQExpBuffer(query, ", password_required = false");
52665263

5267-
if (strcmp(subinfo->subrunasowner, "t") == 0)
5264+
if (subinfo->subrunasowner)
52685265
appendPQExpBufferStr(query, ", run_as_owner = true");
52695266

5270-
if (strcmp(subinfo->subfailover, "t") == 0)
5267+
if (subinfo->subfailover)
52715268
appendPQExpBufferStr(query, ", failover = true");
52725269

52735270
if (strcmp(subinfo->subsynccommit, "off") != 0)
@@ -5303,7 +5300,7 @@ dumpSubscription(Archive *fout, const SubscriptionInfo *subinfo)
53035300
appendPQExpBuffer(query, ", '%s');\n", subinfo->suboriginremotelsn);
53045301
}
53055302

5306-
if (strcmp(subinfo->subenabled, "t") == 0)
5303+
if (subinfo->subenabled)
53075304
{
53085305
/*
53095306
* Enable the subscription to allow the replication to continue

src/bin/pg_dump/pg_dump.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -664,20 +664,20 @@ typedef struct _SubscriptionInfo
664664
{
665665
DumpableObject dobj;
666666
const char *rolname;
667-
char *subenabled;
668-
char *subbinary;
669-
char *substream;
670-
char *subtwophasestate;
671-
char *subdisableonerr;
672-
char *subpasswordrequired;
673-
char *subrunasowner;
667+
bool subenabled;
668+
bool subbinary;
669+
char substream;
670+
char subtwophasestate;
671+
bool subdisableonerr;
672+
bool subpasswordrequired;
673+
bool subrunasowner;
674+
bool subfailover;
674675
char *subconninfo;
675676
char *subslotname;
676677
char *subsynccommit;
677678
char *subpublications;
678679
char *suborigin;
679680
char *suboriginremotelsn;
680-
char *subfailover;
681681
} SubscriptionInfo;
682682

683683
/*

src/bin/psql/describe.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "catalog/pg_default_acl_d.h"
2626
#include "catalog/pg_proc_d.h"
2727
#include "catalog/pg_statistic_ext_d.h"
28+
#include "catalog/pg_subscription_d.h"
2829
#include "catalog/pg_type_d.h"
2930
#include "common.h"
3031
#include "common/logging.h"
@@ -6679,9 +6680,9 @@ describeSubscriptions(const char *pattern, bool verbose)
66796680
if (pset.sversion >= 160000)
66806681
appendPQExpBuffer(&buf,
66816682
", (CASE substream\n"
6682-
" WHEN 'f' THEN 'off'\n"
6683-
" WHEN 't' THEN 'on'\n"
6684-
" WHEN 'p' THEN 'parallel'\n"
6683+
" WHEN " CppAsString2(LOGICALREP_STREAM_OFF) " THEN 'off'\n"
6684+
" WHEN " CppAsString2(LOGICALREP_STREAM_ON) " THEN 'on'\n"
6685+
" WHEN " CppAsString2(LOGICALREP_STREAM_PARALLEL) " THEN 'parallel'\n"
66856686
" END) AS \"%s\"\n",
66866687
gettext_noop("Streaming"));
66876688
else

src/include/catalog/pg_subscription.h

+24-20
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,6 @@
2323
#include "lib/stringinfo.h"
2424
#include "nodes/pg_list.h"
2525

26-
/*
27-
* two_phase tri-state values. See comments atop worker.c to know more about
28-
* these states.
29-
*/
30-
#define LOGICALREP_TWOPHASE_STATE_DISABLED 'd'
31-
#define LOGICALREP_TWOPHASE_STATE_PENDING 'p'
32-
#define LOGICALREP_TWOPHASE_STATE_ENABLED 'e'
33-
34-
/*
35-
* The subscription will request the publisher to only send changes that do not
36-
* have any origin.
37-
*/
38-
#define LOGICALREP_ORIGIN_NONE "none"
39-
40-
/*
41-
* The subscription will request the publisher to send changes regardless
42-
* of their origin.
43-
*/
44-
#define LOGICALREP_ORIGIN_ANY "any"
45-
4626
/* ----------------
4727
* pg_subscription definition. cpp turns this into
4828
* typedef struct FormData_pg_subscription
@@ -159,6 +139,28 @@ typedef struct Subscription
159139
* specified origin */
160140
} Subscription;
161141

142+
#ifdef EXPOSE_TO_CLIENT_CODE
143+
144+
/*
145+
* two_phase tri-state values. See comments atop worker.c to know more about
146+
* these states.
147+
*/
148+
#define LOGICALREP_TWOPHASE_STATE_DISABLED 'd'
149+
#define LOGICALREP_TWOPHASE_STATE_PENDING 'p'
150+
#define LOGICALREP_TWOPHASE_STATE_ENABLED 'e'
151+
152+
/*
153+
* The subscription will request the publisher to only send changes that do not
154+
* have any origin.
155+
*/
156+
#define LOGICALREP_ORIGIN_NONE "none"
157+
158+
/*
159+
* The subscription will request the publisher to send changes regardless
160+
* of their origin.
161+
*/
162+
#define LOGICALREP_ORIGIN_ANY "any"
163+
162164
/* Disallow streaming in-progress transactions. */
163165
#define LOGICALREP_STREAM_OFF 'f'
164166

@@ -174,6 +176,8 @@ typedef struct Subscription
174176
*/
175177
#define LOGICALREP_STREAM_PARALLEL 'p'
176178

179+
#endif /* EXPOSE_TO_CLIENT_CODE */
180+
177181
extern Subscription *GetSubscription(Oid subid, bool missing_ok);
178182
extern void FreeSubscription(Subscription *sub);
179183
extern void DisableSubscription(Oid subid);

0 commit comments

Comments
 (0)