diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index 9e12adb81a1..eac501753c8 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -252,16 +252,14 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt, * COPY retrieves rows from only the target table not any * inheritance children, the same as when RLS doesn't apply. * - * However, when COPY data from a partitioned table, we should not - * use "ONLY", since we also need to retrieve rows from its child - * partitions too. + * However, when copying data from a partitioned table, we don't + * not use "ONLY", since we need to retrieve rows from its + * descendant tables too. */ from = makeRangeVar(get_namespace_name(RelationGetNamespace(rel)), pstrdup(RelationGetRelationName(rel)), -1); - from->inh = false; /* apply ONLY */ - if (get_rel_relkind(relid) == RELKIND_PARTITIONED_TABLE) - from->inh = true; + from->inh = (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE); /* Build query */ select = makeNode(SelectStmt); diff --git a/src/backend/commands/copyto.c b/src/backend/commands/copyto.c index 74497240105..a1919c6db43 100644 --- a/src/backend/commands/copyto.c +++ b/src/backend/commands/copyto.c @@ -84,11 +84,11 @@ typedef struct CopyToStateData List *attnumlist; /* integer list of attnums to copy */ char *filename; /* filename, or NULL for STDOUT */ bool is_program; /* is 'filename' a program to popen? */ - List *partitions; /* OID list of partitions to copy data from */ copy_data_dest_cb data_dest_cb; /* function for writing data */ CopyFormatOptions opts; Node *whereClause; /* WHERE condition (or NULL) */ + List *partitions; /* OID list of partitions to copy data from */ /* * Working state @@ -1160,9 +1160,9 @@ CopyRelationTo(CopyToState cstate, Relation rel, Relation root_rel, uint64 *proc slot = table_slot_create(rel, NULL); /* - * A partition's rowtype might differ from the root table's. If we are - * exporting partition data here, we must convert it back to the root - * table's rowtype. + * If we are exporting partition data here, we check if converting tuples + * to the root table's rowtype, because a partition might have column + * order different than its root table. */ if (root_rel != NULL) { @@ -1182,7 +1182,7 @@ CopyRelationTo(CopyToState cstate, Relation rel, Relation root_rel, uint64 *proc copyslot = execute_attr_map_slot(map, slot, root_slot); else { - /* Deconstruct the tuple ... */ + /* Deconstruct the tuple */ slot_getallattrs(slot); copyslot = slot; } diff --git a/src/test/regress/expected/copy.out b/src/test/regress/expected/copy.out index af01e84cea1..24e0f472f14 100644 --- a/src/test/regress/expected/copy.out +++ b/src/test/regress/expected/copy.out @@ -374,6 +374,8 @@ id 1 DROP MATERIALIZED VIEW copytest_mv; -- Tests for COPY TO with partitioned tables. +-- The child table pp_2 has a different column order than the root table pp. +-- Check if COPY TO exports tuples as the root table's column order. CREATE TABLE pp (id int,val int) PARTITION BY RANGE (id); CREATE TABLE pp_1 (val int, id int) PARTITION BY RANGE (id); CREATE TABLE pp_2 (id int, val int) PARTITION BY RANGE (id); diff --git a/src/test/regress/sql/copy.sql b/src/test/regress/sql/copy.sql index 56d506ad4c6..676a8b342b5 100644 --- a/src/test/regress/sql/copy.sql +++ b/src/test/regress/sql/copy.sql @@ -407,16 +407,15 @@ COPY copytest_mv(id) TO stdout WITH (header); DROP MATERIALIZED VIEW copytest_mv; -- Tests for COPY TO with partitioned tables. +-- The child table pp_2 has a different column order than the root table pp. +-- Check if COPY TO exports tuples as the root table's column order. CREATE TABLE pp (id int,val int) PARTITION BY RANGE (id); CREATE TABLE pp_1 (val int, id int) PARTITION BY RANGE (id); CREATE TABLE pp_2 (id int, val int) PARTITION BY RANGE (id); ALTER TABLE pp ATTACH PARTITION pp_1 FOR VALUES FROM (1) TO (5); ALTER TABLE pp ATTACH PARTITION pp_2 FOR VALUES FROM (5) TO (10); - CREATE TABLE pp_15 PARTITION OF pp_1 FOR VALUES FROM (1) TO (5); 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;