diff --git a/src/backend/commands/copyto.c b/src/backend/commands/copyto.c index 1085d0d5b8d..ffed63a2986 100644 --- a/src/backend/commands/copyto.c +++ b/src/backend/commands/copyto.c @@ -1348,8 +1348,8 @@ CopyRelationTo(CopyToState cstate, Relation rel, Relation root_rel, uint64 *proc if (root_rel != NULL) { root_slot = table_slot_create(root_rel, NULL); - map = build_attrmap_by_name_if_req(RelationGetDescr(root_rel), - RelationGetDescr(rel), + map = build_attrmap_by_name_if_req(RelationGetDescr(rel), + RelationGetDescr(root_rel), false); } diff --git a/src/test/regress/expected/copy.out b/src/test/regress/expected/copy.out index 1714faab39c..37498cdd6e7 100644 --- a/src/test/regress/expected/copy.out +++ b/src/test/regress/expected/copy.out @@ -594,3 +594,14 @@ id val 5 15 6 16 DROP TABLE PP; +-- Check if COPY TO handles dropped columns in partitions. +CREATE TABLE pp_dropcol (id int, val int) PARTITION BY RANGE (id); +CREATE TABLE pp_dropcol_1 (dropme int, id int, val int); +ALTER TABLE pp_dropcol_1 DROP COLUMN dropme; +ALTER TABLE pp_dropcol ATTACH PARTITION pp_dropcol_1 FOR VALUES FROM (1) TO (10); +INSERT INTO pp_dropcol VALUES (1, 11), (2, 12); +COPY pp_dropcol TO stdout(header); +id val +1 11 +2 12 +DROP TABLE pp_dropcol; diff --git a/src/test/regress/sql/copy.sql b/src/test/regress/sql/copy.sql index eaad290b257..094fd76c12b 100644 --- a/src/test/regress/sql/copy.sql +++ b/src/test/regress/sql/copy.sql @@ -535,3 +535,12 @@ CREATE TABLE pp_510 PARTITION OF pp_2 FOR VALUES FROM (5) TO (10); INSERT INTO pp SELECT g, 10 + g FROM generate_series(1,6) g; COPY pp TO stdout(header); DROP TABLE PP; + +-- Check if COPY TO handles dropped columns in partitions. +CREATE TABLE pp_dropcol (id int, val int) PARTITION BY RANGE (id); +CREATE TABLE pp_dropcol_1 (dropme int, id int, val int); +ALTER TABLE pp_dropcol_1 DROP COLUMN dropme; +ALTER TABLE pp_dropcol ATTACH PARTITION pp_dropcol_1 FOR VALUES FROM (1) TO (10); +INSERT INTO pp_dropcol VALUES (1, 11), (2, 12); +COPY pp_dropcol TO stdout(header); +DROP TABLE pp_dropcol;