Skip to content

Commit

Permalink
Teach pg_dump about the new pg_subscription.subrunasowner option.
Browse files Browse the repository at this point in the history
Among numerous other oversights, commit 4826759 neglected to fix
pg_dump to dump this new subscription option.  Since the new default
is "false" while the previous behavior corresponds to "true", this
would cause legacy subscriptions to silently change behavior during
dump/reload or pg_upgrade.  That seems like a bad idea.  Even if it
was intended, failing to preserve the option once set in a new
installation is certainly not OK.

While here, reorder associated stanzas in pg_dump to match the
field order in pg_subscription, in hopes of reducing the impression
that all this code was written with the aid of a dartboard.

Back-patch to v16 where this new field was added.

Philip Warner (cosmetic tweaks by me)

Discussion: https://postgr.es/m/[email protected]
  • Loading branch information
tglsfdc committed Oct 29, 2023
1 parent b2d5544 commit 5ba4cc3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 30 deletions.
64 changes: 38 additions & 26 deletions src/bin/pg_dump/pg_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -4596,16 +4596,17 @@ getSubscriptions(Archive *fout)
int i_oid;
int i_subname;
int i_subowner;
int i_subbinary;
int i_substream;
int i_subtwophasestate;
int i_subdisableonerr;
int i_suborigin;
int i_subpasswordrequired;
int i_subrunasowner;
int i_subconninfo;
int i_subslotname;
int i_subsynccommit;
int i_subpublications;
int i_subbinary;
int i_subpasswordrequired;
int i_suborigin;
int i,
ntups;

Expand Down Expand Up @@ -4659,12 +4660,14 @@ getSubscriptions(Archive *fout)

if (fout->remoteVersion >= 160000)
appendPQExpBufferStr(query,
" s.suborigin,\n"
" s.subpasswordrequired\n");
" s.subpasswordrequired,\n"
" s.subrunasowner,\n"
" s.suborigin\n");
else
appendPQExpBuffer(query,
" '%s' AS suborigin,\n"
" 't' AS subpasswordrequired\n",
" 't' AS subpasswordrequired,\n"
" 't' AS subrunasowner,\n"
" '%s' AS suborigin\n",
LOGICALREP_ORIGIN_ANY);

appendPQExpBufferStr(query,
Expand All @@ -4684,16 +4687,17 @@ getSubscriptions(Archive *fout)
i_oid = PQfnumber(res, "oid");
i_subname = PQfnumber(res, "subname");
i_subowner = PQfnumber(res, "subowner");
i_subconninfo = PQfnumber(res, "subconninfo");
i_subslotname = PQfnumber(res, "subslotname");
i_subsynccommit = PQfnumber(res, "subsynccommit");
i_subpublications = PQfnumber(res, "subpublications");
i_subbinary = PQfnumber(res, "subbinary");
i_substream = PQfnumber(res, "substream");
i_subtwophasestate = PQfnumber(res, "subtwophasestate");
i_subdisableonerr = PQfnumber(res, "subdisableonerr");
i_suborigin = PQfnumber(res, "suborigin");
i_subpasswordrequired = PQfnumber(res, "subpasswordrequired");
i_subrunasowner = PQfnumber(res, "subrunasowner");
i_subconninfo = PQfnumber(res, "subconninfo");
i_subslotname = PQfnumber(res, "subslotname");
i_subsynccommit = PQfnumber(res, "subsynccommit");
i_subpublications = PQfnumber(res, "subpublications");
i_suborigin = PQfnumber(res, "suborigin");

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

Expand All @@ -4706,15 +4710,7 @@ getSubscriptions(Archive *fout)
AssignDumpId(&subinfo[i].dobj);
subinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_subname));
subinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_subowner));
subinfo[i].subconninfo = pg_strdup(PQgetvalue(res, i, i_subconninfo));
if (PQgetisnull(res, i, i_subslotname))
subinfo[i].subslotname = NULL;
else
subinfo[i].subslotname = pg_strdup(PQgetvalue(res, i, i_subslotname));
subinfo[i].subsynccommit =
pg_strdup(PQgetvalue(res, i, i_subsynccommit));
subinfo[i].subpublications =
pg_strdup(PQgetvalue(res, i, i_subpublications));

subinfo[i].subbinary =
pg_strdup(PQgetvalue(res, i, i_subbinary));
subinfo[i].substream =
Expand All @@ -4723,9 +4719,22 @@ getSubscriptions(Archive *fout)
pg_strdup(PQgetvalue(res, i, i_subtwophasestate));
subinfo[i].subdisableonerr =
pg_strdup(PQgetvalue(res, i, i_subdisableonerr));
subinfo[i].suborigin = pg_strdup(PQgetvalue(res, i, i_suborigin));
subinfo[i].subpasswordrequired =
pg_strdup(PQgetvalue(res, i, i_subpasswordrequired));
subinfo[i].subrunasowner =
pg_strdup(PQgetvalue(res, i, i_subrunasowner));
subinfo[i].subconninfo =
pg_strdup(PQgetvalue(res, i, i_subconninfo));
if (PQgetisnull(res, i, i_subslotname))
subinfo[i].subslotname = NULL;
else
subinfo[i].subslotname =
pg_strdup(PQgetvalue(res, i, i_subslotname));
subinfo[i].subsynccommit =
pg_strdup(PQgetvalue(res, i, i_subsynccommit));
subinfo[i].subpublications =
pg_strdup(PQgetvalue(res, i, i_subpublications));
subinfo[i].suborigin = pg_strdup(PQgetvalue(res, i, i_suborigin));

/* Decide whether we want to dump it */
selectDumpableObject(&(subinfo[i].dobj), fout);
Expand Down Expand Up @@ -4801,14 +4810,17 @@ dumpSubscription(Archive *fout, const SubscriptionInfo *subinfo)
if (strcmp(subinfo->subdisableonerr, "t") == 0)
appendPQExpBufferStr(query, ", disable_on_error = true");

if (pg_strcasecmp(subinfo->suborigin, LOGICALREP_ORIGIN_ANY) != 0)
appendPQExpBuffer(query, ", origin = %s", subinfo->suborigin);
if (strcmp(subinfo->subpasswordrequired, "t") != 0)
appendPQExpBuffer(query, ", password_required = false");

if (strcmp(subinfo->subrunasowner, "t") == 0)
appendPQExpBufferStr(query, ", run_as_owner = true");

if (strcmp(subinfo->subsynccommit, "off") != 0)
appendPQExpBuffer(query, ", synchronous_commit = %s", fmtId(subinfo->subsynccommit));

if (strcmp(subinfo->subpasswordrequired, "t") != 0)
appendPQExpBuffer(query, ", password_required = false");
if (pg_strcasecmp(subinfo->suborigin, LOGICALREP_ORIGIN_ANY) != 0)
appendPQExpBuffer(query, ", origin = %s", subinfo->suborigin);

appendPQExpBufferStr(query, ");\n");

Expand Down
9 changes: 5 additions & 4 deletions src/bin/pg_dump/pg_dump.h
Original file line number Diff line number Diff line change
Expand Up @@ -660,16 +660,17 @@ typedef struct _SubscriptionInfo
{
DumpableObject dobj;
const char *rolname;
char *subconninfo;
char *subslotname;
char *subbinary;
char *substream;
char *subtwophasestate;
char *subdisableonerr;
char *suborigin;
char *subpasswordrequired;
char *subrunasowner;
char *subconninfo;
char *subslotname;
char *subsynccommit;
char *subpublications;
char *subpasswordrequired;
char *suborigin;
} SubscriptionInfo;

/*
Expand Down

0 comments on commit 5ba4cc3

Please sign in to comment.