Skip to content

Commit 8355a01

Browse files
committed
Allow ON CONFLICT .. DO NOTHING on a partitioned table.
ON CONFLICT .. DO UPDATE still doesn't work, for lack of a way of enforcing uniqueness across partitions, but we can still allow this case. Amit Langote, per discussion with Peter Geoghegan. Additional wordsmithing by me. Discussion: http://postgr.es/m/CAA-aLv7Z4uygtq-Q5CvDi9Y=VZxUyEnuWjL=EwCfOof=L04hgg@mail.gmail.com
1 parent 3371e4d commit 8355a01

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

doc/src/sgml/ddl.sgml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3854,8 +3854,12 @@ ANALYZE measurement;
38543854

38553855
<listitem>
38563856
<para>
3857-
<command>INSERT</command> statements with <literal>ON CONFLICT</>
3858-
clause are currently not allowed on partitioned tables.
3857+
Using the <literal>ON CONFLICT</literal> clause with partitioned tables
3858+
will cause an error if <literal>DO UPDATE</literal> is specified as the
3859+
alternative action, because unique or exclusion constraints can only be
3860+
created on individual partitions. There is no support for enforcing
3861+
uniqueness (or an exclusion constraint) across an entire partitioning
3862+
hierarchy.
38593863
</para>
38603864
</listitem>
38613865

src/backend/parser/analyze.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -842,16 +842,8 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
842842

843843
/* Process ON CONFLICT, if any. */
844844
if (stmt->onConflictClause)
845-
{
846-
/* Bail out if target relation is partitioned table */
847-
if (pstate->p_target_rangetblentry->relkind == RELKIND_PARTITIONED_TABLE)
848-
ereport(ERROR,
849-
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
850-
errmsg("ON CONFLICT clause is not supported with partitioned tables")));
851-
852845
qry->onConflict = transformOnConflictClause(pstate,
853846
stmt->onConflictClause);
854-
}
855847

856848
/*
857849
* If we have a RETURNING clause, we need to add the target relation to

src/test/regress/expected/insert_conflict.out

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,3 +786,13 @@ select * from selfconflict;
786786
(3 rows)
787787

788788
drop table selfconflict;
789+
-- check that the following works:
790+
-- insert into partitioned_table on conflict do nothing
791+
create table parted_conflict_test (a int, b char) partition by list (a);
792+
create table parted_conflict_test_1 partition of parted_conflict_test for values in (1);
793+
insert into parted_conflict_test values (1, 'a') on conflict do nothing;
794+
insert into parted_conflict_test values (1, 'a') on conflict do nothing;
795+
-- however, on conflict do update not supported yet
796+
insert into parted_conflict_test values (1) on conflict (a) do update set b = excluded.b where excluded.a = 1;
797+
ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
798+
drop table parted_conflict_test, parted_conflict_test_1;

src/test/regress/sql/insert_conflict.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,3 +471,13 @@ commit;
471471
select * from selfconflict;
472472

473473
drop table selfconflict;
474+
475+
-- check that the following works:
476+
-- insert into partitioned_table on conflict do nothing
477+
create table parted_conflict_test (a int, b char) partition by list (a);
478+
create table parted_conflict_test_1 partition of parted_conflict_test for values in (1);
479+
insert into parted_conflict_test values (1, 'a') on conflict do nothing;
480+
insert into parted_conflict_test values (1, 'a') on conflict do nothing;
481+
-- however, on conflict do update not supported yet
482+
insert into parted_conflict_test values (1) on conflict (a) do update set b = excluded.b where excluded.a = 1;
483+
drop table parted_conflict_test, parted_conflict_test_1;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy