@@ -3742,8 +3742,8 @@ dumpPolicy(Archive *fout, PolicyInfo *polinfo)
3742
3742
* getPublications
3743
3743
* get information about publications
3744
3744
*/
3745
- void
3746
- getPublications(Archive *fout)
3745
+ PublicationInfo *
3746
+ getPublications(Archive *fout, int *numPublications )
3747
3747
{
3748
3748
DumpOptions *dopt = fout->dopt;
3749
3749
PQExpBuffer query;
@@ -3762,7 +3762,10 @@ getPublications(Archive *fout)
3762
3762
ntups;
3763
3763
3764
3764
if (dopt->no_publications || fout->remoteVersion < 100000)
3765
- return;
3765
+ {
3766
+ *numPublications = 0;
3767
+ return NULL;
3768
+ }
3766
3769
3767
3770
query = createPQExpBuffer();
3768
3771
@@ -3830,6 +3833,9 @@ getPublications(Archive *fout)
3830
3833
PQclear(res);
3831
3834
3832
3835
destroyPQExpBuffer(query);
3836
+
3837
+ *numPublications = ntups;
3838
+ return pubinfo;
3833
3839
}
3834
3840
3835
3841
/*
@@ -3935,7 +3941,8 @@ getPublicationTables(Archive *fout, TableInfo tblinfo[], int numTables)
3935
3941
DumpOptions *dopt = fout->dopt;
3936
3942
int i_tableoid;
3937
3943
int i_oid;
3938
- int i_pubname;
3944
+ int i_prpubid;
3945
+ int i_prrelid;
3939
3946
int i,
3940
3947
j,
3941
3948
ntups;
@@ -3945,12 +3952,39 @@ getPublicationTables(Archive *fout, TableInfo tblinfo[], int numTables)
3945
3952
3946
3953
query = createPQExpBuffer();
3947
3954
3948
- for (i = 0; i < numTables; i++)
3955
+ /* Collect all publication membership info. */
3956
+ appendPQExpBufferStr(query,
3957
+ "SELECT tableoid, oid, prpubid, prrelid "
3958
+ "FROM pg_catalog.pg_publication_rel");
3959
+ res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3960
+
3961
+ ntups = PQntuples(res);
3962
+
3963
+ i_tableoid = PQfnumber(res, "tableoid");
3964
+ i_oid = PQfnumber(res, "oid");
3965
+ i_prpubid = PQfnumber(res, "prpubid");
3966
+ i_prrelid = PQfnumber(res, "prrelid");
3967
+
3968
+ /* this allocation may be more than we need */
3969
+ pubrinfo = pg_malloc(ntups * sizeof(PublicationRelInfo));
3970
+ j = 0;
3971
+
3972
+ for (i = 0; i < ntups; i++)
3949
3973
{
3950
- TableInfo *tbinfo = &tblinfo[i];
3974
+ Oid prpubid = atooid(PQgetvalue(res, i, i_prpubid));
3975
+ Oid prrelid = atooid(PQgetvalue(res, i, i_prrelid));
3976
+ PublicationInfo *pubinfo;
3977
+ TableInfo *tbinfo;
3951
3978
3952
- /* Only plain tables can be aded to publications. */
3953
- if (tbinfo->relkind != RELKIND_RELATION)
3979
+ /*
3980
+ * Ignore any entries for which we aren't interested in either the
3981
+ * publication or the rel.
3982
+ */
3983
+ pubinfo = findPublicationByOid(prpubid);
3984
+ if (pubinfo == NULL)
3985
+ continue;
3986
+ tbinfo = findTableByOid(prrelid);
3987
+ if (tbinfo == NULL)
3954
3988
continue;
3955
3989
3956
3990
/*
@@ -3960,56 +3994,24 @@ getPublicationTables(Archive *fout, TableInfo tblinfo[], int numTables)
3960
3994
if (!(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
3961
3995
continue;
3962
3996
3963
- if (g_verbose)
3964
- write_msg(NULL, "reading publication membership for table \"%s.%s\"\n",
3965
- tbinfo->dobj.namespace->dobj.name,
3966
- tbinfo->dobj.name);
3967
-
3968
- resetPQExpBuffer(query);
3969
-
3970
- /* Get the publication membership for the table. */
3971
- appendPQExpBuffer(query,
3972
- "SELECT pr.tableoid, pr.oid, p.pubname "
3973
- "FROM pg_publication_rel pr, pg_publication p "
3974
- "WHERE pr.prrelid = '%u'"
3975
- " AND p.oid = pr.prpubid",
3976
- tbinfo->dobj.catId.oid);
3977
- res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3978
-
3979
- ntups = PQntuples(res);
3980
-
3981
- if (ntups == 0)
3982
- {
3983
- /*
3984
- * Table is not member of any publications. Clean up and return.
3985
- */
3986
- PQclear(res);
3987
- continue;
3988
- }
3989
-
3990
- i_tableoid = PQfnumber(res, "tableoid");
3991
- i_oid = PQfnumber(res, "oid");
3992
- i_pubname = PQfnumber(res, "pubname");
3997
+ /* OK, make a DumpableObject for this relationship */
3998
+ pubrinfo[j].dobj.objType = DO_PUBLICATION_REL;
3999
+ pubrinfo[j].dobj.catId.tableoid =
4000
+ atooid(PQgetvalue(res, i, i_tableoid));
4001
+ pubrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4002
+ AssignDumpId(&pubrinfo[j].dobj);
4003
+ pubrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
4004
+ pubrinfo[j].dobj.name = tbinfo->dobj.name;
4005
+ pubrinfo[j].publication = pubinfo;
4006
+ pubrinfo[j].pubtable = tbinfo;
3993
4007
3994
- pubrinfo = pg_malloc(ntups * sizeof(PublicationRelInfo));
4008
+ /* Decide whether we want to dump it */
4009
+ selectDumpablePublicationTable(&(pubrinfo[j].dobj), fout);
3995
4010
3996
- for (j = 0; j < ntups; j++)
3997
- {
3998
- pubrinfo[j].dobj.objType = DO_PUBLICATION_REL;
3999
- pubrinfo[j].dobj.catId.tableoid =
4000
- atooid(PQgetvalue(res, j, i_tableoid));
4001
- pubrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
4002
- AssignDumpId(&pubrinfo[j].dobj);
4003
- pubrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
4004
- pubrinfo[j].dobj.name = tbinfo->dobj.name;
4005
- pubrinfo[j].pubname = pg_strdup(PQgetvalue(res, j, i_pubname));
4006
- pubrinfo[j].pubtable = tbinfo;
4007
-
4008
- /* Decide whether we want to dump it */
4009
- selectDumpablePublicationTable(&(pubrinfo[j].dobj), fout);
4010
- }
4011
- PQclear(res);
4011
+ j++;
4012
4012
}
4013
+
4014
+ PQclear(res);
4013
4015
destroyPQExpBuffer(query);
4014
4016
}
4015
4017
@@ -4020,31 +4022,35 @@ getPublicationTables(Archive *fout, TableInfo tblinfo[], int numTables)
4020
4022
static void
4021
4023
dumpPublicationTable(Archive *fout, PublicationRelInfo *pubrinfo)
4022
4024
{
4025
+ PublicationInfo *pubinfo = pubrinfo->publication;
4023
4026
TableInfo *tbinfo = pubrinfo->pubtable;
4024
4027
PQExpBuffer query;
4025
4028
char *tag;
4026
4029
4027
4030
if (!(pubrinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
4028
4031
return;
4029
4032
4030
- tag = psprintf("%s %s", pubrinfo->pubname , tbinfo->dobj.name);
4033
+ tag = psprintf("%s %s", pubinfo->dobj.name , tbinfo->dobj.name);
4031
4034
4032
4035
query = createPQExpBuffer();
4033
4036
4034
4037
appendPQExpBuffer(query, "ALTER PUBLICATION %s ADD TABLE ONLY",
4035
- fmtId(pubrinfo->pubname ));
4038
+ fmtId(pubinfo->dobj.name ));
4036
4039
appendPQExpBuffer(query, " %s;\n",
4037
4040
fmtQualifiedDumpable(tbinfo));
4038
4041
4039
4042
/*
4040
- * There is no point in creating drop query as drop query as the drop is
4041
- * done by table drop.
4043
+ * There is no point in creating a drop query as the drop is done by table
4044
+ * drop. (If you think to change this, see also _printTocEntry().)
4045
+ * Although this object doesn't really have ownership as such, set the
4046
+ * owner field anyway to ensure that the command is run by the correct
4047
+ * role at restore time.
4042
4048
*/
4043
4049
ArchiveEntry(fout, pubrinfo->dobj.catId, pubrinfo->dobj.dumpId,
4044
4050
tag,
4045
4051
tbinfo->dobj.namespace->dobj.name,
4046
4052
NULL,
4047
- "" , false,
4053
+ pubinfo->rolname , false,
4048
4054
"PUBLICATION TABLE", SECTION_POST_DATA,
4049
4055
query->data, "", NULL,
4050
4056
NULL, 0,
0 commit comments