Skip to content

Commit 248c2d1

Browse files
committed
Refactor code converting a publication name List to a StringInfo
The existing get_publications_str() is renamed to GetPublicationsStr() and is moved to pg_subscription.c, so as it is possible to reuse it at two locations of the tablesync code where the same logic was duplicated. fetch_remote_table_info() was doing two List->StringInfo conversions when dealing with a server of version 15 or newer. The conversion happens only once now. This refactoring leads to less code overall. Author: Peter Smith Reviewed-by: Michael Paquier, Masahiko Sawada Discussion: https://postgr.es/m/CAHut+PtJMk4bKXqtpvqVy9ckknCgK9P6=FeG8zHF=6+Em_Snpw@mail.gmail.com
1 parent 1564339 commit 248c2d1

File tree

4 files changed

+58
-73
lines changed

4 files changed

+58
-73
lines changed

src/backend/catalog/pg_subscription.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,37 @@
3434

3535
static List *textarray_to_stringlist(ArrayType *textarray);
3636

37+
/*
38+
* Add a comma-separated list of publication names to the 'dest' string.
39+
*/
40+
void
41+
GetPublicationsStr(List *publications, StringInfo dest, bool quote_literal)
42+
{
43+
ListCell *lc;
44+
bool first = true;
45+
46+
Assert(publications != NIL);
47+
48+
foreach(lc, publications)
49+
{
50+
char *pubname = strVal(lfirst(lc));
51+
52+
if (first)
53+
first = false;
54+
else
55+
appendStringInfoString(dest, ", ");
56+
57+
if (quote_literal)
58+
appendStringInfoString(dest, quote_literal_cstr(pubname));
59+
else
60+
{
61+
appendStringInfoChar(dest, '"');
62+
appendStringInfoString(dest, pubname);
63+
appendStringInfoChar(dest, '"');
64+
}
65+
}
66+
}
67+
3768
/*
3869
* Fetch the subscription from the syscache.
3970
*/

src/backend/commands/subscriptioncmds.c

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -439,37 +439,6 @@ parse_subscription_options(ParseState *pstate, List *stmt_options,
439439
}
440440
}
441441

442-
/*
443-
* Add publication names from the list to a string.
444-
*/
445-
static void
446-
get_publications_str(List *publications, StringInfo dest, bool quote_literal)
447-
{
448-
ListCell *lc;
449-
bool first = true;
450-
451-
Assert(publications != NIL);
452-
453-
foreach(lc, publications)
454-
{
455-
char *pubname = strVal(lfirst(lc));
456-
457-
if (first)
458-
first = false;
459-
else
460-
appendStringInfoString(dest, ", ");
461-
462-
if (quote_literal)
463-
appendStringInfoString(dest, quote_literal_cstr(pubname));
464-
else
465-
{
466-
appendStringInfoChar(dest, '"');
467-
appendStringInfoString(dest, pubname);
468-
appendStringInfoChar(dest, '"');
469-
}
470-
}
471-
}
472-
473442
/*
474443
* Check that the specified publications are present on the publisher.
475444
*/
@@ -486,7 +455,7 @@ check_publications(WalReceiverConn *wrconn, List *publications)
486455
appendStringInfoString(cmd, "SELECT t.pubname FROM\n"
487456
" pg_catalog.pg_publication t WHERE\n"
488457
" t.pubname IN (");
489-
get_publications_str(publications, cmd, true);
458+
GetPublicationsStr(publications, cmd, true);
490459
appendStringInfoChar(cmd, ')');
491460

492461
res = walrcv_exec(wrconn, cmd->data, 1, tableRow);
@@ -523,7 +492,7 @@ check_publications(WalReceiverConn *wrconn, List *publications)
523492
/* Prepare the list of non-existent publication(s) for error message. */
524493
StringInfo pubnames = makeStringInfo();
525494

526-
get_publications_str(publicationsCopy, pubnames, false);
495+
GetPublicationsStr(publicationsCopy, pubnames, false);
527496
ereport(WARNING,
528497
errcode(ERRCODE_UNDEFINED_OBJECT),
529498
errmsg_plural("publication %s does not exist on the publisher",
@@ -2151,7 +2120,7 @@ check_publications_origin(WalReceiverConn *wrconn, List *publications,
21512120
" JOIN pg_subscription_rel PS ON (GPT.relid = PS.srrelid),\n"
21522121
" pg_class C JOIN pg_namespace N ON (N.oid = C.relnamespace)\n"
21532122
"WHERE C.oid = GPT.relid AND P.pubname IN (");
2154-
get_publications_str(publications, &cmd, true);
2123+
GetPublicationsStr(publications, &cmd, true);
21552124
appendStringInfoString(&cmd, ")\n");
21562125

21572126
/*
@@ -2208,7 +2177,7 @@ check_publications_origin(WalReceiverConn *wrconn, List *publications,
22082177
StringInfo pubnames = makeStringInfo();
22092178

22102179
/* Prepare the list of publication(s) for warning message. */
2211-
get_publications_str(publist, pubnames, false);
2180+
GetPublicationsStr(publist, pubnames, false);
22122181
ereport(WARNING,
22132182
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
22142183
errmsg("subscription \"%s\" requested copy_data with origin = NONE but might copy data that had a different origin",
@@ -2243,17 +2212,17 @@ fetch_table_list(WalReceiverConn *wrconn, List *publications)
22432212
List *tablelist = NIL;
22442213
int server_version = walrcv_server_version(wrconn);
22452214
bool check_columnlist = (server_version >= 150000);
2215+
StringInfo pub_names = makeStringInfo();
22462216

22472217
initStringInfo(&cmd);
22482218

2219+
/* Build the pub_names comma-separated string. */
2220+
GetPublicationsStr(publications, pub_names, true);
2221+
22492222
/* Get the list of tables from the publisher. */
22502223
if (server_version >= 160000)
22512224
{
2252-
StringInfoData pub_names;
2253-
22542225
tableRow[2] = INT2VECTOROID;
2255-
initStringInfo(&pub_names);
2256-
get_publications_str(publications, &pub_names, true);
22572226

22582227
/*
22592228
* From version 16, we allowed passing multiple publications to the
@@ -2275,9 +2244,7 @@ fetch_table_list(WalReceiverConn *wrconn, List *publications)
22752244
" FROM pg_publication\n"
22762245
" WHERE pubname IN ( %s )) AS gpt\n"
22772246
" ON gpt.relid = c.oid\n",
2278-
pub_names.data);
2279-
2280-
pfree(pub_names.data);
2247+
pub_names->data);
22812248
}
22822249
else
22832250
{
@@ -2288,12 +2255,13 @@ fetch_table_list(WalReceiverConn *wrconn, List *publications)
22882255
if (check_columnlist)
22892256
appendStringInfoString(&cmd, ", t.attnames\n");
22902257

2291-
appendStringInfoString(&cmd, "FROM pg_catalog.pg_publication_tables t\n"
2292-
" WHERE t.pubname IN (");
2293-
get_publications_str(publications, &cmd, true);
2294-
appendStringInfoChar(&cmd, ')');
2258+
appendStringInfo(&cmd, "FROM pg_catalog.pg_publication_tables t\n"
2259+
" WHERE t.pubname IN ( %s )",
2260+
pub_names->data);
22952261
}
22962262

2263+
destroyStringInfo(pub_names);
2264+
22972265
res = walrcv_exec(wrconn, cmd.data, check_columnlist ? 3 : 2, tableRow);
22982266
pfree(cmd.data);
22992267

src/backend/replication/logical/tablesync.c

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ fetch_remote_table_info(char *nspname, char *relname,
802802
Oid qualRow[] = {TEXTOID};
803803
bool isnull;
804804
int natt;
805-
ListCell *lc;
805+
StringInfo pub_names = NULL;
806806
Bitmapset *included_cols = NULL;
807807

808808
lrel->nspname = nspname;
@@ -856,15 +856,10 @@ fetch_remote_table_info(char *nspname, char *relname,
856856
WalRcvExecResult *pubres;
857857
TupleTableSlot *tslot;
858858
Oid attrsRow[] = {INT2VECTOROID};
859-
StringInfoData pub_names;
860859

861-
initStringInfo(&pub_names);
862-
foreach(lc, MySubscription->publications)
863-
{
864-
if (foreach_current_index(lc) > 0)
865-
appendStringInfoString(&pub_names, ", ");
866-
appendStringInfoString(&pub_names, quote_literal_cstr(strVal(lfirst(lc))));
867-
}
860+
/* Build the pub_names comma-separated string. */
861+
pub_names = makeStringInfo();
862+
GetPublicationsStr(MySubscription->publications, pub_names, true);
868863

869864
/*
870865
* Fetch info about column lists for the relation (from all the
@@ -881,7 +876,7 @@ fetch_remote_table_info(char *nspname, char *relname,
881876
" WHERE gpt.relid = %u AND c.oid = gpt.relid"
882877
" AND p.pubname IN ( %s )",
883878
lrel->remoteid,
884-
pub_names.data);
879+
pub_names->data);
885880

886881
pubres = walrcv_exec(LogRepWorkerWalRcvConn, cmd.data,
887882
lengthof(attrsRow), attrsRow);
@@ -936,8 +931,6 @@ fetch_remote_table_info(char *nspname, char *relname,
936931
ExecDropSingleTupleTableSlot(tslot);
937932

938933
walrcv_clear_result(pubres);
939-
940-
pfree(pub_names.data);
941934
}
942935

943936
/*
@@ -1039,19 +1032,8 @@ fetch_remote_table_info(char *nspname, char *relname,
10391032
*/
10401033
if (walrcv_server_version(LogRepWorkerWalRcvConn) >= 150000)
10411034
{
1042-
StringInfoData pub_names;
1043-
1044-
/* Build the pubname list. */
1045-
initStringInfo(&pub_names);
1046-
foreach_node(String, pubstr, MySubscription->publications)
1047-
{
1048-
char *pubname = strVal(pubstr);
1049-
1050-
if (foreach_current_index(pubstr) > 0)
1051-
appendStringInfoString(&pub_names, ", ");
1052-
1053-
appendStringInfoString(&pub_names, quote_literal_cstr(pubname));
1054-
}
1035+
/* Reuse the already-built pub_names. */
1036+
Assert(pub_names != NULL);
10551037

10561038
/* Check for row filters. */
10571039
resetStringInfo(&cmd);
@@ -1062,7 +1044,7 @@ fetch_remote_table_info(char *nspname, char *relname,
10621044
" WHERE gpt.relid = %u"
10631045
" AND p.pubname IN ( %s )",
10641046
lrel->remoteid,
1065-
pub_names.data);
1047+
pub_names->data);
10661048

10671049
res = walrcv_exec(LogRepWorkerWalRcvConn, cmd.data, 1, qualRow);
10681050

@@ -1101,6 +1083,7 @@ fetch_remote_table_info(char *nspname, char *relname,
11011083
ExecDropSingleTupleTableSlot(slot);
11021084

11031085
walrcv_clear_result(res);
1086+
destroyStringInfo(pub_names);
11041087
}
11051088

11061089
pfree(cmd.data);

src/include/catalog/pg_subscription.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "access/xlogdefs.h"
2121
#include "catalog/genbki.h"
2222
#include "catalog/pg_subscription_d.h"
23-
23+
#include "lib/stringinfo.h"
2424
#include "nodes/pg_list.h"
2525

2626
/*
@@ -180,4 +180,7 @@ extern void DisableSubscription(Oid subid);
180180

181181
extern int CountDBSubscriptions(Oid dbid);
182182

183+
extern void GetPublicationsStr(List *publications, StringInfo dest,
184+
bool quote_literal);
185+
183186
#endif /* PG_SUBSCRIPTION_H */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy