diff --git a/src/backend/replication/pgoutput/pgoutput.c b/src/backend/replication/pgoutput/pgoutput.c index f68348dcf4..d51975025c 100644 --- a/src/backend/replication/pgoutput/pgoutput.c +++ b/src/backend/replication/pgoutput/pgoutput.c @@ -94,7 +94,8 @@ typedef struct RelationSyncEntry /* * Did we send the schema? If ancestor relid is set, its schema must also - * have been sent for this to be true. + * have been sent and the map to convert the relation's tuples into the + * ancestor's format created before this can be set to be true. */ bool schema_sent; List *streamed_txns; /* streamed toplevel transactions with this @@ -440,7 +441,10 @@ maybe_send_schema(LogicalDecodingContext *ctx, if (schema_sent) return; - /* If needed, send the ancestor's schema first. */ + /* + * If needed, send the ancestor's schema first. Also, set the map to + * convert leaf partition tuples into the ancestor's format. + */ if (relentry->publish_as_relid != RelationGetRelid(relation)) { Relation ancestor = RelationIdGetRelation(relentry->publish_as_relid); @@ -448,12 +452,26 @@ maybe_send_schema(LogicalDecodingContext *ctx, TupleDesc outdesc = RelationGetDescr(ancestor); MemoryContext oldctx; + send_relation_and_attrs(ancestor, xid, ctx); + /* Map must live as long as the session does. */ oldctx = MemoryContextSwitchTo(CacheMemoryContext); - relentry->map = convert_tuples_by_name(CreateTupleDescCopy(indesc), - CreateTupleDescCopy(outdesc)); + + /* + * Make copies of the TupleDescs that will live as long as the map + * does before putting into the map. + */ + indesc = CreateTupleDescCopy(indesc); + outdesc = CreateTupleDescCopy(outdesc); + relentry->map = convert_tuples_by_name(indesc, outdesc); + if (relentry->map == NULL) + { + /* Map not necessary, so free the TupleDescs too. */ + FreeTupleDesc(indesc); + FreeTupleDesc(outdesc); + } + MemoryContextSwitchTo(oldctx); - send_relation_and_attrs(ancestor, xid, ctx); RelationClose(ancestor); } @@ -1190,13 +1208,25 @@ rel_sync_cache_relation_cb(Datum arg, Oid relid) HASH_FIND, NULL); /* - * Reset schema sent status as the relation definition may have changed. + * Reset schema sent status as the relation definition may have changed, + * also freeing any objects that depended on the earlier definition. */ if (entry != NULL) { entry->schema_sent = false; list_free(entry->streamed_txns); entry->streamed_txns = NIL; + if (entry->map) + { + /* + * Must free the TupleDescs contained in the map explicitly, + * because free_conversion_map() doesn't. + */ + FreeTupleDesc(entry->map->indesc); + FreeTupleDesc(entry->map->outdesc); + free_conversion_map(entry->map); + } + entry->map = NULL; } } diff --git a/src/test/subscription/t/013_partition.pl b/src/test/subscription/t/013_partition.pl index 6daf8daa3c..9de01017be 100644 --- a/src/test/subscription/t/013_partition.pl +++ b/src/test/subscription/t/013_partition.pl @@ -6,7 +6,7 @@ use strict; use warnings; use PostgresNode; use TestLib; -use Test::More tests => 54; +use Test::More tests => 56; # setup @@ -624,3 +624,29 @@ is($result, qq(), 'truncate of tab3 replicated'); $result = $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab3_1"); is($result, qq(), 'truncate of tab3_1 replicated'); + +# check that the map to convert tuples from leaf partition to the root +# table is correctly rebuilt when a new column is added +$node_publisher->safe_psql('postgres', + "ALTER TABLE tab2 DROP b, ADD COLUMN c text DEFAULT 'pub_tab2', ADD b text"); +$node_publisher->safe_psql('postgres', + "INSERT INTO tab2 (a, b) VALUES (1, 'xxx'), (3, 'yyy'), (5, 'zzz')"); +$node_publisher->safe_psql('postgres', + "INSERT INTO tab2 (a, b, c) VALUES (6, 'aaa', 'xxx_c')"); + +$node_publisher->wait_for_catchup('sub_viaroot'); +$node_publisher->wait_for_catchup('sub2'); + +$result = $node_subscriber1->safe_psql('postgres', + "SELECT c, a, b FROM tab2 ORDER BY 1, 2"); +is( $result, qq(pub_tab2|1|xxx +pub_tab2|3|yyy +pub_tab2|5|zzz +xxx_c|6|aaa), 'inserts into tab2 replicated'); + +$result = $node_subscriber2->safe_psql('postgres', + "SELECT c, a, b FROM tab2 ORDER BY 1, 2"); +is( $result, qq(pub_tab2|1|xxx +pub_tab2|3|yyy +pub_tab2|5|zzz +xxx_c|6|aaa), 'inserts into tab2 replicated');