diff options
author | Tomas Vondra | 2022-03-25 23:45:21 +0000 |
---|---|---|
committer | Tomas Vondra | 2022-03-26 00:01:27 +0000 |
commit | 923def9a533a7d986acfb524139d8b9e5466d0a5 (patch) | |
tree | b6ce8d5bfe8d932e3cc89e52aba68519558e8033 /src/bin/psql/describe.c | |
parent | 05843b1aa49df2ecc9b97c693b755bd1b6f856a9 (diff) |
Allow specifying column lists for logical replication
This allows specifying an optional column list when adding a table to
logical replication. The column list may be specified after the table
name, enclosed in parentheses. Columns not included in this list are not
sent to the subscriber, allowing the schema on the subscriber to be a
subset of the publisher schema.
For UPDATE/DELETE publications, the column list needs to cover all
REPLICA IDENTITY columns. For INSERT publications, the column list is
arbitrary and may omit some REPLICA IDENTITY columns. Furthermore, if
the table uses REPLICA IDENTITY FULL, column list is not allowed.
The column list can contain only simple column references. Complex
expressions, function calls etc. are not allowed. This restriction could
be relaxed in the future.
During the initial table synchronization, only columns included in the
column list are copied to the subscriber. If the subscription has
several publications, containing the same table with different column
lists, columns specified in any of the lists will be copied.
This means all columns are replicated if the table has no column list
at all (which is treated as column list with all columns), or when of
the publications is defined as FOR ALL TABLES (possibly IN SCHEMA that
matches the schema of the table).
For partitioned tables, publish_via_partition_root determines whether
the column list for the root or the leaf relation will be used. If the
parameter is 'false' (the default), the list defined for the leaf
relation is used. Otherwise, the column list for the root partition
will be used.
Psql commands \dRp+ and \d <table-name> now display any column lists.
Author: Tomas Vondra, Alvaro Herrera, Rahila Syed
Reviewed-by: Peter Eisentraut, Alvaro Herrera, Vignesh C, Ibrar Ahmed,
Amit Kapila, Hou zj, Peter Smith, Wang wei, Tang, Shi yu
Discussion: https://postgr.es/m/CAH2L28vddB_NFdRVpuyRBJEBWjz4BSyTB=_ektNRH8NJ1jf95g@mail.gmail.com
Diffstat (limited to 'src/bin/psql/describe.c')
-rw-r--r-- | src/bin/psql/describe.c | 44 |
1 files changed, 37 insertions, 7 deletions
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index b8a532a45f7..4dddf087893 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -2960,6 +2960,7 @@ describeOneTableDetails(const char *schemaname, printfPQExpBuffer(&buf, "SELECT pubname\n" " , NULL\n" + " , NULL\n" "FROM pg_catalog.pg_publication p\n" " JOIN pg_catalog.pg_publication_namespace pn ON p.oid = pn.pnpubid\n" " JOIN pg_catalog.pg_class pc ON pc.relnamespace = pn.pnnspid\n" @@ -2967,6 +2968,12 @@ describeOneTableDetails(const char *schemaname, "UNION\n" "SELECT pubname\n" " , pg_get_expr(pr.prqual, c.oid)\n" + " , (CASE WHEN pr.prattrs IS NOT NULL THEN\n" + " (SELECT string_agg(attname, ', ')\n" + " FROM pg_catalog.generate_series(0, pg_catalog.array_upper(pr.prattrs::pg_catalog.int2[], 1)) s,\n" + " pg_catalog.pg_attribute\n" + " WHERE attrelid = pr.prrelid AND attnum = prattrs[s])\n" + " ELSE NULL END) " "FROM pg_catalog.pg_publication p\n" " JOIN pg_catalog.pg_publication_rel pr ON p.oid = pr.prpubid\n" " JOIN pg_catalog.pg_class c ON c.oid = pr.prrelid\n" @@ -2974,6 +2981,7 @@ describeOneTableDetails(const char *schemaname, "UNION\n" "SELECT pubname\n" " , NULL\n" + " , NULL\n" "FROM pg_catalog.pg_publication p\n" "WHERE p.puballtables AND pg_catalog.pg_relation_is_publishable('%s')\n" "ORDER BY 1;", @@ -2984,12 +2992,14 @@ describeOneTableDetails(const char *schemaname, printfPQExpBuffer(&buf, "SELECT pubname\n" " , NULL\n" + " , NULL\n" "FROM pg_catalog.pg_publication p\n" "JOIN pg_catalog.pg_publication_rel pr ON p.oid = pr.prpubid\n" "WHERE pr.prrelid = '%s'\n" "UNION ALL\n" "SELECT pubname\n" " , NULL\n" + " , NULL\n" "FROM pg_catalog.pg_publication p\n" "WHERE p.puballtables AND pg_catalog.pg_relation_is_publishable('%s')\n" "ORDER BY 1;", @@ -3011,6 +3021,11 @@ describeOneTableDetails(const char *schemaname, printfPQExpBuffer(&buf, " \"%s\"", PQgetvalue(result, i, 0)); + /* column list (if any) */ + if (!PQgetisnull(result, i, 2)) + appendPQExpBuffer(&buf, " (%s)", + PQgetvalue(result, i, 2)); + /* row filter (if any) */ if (!PQgetisnull(result, i, 1)) appendPQExpBuffer(&buf, " WHERE %s", @@ -5979,7 +5994,7 @@ listPublications(const char *pattern) */ static bool addFooterToPublicationDesc(PQExpBuffer buf, char *footermsg, - bool singlecol, printTableContent *cont) + bool as_schema, printTableContent *cont) { PGresult *res; int count = 0; @@ -5996,15 +6011,19 @@ addFooterToPublicationDesc(PQExpBuffer buf, char *footermsg, for (i = 0; i < count; i++) { - if (!singlecol) + if (as_schema) + printfPQExpBuffer(buf, " \"%s\"", PQgetvalue(res, i, 0)); + else { printfPQExpBuffer(buf, " \"%s.%s\"", PQgetvalue(res, i, 0), PQgetvalue(res, i, 1)); + + if (!PQgetisnull(res, i, 3)) + appendPQExpBuffer(buf, " (%s)", PQgetvalue(res, i, 3)); + if (!PQgetisnull(res, i, 2)) appendPQExpBuffer(buf, " WHERE %s", PQgetvalue(res, i, 2)); } - else - printfPQExpBuffer(buf, " \"%s\"", PQgetvalue(res, i, 0)); printTableAddFooter(cont, buf->data); } @@ -6155,11 +6174,22 @@ describePublications(const char *pattern) printfPQExpBuffer(&buf, "SELECT n.nspname, c.relname"); if (pset.sversion >= 150000) + { appendPQExpBufferStr(&buf, ", pg_get_expr(pr.prqual, c.oid)"); + appendPQExpBufferStr(&buf, + ", (CASE WHEN pr.prattrs IS NOT NULL THEN\n" + " pg_catalog.array_to_string(" + " ARRAY(SELECT attname\n" + " FROM\n" + " pg_catalog.generate_series(0, pg_catalog.array_upper(pr.prattrs::pg_catalog.int2[], 1)) s,\n" + " pg_catalog.pg_attribute\n" + " WHERE attrelid = c.oid AND attnum = prattrs[s]), ', ')\n" + " ELSE NULL END)"); + } else appendPQExpBufferStr(&buf, - ", NULL"); + ", NULL, NULL"); appendPQExpBuffer(&buf, "\nFROM pg_catalog.pg_class c,\n" " pg_catalog.pg_namespace n,\n" @@ -6189,9 +6219,9 @@ describePublications(const char *pattern) if (!puballsequences) { - /* Get the tables for the specified publication */ + /* Get the sequences for the specified publication */ printfPQExpBuffer(&buf, - "SELECT n.nspname, c.relname, NULL\n" + "SELECT n.nspname, c.relname, NULL, NULL\n" "FROM pg_catalog.pg_class c,\n" " pg_catalog.pg_namespace n,\n" " pg_catalog.pg_publication_rel pr\n" |