pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Márcio Antônio Sepp <marcio@zyontecnologia.com.br>
To: pgsql-general@postgresql.org
Subject: Weird behavior with update cascade on partitioned tables when moving data between partitions
Date: Wed, 5 Feb 2020 14:30:04 -0300
Message-ID: <00d801d5dc49$e7225d50$b56717f0$@com.br> (raw)


Hi all,


Update cascade apparently doesn't work as well on partiotioned tables (when
moving data between different partitions). 
Please, look at example below:


-- Create master partitioned table;
CREATE TABLE users (
    id             serial NOT NULL,
    username       text NOT NULL,
    password       text,
    primary key (id)
)
PARTITION BY RANGE ( id );

CREATE TABLE users_p0
    partition OF users 
    FOR VALUES FROM (MINVALUE) TO (10);
CREATE TABLE users_p1
    partition OF users
    FOR VALUES FROM (10) TO (20);


-- Create detail table;
create table logs ( id serial not null,
    user_id integer not null,
    primary key (id),
    foreign key (user_id) references users (id) on update cascade 
); 


test=# insert into users (id, username, password) values (1, 'user',
'pass');
INSERT 0 1

test=# insert into logs (id, user_id) values (1, 1);
INSERT 0 1

-- update without changing partition
test=# update users set id = 8 where id = 1;
UPDATE 1

-- Lets see the log table
test=# select * from logs;
 id | user_id
----+---------
  1 |       8
(1 row)

-- update changing partition;
test=# update users set id = 12 where id = 8;
ERROR:  update or delete on table "users_p0" violates foreign key constraint
"logs_user_id_fkey1" on table "logs"
DETAIL:  Key (id)=(8) is still referenced from table "logs".


IF create the foreign key with option "on update cascade on delete cascade"
after running this command: update users set id = 12 where id = 8; the
Record on logs table Will be deleted. 

Is this the expectec behavior? 


--
Att
Márcio A. Sepp






Message-ID: <00d801d5dc49$e7225d50$b56717f0$@com.br>
Permalink:  ../00d801d5dc49$e7225d50$b56717f0$@com.br/
Also on:    postgresql.org/message-id/00d801d5dc49$e7225d50$b56717f0$@com.br

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: marcio@zyontecnologia.com.br
  Subject: Re: Weird behavior with update cascade on partitioned tables when moving data between partitions
  In-Reply-To: <00d801d5dc49$e7225d50$b56717f0$@com.br>
  References: <00d801d5dc49$e7225d50$b56717f0$@com.br>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox