Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[YSQL] Unable to use ON CONFLICT on partitioned table #13181

Open
jannehietamaki opened this issue Jul 7, 2022 · 12 comments
Open

[YSQL] Unable to use ON CONFLICT on partitioned table #13181

jannehietamaki opened this issue Jul 7, 2022 · 12 comments
Labels
area/ysql Yugabyte SQL (YSQL) kind/enhancement This is an enhancement of an existing feature priority/medium Medium priority issue

Comments

@jannehietamaki
Copy link

jannehietamaki commented Jul 7, 2022

Jira Link: DB-2865

Description

This query is returning an error ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification:

create table part ( a int, b int, list varchar(5) ) partition by list (list);
create table part_1 partition of part for values in ('beer');
create table part_2 partition of part for values in ('wine');
create unique index iu1 on part_1 (a);
create unique index iu2 on part_2 (a);
insert into part(a,b,list) values (1,2,'beer') on conflict (a,list) do update set b = part.b + 1;

It should work on Postgres 11: https://www.dbi-services.com/blog/insert-on-conflict-with-partitions-finally-works-in-postgresql-11/

@jannehietamaki jannehietamaki added area/ysql Yugabyte SQL (YSQL) status/awaiting-triage Issue awaiting triage labels Jul 7, 2022
@yugabyte-ci yugabyte-ci added kind/bug This issue is a bug priority/medium Medium priority issue labels Jul 7, 2022
@ddorian
Copy link
Contributor

ddorian commented Jul 7, 2022

Just tried it on PostgreSQL 11.16 and it doesn't work:

guru=# select version();
                                                              version                                                              
-----------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 11.16 (Ubuntu 11.16-1.pgdg18.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0, 64-bit
(1 row)

guru=# create table part ( a int, b int, list varchar(5) ) partition by list (list);
CREATE TABLE
guru=# create table part_1 partition of part for values in ('beer');
CREATE TABLE
guru=# create table part_2 partition of part for values in ('wine');
CREATE TABLE
guru=# create unique index iu1 on part_1 (a);
CREATE INDEX
guru=# create unique index iu2 on part_2 (a);
CREATE INDEX
guru=# insert into part(a,b,list) values (1,2,'beer') on conflict (a,list) do update set b = part.b + 1;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification

@ddorian
Copy link
Contributor

ddorian commented Jul 7, 2022

This works though:

guru=# create unique index iu0 on part(a,list);
CREATE INDEX
guru=# insert into part(a,b,list) values (1,2,'beer') on conflict (a,list) do update set b = part.b + 1;
INSERT 0 1

@jannehietamaki
Copy link
Author

@ddorian yes that works, but when doing geopartitioning it's not good idea to create the index on parent table.

@ddorian
Copy link
Contributor

ddorian commented Jul 7, 2022

As a workaround you can upsert directly to the child table I believe. Can that work for you as a workaround?

@jannehietamaki
Copy link
Author

Can't do that in my use case because can't resolve the table name easily.

@ddorian
Copy link
Contributor

ddorian commented Jul 7, 2022

Can you describe your exact use case?

@jannehietamaki
Copy link
Author

I'm doing upsert from trigger, and partition table name is not easily available.

But I'll use update+insert as a workaround for now and waiting if YB will be updated to new Postgres. 2.15 was supposed to be based on v13, but I guess that has not happened?

@ddorian
Copy link
Contributor

ddorian commented Jul 7, 2022

to new Postgres. 2.15 was supposed to be based on v13, but I guess that has not happened?

It's still in progress. You can track this issue: #9797

@jannehietamaki
Copy link
Author

It's still in progress. You can track this issue: #9797

Already tracking, but there is no progress since it was created last year.

@ddorian
Copy link
Contributor

ddorian commented Jul 7, 2022

A recent update from the dev:

This is active work in progress, code porting is done and stabilization is in progress. Since this involves changes to the core module, it needs to go though rigorous testing, we expect this feature to be released in later this year (around fall releases 2.16. or 2.17)

@FranckPachot
Copy link
Contributor

@jannehietamaki the behaviour above (constraint created on child, query on parent) will be the same in PostgreSQL 14:
https://dbfiddle.uk/?rdbms=postgres_14&fiddle=2106039b5855b6f0b8c4602abbd2645a

The dbi-services blog creates the constraint on the parent. That works in YugabyteDB as well.

What I do with geo-partitioning is mapping the owner / schema / tablespaces to get it transparent. Example:
https://dev.to/aws-heroes/postgresql-index-partition-in-same-tablespace-as-table-2hak

@yugabyte-ci yugabyte-ci added kind/enhancement This is an enhancement of an existing feature and removed kind/bug This issue is a bug status/awaiting-triage Issue awaiting triage labels Jul 27, 2022
@karthik-ramanathan-3006
Copy link
Contributor

Postgres 15 (as of version 15.7) does not have support for "partitioned table, query on parent" either.

From the (latest version) docs:

INSERT statements with ON CONFLICT clauses are unlikely to work as expected, as the ON CONFLICT action is only taken in case of unique violations on the specified target relation, not its child relations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/ysql Yugabyte SQL (YSQL) kind/enhancement This is an enhancement of an existing feature priority/medium Medium priority issue
Projects
None yet
Development

No branches or pull requests

5 participants