diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index d06662bd32..6674ee2213 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -2414,11 +2414,20 @@ CopyFrom(CopyState cstate) * go into pages containing tuples from any other transactions --- but this * must be the case if we have a new table or new relfilenode, so we need * no additional work to enforce that. + * + * We currently don't support this optimization if the COPY target is a + * partitioned table as we currently only lazily initialize partition + * information when routing the first tuple to the partition. We cannot + * know at this stage if we can perform this optimization. It should be + * possible to improve on this, but it does mean maintaining heap insert + * option flags per partition and setting them when we first open the + * partition. *---------- */ /* createSubid is creation check, newRelfilenodeSubid is truncation check */ - if (cstate->rel->rd_createSubid != InvalidSubTransactionId || - cstate->rel->rd_newRelfilenodeSubid != InvalidSubTransactionId) + if (cstate->rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE && + (cstate->rel->rd_createSubid != InvalidSubTransactionId || + cstate->rel->rd_newRelfilenodeSubid != InvalidSubTransactionId)) { hi_options |= HEAP_INSERT_SKIP_FSM; if (!XLogIsNeeded()) @@ -2438,6 +2447,23 @@ CopyFrom(CopyState cstate) */ if (cstate->freeze) { + /* + * We currently disallow COPY FREEZE on partitioned tables. The + * reason for this is that here we're only able to determine details + * about the partitioned table. The actual partitions are not opened + * until we start routing tuples to them, so we cannot know in advance + * if the partition has just been created or not. It may be possible + * to relax this, but we would need to store hi_options per partition + * and it would possibly mean that we'd freeze some partitions but not + * others. An outright ERROR seems better than surprising behavior. + */ + if (cstate->rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) + { + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot perform FREEZE on a partitioned table"))); + } + /* * Tolerate one registration for the benefit of FirstXactSnapshot. * Scan-bearing queries generally create at least two registrations, diff --git a/src/test/regress/input/copy.source b/src/test/regress/input/copy.source index 014b1b5711..b5a6a6a4b9 100644 --- a/src/test/regress/input/copy.source +++ b/src/test/regress/input/copy.source @@ -159,6 +159,12 @@ truncate parted_copytest; copy parted_copytest from '@abs_builddir@/results/parted_copytest.csv'; +-- Ensure COPY FREEZE errors for partitioned tables. +begin; +truncate parted_copytest; +copy parted_copytest from '@abs_builddir@/results/parted_copytest.csv' (freeze); +rollback; + select tableoid::regclass,count(*),sum(a) from parted_copytest group by tableoid order by tableoid::regclass::name; diff --git a/src/test/regress/output/copy.source b/src/test/regress/output/copy.source index ab096153ad..c4f10a223a 100644 --- a/src/test/regress/output/copy.source +++ b/src/test/regress/output/copy.source @@ -113,6 +113,12 @@ insert into parted_copytest select x,1,'One' from generate_series(1011,1020) x; copy (select * from parted_copytest order by a) to '@abs_builddir@/results/parted_copytest.csv'; truncate parted_copytest; copy parted_copytest from '@abs_builddir@/results/parted_copytest.csv'; +-- Ensure COPY FREEZE errors for partitioned tables. +begin; +truncate parted_copytest; +copy parted_copytest from '@abs_builddir@/results/parted_copytest.csv' (freeze); +ERROR: cannot perform FREEZE on a partitioned table +rollback; select tableoid::regclass,count(*),sum(a) from parted_copytest group by tableoid order by tableoid::regclass::name; tableoid | count | sum