Skip to content

Commit a2141c4

Browse files
committed
Tweak publication fetching in psql
Viewing a table with \d in psql also shows the publications at table is in. If a publication is concurrently dropped, this shows an error, because the view pg_publication_tables internally uses pg_get_publication_tables(), which uses a catalog snapshot. This can be particularly annoying if a for-all-tables publication is concurrently dropped. To avoid that, write the query in psql differently. Expose the function pg_relation_is_publishable() to SQL and write the query using that. That still has a risk of being affected by concurrent catalog changes, but in this case it would be a table drop that causes problems, and then the psql \d command wouldn't be interesting anymore anyway. Reported-by: Tom Lane <tgl@sss.pgh.pa.us>
1 parent 20d7d68 commit a2141c4

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

src/backend/catalog/pg_publication.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,30 @@ is_publishable_class(Oid relid, Form_pg_class reltuple)
105105
relid >= FirstNormalObjectId;
106106
}
107107

108+
109+
/*
110+
* SQL-callable variant of the above
111+
*
112+
* This returns null when the relation does not exist. This is intended to be
113+
* used for example in psql to avoid gratuitous errors when there are
114+
* concurrent catalog changes.
115+
*/
116+
Datum
117+
pg_relation_is_publishable(PG_FUNCTION_ARGS)
118+
{
119+
Oid relid = PG_GETARG_OID(0);
120+
HeapTuple tuple;
121+
bool result;
122+
123+
tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
124+
if (!tuple)
125+
PG_RETURN_NULL();
126+
result = is_publishable_class(relid, (Form_pg_class) GETSTRUCT(tuple));
127+
ReleaseSysCache(tuple);
128+
PG_RETURN_BOOL(result);
129+
}
130+
131+
108132
/*
109133
* Insert new publication / relation mapping.
110134
*/

src/bin/psql/describe.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,12 +2536,16 @@ describeOneTableDetails(const char *schemaname,
25362536
if (pset.sversion >= 100000)
25372537
{
25382538
printfPQExpBuffer(&buf,
2539-
"SELECT pub.pubname\n"
2540-
" FROM pg_catalog.pg_publication pub,\n"
2541-
" pg_catalog.pg_get_publication_tables(pub.pubname)\n"
2542-
"WHERE relid = '%s'\n"
2539+
"SELECT pubname\n"
2540+
"FROM pg_catalog.pg_publication p\n"
2541+
"JOIN pg_catalog.pg_publication_rel pr ON p.oid = pr.prpubid\n"
2542+
"WHERE pr.prrelid = '%s'\n"
2543+
"UNION ALL\n"
2544+
"SELECT pubname\n"
2545+
"FROM pg_catalog.pg_publication p\n"
2546+
"WHERE p.puballtables AND pg_relation_is_publishable('%s')\n"
25432547
"ORDER BY 1;",
2544-
oid);
2548+
oid, oid);
25452549

25462550
result = PSQLexec(buf.data);
25472551
if (!result)

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 201706201
56+
#define CATALOG_VERSION_NO 201706202
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5436,6 +5436,8 @@ DESCR("get progress for all replication origins");
54365436
/* publications */
54375437
DATA(insert OID = 6119 ( pg_get_publication_tables PGNSP PGUID 12 1 1000 0 0 f f f f t t s s 1 0 26 "25" "{25,26}" "{i,o}" "{pubname,relid}" _null_ _null_ pg_get_publication_tables _null_ _null_ _null_ ));
54385438
DESCR("get OIDs of tables in a publication");
5439+
DATA(insert OID = 6121 ( pg_relation_is_publishable PGNSP PGUID 12 1 0 0 0 f f f f t f s s 1 0 16 "2205" _null_ _null_ _null_ _null_ _null_ pg_relation_is_publishable _null_ _null_ _null_ ));
5440+
DESCR("returns whether a relation can be part of a publication");
54395441

54405442
/* rls */
54415443
DATA(insert OID = 3298 ( row_security_active PGNSP PGUID 12 1 0 0 0 f f f f t f s s 1 0 16 "26" _null_ _null_ _null_ _null_ _null_ row_security_active _null_ _null_ _null_ ));

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