@@ -3785,8 +3785,8 @@ dumpPolicy(Archive *fout, PolicyInfo *polinfo)
3785
3785
* getPublications
3786
3786
* get information about publications
3787
3787
*/
3788
- void
3789
- getPublications(Archive *fout)
3788
+ PublicationInfo *
3789
+ getPublications(Archive *fout, int *numPublications )
3790
3790
{
3791
3791
DumpOptions *dopt = fout->dopt;
3792
3792
PQExpBuffer query;
@@ -3805,7 +3805,10 @@ getPublications(Archive *fout)
3805
3805
ntups;
3806
3806
3807
3807
if (dopt->no_publications || fout->remoteVersion < 100000)
3808
- return;
3808
+ {
3809
+ *numPublications = 0;
3810
+ return NULL;
3811
+ }
3809
3812
3810
3813
query = createPQExpBuffer();
3811
3814
@@ -3873,6 +3876,9 @@ getPublications(Archive *fout)
3873
3876
PQclear(res);
3874
3877
3875
3878
destroyPQExpBuffer(query);
3879
+
3880
+ *numPublications = ntups;
3881
+ return pubinfo;
3876
3882
}
3877
3883
3878
3884
/*
@@ -3976,7 +3982,8 @@ getPublicationTables(Archive *fout, TableInfo tblinfo[], int numTables)
3976
3982
DumpOptions *dopt = fout->dopt;
3977
3983
int i_tableoid;
3978
3984
int i_oid;
3979
- int i_pubname;
3985
+ int i_prpubid;
3986
+ int i_prrelid;
3980
3987
int i,
3981
3988
j,
3982
3989
ntups;
@@ -3986,12 +3993,39 @@ getPublicationTables(Archive *fout, TableInfo tblinfo[], int numTables)
3986
3993
3987
3994
query = createPQExpBuffer();
3988
3995
3989
- for (i = 0; i < numTables; i++)
3996
+ /* Collect all publication membership info. */
3997
+ appendPQExpBufferStr(query,
3998
+ "SELECT tableoid, oid, prpubid, prrelid "
3999
+ "FROM pg_catalog.pg_publication_rel");
4000
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4001
+
4002
+ ntups = PQntuples(res);
4003
+
4004
+ i_tableoid = PQfnumber(res, "tableoid");
4005
+ i_oid = PQfnumber(res, "oid");
4006
+ i_prpubid = PQfnumber(res, "prpubid");
4007
+ i_prrelid = PQfnumber(res, "prrelid");
4008
+
4009
+ /* this allocation may be more than we need */
4010
+ pubrinfo = pg_malloc(ntups * sizeof(PublicationRelInfo));
4011
+ j = 0;
4012
+
4013
+ for (i = 0; i < ntups; i++)
3990
4014
{
3991
- TableInfo *tbinfo = &tblinfo[i];
4015
+ Oid prpubid = atooid(PQgetvalue(res, i, i_prpubid));
4016
+ Oid prrelid = atooid(PQgetvalue(res, i, i_prrelid));
4017
+ PublicationInfo *pubinfo;
4018
+ TableInfo *tbinfo;
3992
4019
3993
- /* Only plain tables can be aded to publications. */
3994
- if (tbinfo->relkind != RELKIND_RELATION)
4020
+ /*
4021
+ * Ignore any entries for which we aren't interested in either the
4022
+ * publication or the rel.
4023
+ */
4024
+ pubinfo = findPublicationByOid(prpubid);
4025
+ if (pubinfo == NULL)
4026
+ continue;
4027
+ tbinfo = findTableByOid(prrelid);
4028
+ if (tbinfo == NULL)
3995
4029
continue;
3996
4030
3997
4031
/*
@@ -4001,55 +4035,24 @@ getPublicationTables(Archive *fout, TableInfo tblinfo[], int numTables)
4001
4035
if (!(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
4002
4036
continue;
4003
4037
4004
- pg_log_info("reading publication membership for table \"%s.%s\"",
4005
- tbinfo->dobj.namespace->dobj.name,
4006
- tbinfo->dobj.name);
4007
-
4008
- resetPQExpBuffer(query);
4009
-
4010
- /* Get the publication membership for the table. */
4011
- appendPQExpBuffer(query,
4012
- "SELECT pr.tableoid, pr.oid, p.pubname "
4013
- "FROM pg_publication_rel pr, pg_publication p "
4014
- "WHERE pr.prrelid = '%u'"
4015
- " AND p.oid = pr.prpubid",
4016
- tbinfo->dobj.catId.oid);
4017
- res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4018
-
4019
- ntups = PQntuples(res);
4020
-
4021
- if (ntups == 0)
4022
- {
4023
- /*
4024
- * Table is not member of any publications. Clean up and return.
4025
- */
4026
- PQclear(res);
4027
- continue;
4028
- }
4029
-
4030
- i_tableoid = PQfnumber(res, "tableoid");
4031
- i_oid = PQfnumber(res, "oid");
4032
- i_pubname = PQfnumber(res, "pubname");
4038
+ /* OK, make a DumpableObject for this relationship */
4039
+ pubrinfo[j].dobj.objType = DO_PUBLICATION_REL;
4040
+ pubrinfo[j].dobj.catId.tableoid =
4041
+ atooid(PQgetvalue(res, i, i_tableoid));
4042
+ pubrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4043
+ AssignDumpId(&pubrinfo[j].dobj);
4044
+ pubrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
4045
+ pubrinfo[j].dobj.name = tbinfo->dobj.name;
4046
+ pubrinfo[j].publication = pubinfo;
4047
+ pubrinfo[j].pubtable = tbinfo;
4033
4048
4034
- pubrinfo = pg_malloc(ntups * sizeof(PublicationRelInfo));
4049
+ /* Decide whether we want to dump it */
4050
+ selectDumpablePublicationTable(&(pubrinfo[j].dobj), fout);
4035
4051
4036
- for (j = 0; j < ntups; j++)
4037
- {
4038
- pubrinfo[j].dobj.objType = DO_PUBLICATION_REL;
4039
- pubrinfo[j].dobj.catId.tableoid =
4040
- atooid(PQgetvalue(res, j, i_tableoid));
4041
- pubrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
4042
- AssignDumpId(&pubrinfo[j].dobj);
4043
- pubrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
4044
- pubrinfo[j].dobj.name = tbinfo->dobj.name;
4045
- pubrinfo[j].pubname = pg_strdup(PQgetvalue(res, j, i_pubname));
4046
- pubrinfo[j].pubtable = tbinfo;
4047
-
4048
- /* Decide whether we want to dump it */
4049
- selectDumpablePublicationTable(&(pubrinfo[j].dobj), fout);
4050
- }
4051
- PQclear(res);
4052
+ j++;
4052
4053
}
4054
+
4055
+ PQclear(res);
4053
4056
destroyPQExpBuffer(query);
4054
4057
}
4055
4058
@@ -4060,29 +4063,34 @@ getPublicationTables(Archive *fout, TableInfo tblinfo[], int numTables)
4060
4063
static void
4061
4064
dumpPublicationTable(Archive *fout, PublicationRelInfo *pubrinfo)
4062
4065
{
4066
+ PublicationInfo *pubinfo = pubrinfo->publication;
4063
4067
TableInfo *tbinfo = pubrinfo->pubtable;
4064
4068
PQExpBuffer query;
4065
4069
char *tag;
4066
4070
4067
4071
if (!(pubrinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
4068
4072
return;
4069
4073
4070
- tag = psprintf("%s %s", pubrinfo->pubname , tbinfo->dobj.name);
4074
+ tag = psprintf("%s %s", pubinfo->dobj.name , tbinfo->dobj.name);
4071
4075
4072
4076
query = createPQExpBuffer();
4073
4077
4074
4078
appendPQExpBuffer(query, "ALTER PUBLICATION %s ADD TABLE ONLY",
4075
- fmtId(pubrinfo->pubname ));
4079
+ fmtId(pubinfo->dobj.name ));
4076
4080
appendPQExpBuffer(query, " %s;\n",
4077
4081
fmtQualifiedDumpable(tbinfo));
4078
4082
4079
4083
/*
4080
- * There is no point in creating drop query as the drop is done by table
4081
- * drop.
4084
+ * There is no point in creating a drop query as the drop is done by table
4085
+ * drop. (If you think to change this, see also _printTocEntry().)
4086
+ * Although this object doesn't really have ownership as such, set the
4087
+ * owner field anyway to ensure that the command is run by the correct
4088
+ * role at restore time.
4082
4089
*/
4083
4090
ArchiveEntry(fout, pubrinfo->dobj.catId, pubrinfo->dobj.dumpId,
4084
4091
ARCHIVE_OPTS(.tag = tag,
4085
4092
.namespace = tbinfo->dobj.namespace->dobj.name,
4093
+ .owner = pubinfo->rolname,
4086
4094
.description = "PUBLICATION TABLE",
4087
4095
.section = SECTION_POST_DATA,
4088
4096
.createStmt = query->data));
0 commit comments