From 16d36b556d066480cda4dd170e13183aaf9d8173 Mon Sep 17 00:00:00 2001 From: "houzj.fnst" Date: Fri, 13 May 2022 11:35:14 +0800 Subject: [PATCH] extent pg_publication_tables Extend the pg_publication_tables view and pg_get_publication_tables function so that they can return the column list and row fitler of the table. It will make it easier for users and developers to fetch the column lists and row filters. --- doc/src/sgml/catalogs.sgml | 20 +++++++++++ src/backend/catalog/pg_publication.c | 52 ++++++++++++++++++++++++++++- src/backend/catalog/system_views.sql | 4 ++- src/backend/replication/logical/tablesync.c | 14 +++----- src/include/catalog/pg_proc.dat | 6 ++-- src/test/regress/expected/publication.out | 42 +++++++++++------------ src/test/regress/expected/rules.out | 6 ++-- 7 files changed, 106 insertions(+), 38 deletions(-) diff --git a/doc/src/sgml/catalogs.sgml b/doc/src/sgml/catalogs.sgml index a533a21..fd0c61b 100644 --- a/doc/src/sgml/catalogs.sgml +++ b/doc/src/sgml/catalogs.sgml @@ -11687,6 +11687,26 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx Name of table + + + + columnlist int2vector + (references pg_publication_rel.prattrs) + + + Column list of table + + + + + + rowfilter text + (references pg_publication_rel.prqual) + + + Row filter of table + + diff --git a/src/backend/catalog/pg_publication.c b/src/backend/catalog/pg_publication.c index e2c8bcb..3fe607e 100644 --- a/src/backend/catalog/pg_publication.c +++ b/src/backend/catalog/pg_publication.c @@ -1082,6 +1082,7 @@ get_publication_name(Oid pubid, bool missing_ok) Datum pg_get_publication_tables(PG_FUNCTION_ARGS) { +#define NUM_PUBLICATOIN_TABLES_ELEM 3 FuncCallContext *funcctx; char *pubname = text_to_cstring(PG_GETARG_TEXT_PP(0)); Publication *publication; @@ -1090,6 +1091,7 @@ pg_get_publication_tables(PG_FUNCTION_ARGS) /* stuff done only on the first call of the function */ if (SRF_IS_FIRSTCALL()) { + TupleDesc tupdesc; MemoryContext oldcontext; /* create a function context for cross-call persistence */ @@ -1136,6 +1138,16 @@ pg_get_publication_tables(PG_FUNCTION_ARGS) tables = filter_partitions(tables); } + /* Construct a tuple descriptor for the result rows. */ + tupdesc = CreateTemplateTupleDesc(NUM_PUBLICATOIN_TABLES_ELEM); + TupleDescInitEntry(tupdesc, (AttrNumber) 1, "relid", + OIDOID, -1, 0); + TupleDescInitEntry(tupdesc, (AttrNumber) 2, "attrs", + INT2VECTOROID, -1, 0); + TupleDescInitEntry(tupdesc, (AttrNumber) 3, "qual", + PG_NODE_TREEOID, -1, 0); + + funcctx->tuple_desc = BlessTupleDesc(tupdesc); funcctx->user_fctx = (void *) tables; MemoryContextSwitchTo(oldcontext); @@ -1147,9 +1159,47 @@ pg_get_publication_tables(PG_FUNCTION_ARGS) if (funcctx->call_cntr < list_length(tables)) { + HeapTuple pubtuple = NULL; + HeapTuple rettuple; Oid relid = list_nth_oid(tables, funcctx->call_cntr); + Datum values[NUM_PUBLICATOIN_TABLES_ELEM]; + bool nulls[NUM_PUBLICATOIN_TABLES_ELEM]; + + /* + * Form tuple with appropriate data. + */ + MemSet(nulls, 0, sizeof(nulls)); + MemSet(values, 0, sizeof(values)); + + publication = GetPublicationByName(pubname, false); + + values[0] = ObjectIdGetDatum(relid); + + pubtuple = SearchSysCacheCopy2(PUBLICATIONRELMAP, + ObjectIdGetDatum(relid), + ObjectIdGetDatum(publication->oid)); + + if (HeapTupleIsValid(pubtuple)) + { + /* Lookup the column list attribute. */ + values[1] = SysCacheGetAttr(PUBLICATIONRELMAP, pubtuple, + Anum_pg_publication_rel_prattrs, + &(nulls[1])); + + /* Null indicates no filter. */ + values[2] = SysCacheGetAttr(PUBLICATIONRELMAP, pubtuple, + Anum_pg_publication_rel_prqual, + &(nulls[2])); + } + else + { + nulls[1] = true; + nulls[2] = true; + } + + rettuple = heap_form_tuple(funcctx->tuple_desc, values, nulls); - SRF_RETURN_NEXT(funcctx, ObjectIdGetDatum(relid)); + SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(rettuple)); } SRF_RETURN_DONE(funcctx); diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql index 0fc614e..f758f15 100644 --- a/src/backend/catalog/system_views.sql +++ b/src/backend/catalog/system_views.sql @@ -368,7 +368,9 @@ CREATE VIEW pg_publication_tables AS SELECT P.pubname AS pubname, N.nspname AS schemaname, - C.relname AS tablename + C.relname AS tablename, + GPT.attrs AS columnlist, + pg_get_expr(GPT.qual, GPT.relid) AS rowfilter FROM pg_publication P, LATERAL pg_get_publication_tables(P.pubname) GPT, pg_class C JOIN pg_namespace N ON (N.oid = C.relnamespace) diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c index b03e0f5..994c7a0 100644 --- a/src/backend/replication/logical/tablesync.c +++ b/src/backend/replication/logical/tablesync.c @@ -795,15 +795,12 @@ fetch_remote_table_info(char *nspname, char *relname, resetStringInfo(&cmd); appendStringInfo(&cmd, "SELECT DISTINCT unnest" - " FROM pg_publication p" - " LEFT OUTER JOIN pg_publication_rel pr" - " ON (p.oid = pr.prpubid AND pr.prrelid = %u)" - " LEFT OUTER JOIN unnest(pr.prattrs) ON TRUE," + " FROM pg_publication p," " LATERAL pg_get_publication_tables(p.pubname) gpt" + " LEFT OUTER JOIN unnest(gpt.attrs) ON TRUE" " WHERE gpt.relid = %u" " AND p.pubname IN ( %s )", lrel->remoteid, - lrel->remoteid, pub_names.data); pubres = walrcv_exec(LogRepWorkerWalRcvConn, cmd.data, @@ -965,15 +962,12 @@ fetch_remote_table_info(char *nspname, char *relname, /* Check for row filters. */ resetStringInfo(&cmd); appendStringInfo(&cmd, - "SELECT DISTINCT pg_get_expr(pr.prqual, pr.prrelid)" - " FROM pg_publication p" - " LEFT OUTER JOIN pg_publication_rel pr" - " ON (p.oid = pr.prpubid AND pr.prrelid = %u)," + "SELECT DISTINCT pg_get_expr(gpt.qual, gpt.relid)" + " FROM pg_publication p," " LATERAL pg_get_publication_tables(p.pubname) gpt" " WHERE gpt.relid = %u" " AND p.pubname IN ( %s )", lrel->remoteid, - lrel->remoteid, pub_names.data); res = walrcv_exec(LogRepWorkerWalRcvConn, cmd.data, 1, qualRow); diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index babe16f..cf6235d 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -11675,9 +11675,9 @@ # publications { oid => '6119', descr => 'get OIDs of tables in a publication', proname => 'pg_get_publication_tables', prorows => '1000', proretset => 't', - provolatile => 's', prorettype => 'oid', proargtypes => 'text', - proallargtypes => '{text,oid}', proargmodes => '{i,o}', - proargnames => '{pubname,relid}', prosrc => 'pg_get_publication_tables' }, + provolatile => 's', prorettype => 'record', proargtypes => 'text', + proallargtypes => '{text,oid,int2vector,pg_node_tree}', proargmodes => '{i,o,o,o}', + proargnames => '{pubname,relid,attrs,qual}', prosrc => 'pg_get_publication_tables' }, { oid => '6121', descr => 'returns whether a relation can be part of a publication', proname => 'pg_relation_is_publishable', provolatile => 's', diff --git a/src/test/regress/expected/publication.out b/src/test/regress/expected/publication.out index 398c0f3..4ebafb3 100644 --- a/src/test/regress/expected/publication.out +++ b/src/test/regress/expected/publication.out @@ -1585,52 +1585,52 @@ CREATE TABLE sch2.tbl1_part1 PARTITION OF sch1.tbl1 FOR VALUES FROM (1) to (10); -- Schema publication that does not include the schema that has the parent table CREATE PUBLICATION pub FOR ALL TABLES IN SCHEMA sch2 WITH (PUBLISH_VIA_PARTITION_ROOT=1); SELECT * FROM pg_publication_tables; - pubname | schemaname | tablename ----------+------------+------------ - pub | sch2 | tbl1_part1 + pubname | schemaname | tablename | columnlist | rowfilter +---------+------------+------------+------------+----------- + pub | sch2 | tbl1_part1 | | (1 row) DROP PUBLICATION pub; -- Table publication that does not include the parent table CREATE PUBLICATION pub FOR TABLE sch2.tbl1_part1 WITH (PUBLISH_VIA_PARTITION_ROOT=1); SELECT * FROM pg_publication_tables; - pubname | schemaname | tablename ----------+------------+------------ - pub | sch2 | tbl1_part1 + pubname | schemaname | tablename | columnlist | rowfilter +---------+------------+------------+------------+----------- + pub | sch2 | tbl1_part1 | | (1 row) -- Table publication that includes both the parent table and the child table ALTER PUBLICATION pub ADD TABLE sch1.tbl1; SELECT * FROM pg_publication_tables; - pubname | schemaname | tablename ----------+------------+----------- - pub | sch1 | tbl1 + pubname | schemaname | tablename | columnlist | rowfilter +---------+------------+-----------+------------+----------- + pub | sch1 | tbl1 | | (1 row) DROP PUBLICATION pub; -- Schema publication that does not include the schema that has the parent table CREATE PUBLICATION pub FOR ALL TABLES IN SCHEMA sch2 WITH (PUBLISH_VIA_PARTITION_ROOT=0); SELECT * FROM pg_publication_tables; - pubname | schemaname | tablename ----------+------------+------------ - pub | sch2 | tbl1_part1 + pubname | schemaname | tablename | columnlist | rowfilter +---------+------------+------------+------------+----------- + pub | sch2 | tbl1_part1 | | (1 row) DROP PUBLICATION pub; -- Table publication that does not include the parent table CREATE PUBLICATION pub FOR TABLE sch2.tbl1_part1 WITH (PUBLISH_VIA_PARTITION_ROOT=0); SELECT * FROM pg_publication_tables; - pubname | schemaname | tablename ----------+------------+------------ - pub | sch2 | tbl1_part1 + pubname | schemaname | tablename | columnlist | rowfilter +---------+------------+------------+------------+----------- + pub | sch2 | tbl1_part1 | | (1 row) -- Table publication that includes both the parent table and the child table ALTER PUBLICATION pub ADD TABLE sch1.tbl1; SELECT * FROM pg_publication_tables; - pubname | schemaname | tablename ----------+------------+------------ - pub | sch2 | tbl1_part1 + pubname | schemaname | tablename | columnlist | rowfilter +---------+------------+------------+------------+----------- + pub | sch2 | tbl1_part1 | | (1 row) DROP PUBLICATION pub; @@ -1643,9 +1643,9 @@ CREATE TABLE sch1.tbl1_part3 (a int) PARTITION BY RANGE(a); ALTER TABLE sch1.tbl1 ATTACH PARTITION sch1.tbl1_part3 FOR VALUES FROM (20) to (30); CREATE PUBLICATION pub FOR ALL TABLES IN SCHEMA sch1 WITH (PUBLISH_VIA_PARTITION_ROOT=1); SELECT * FROM pg_publication_tables; - pubname | schemaname | tablename ----------+------------+----------- - pub | sch1 | tbl1 + pubname | schemaname | tablename | columnlist | rowfilter +---------+------------+-----------+------------+----------- + pub | sch1 | tbl1 | | (1 row) RESET client_min_messages; diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index 21effe8..c7f8300 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -1437,9 +1437,11 @@ pg_prepared_xacts| SELECT p.transaction, LEFT JOIN pg_database d ON ((p.dbid = d.oid))); pg_publication_tables| SELECT p.pubname, n.nspname AS schemaname, - c.relname AS tablename + c.relname AS tablename, + gpt.attrs AS columnlist, + pg_get_expr(gpt.qual, gpt.relid) AS rowfilter FROM pg_publication p, - LATERAL pg_get_publication_tables((p.pubname)::text) gpt(relid), + LATERAL pg_get_publication_tables((p.pubname)::text) gpt(relid, attrs, qual), (pg_class c JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (c.oid = gpt.relid); -- 2.7.2.windows.1