Having trouble understanding how to support foreign keys on children of a partitioned parent #400
Replies: 1 comment
-
I'm not sure of any better way to handle this at the moment than your last option of doing everything in a transaction to try and make it transparent to the user. It seems the issue is the nature of how you have to move the data out of the default and into a new child table. You cannot add the new child table without first removing the data from the default but that would potentially violate your FK reference, as you're seeing here. My recommendation to avoid this would be to ensure your premake is set high enough to account for your normally expected data entries and avoid them going into the default. If you really don't want data going into the default and then having to worry about this, you can drop the default. But then if someone tries to insert data outside the range of the children, it will fail and be lost. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I created a testcase:
I can work around issue by issuing
What am I missing?
Reading -- misspelling -- https://github.com/pgpartman/pg_partman/blob/master/doc/pg_partman_howto_native.md#online-partitioning
Looks like its a issue (have you considered something like an online redefinition -- like pg_repack? )
"IMPORTANT NOTE REGUARDING FOREIGN KEYS
Taking the partitioned table offline is the only method that realistically works when you have foreign keys TO the table being partitioned. Since a brand new table must be created no matter what, the foreign key must also be recreated, so an outage involving all tables that are part of the FK relationship must be taken. A shorter outage may be possible with the online method below, but if you have to take an outage, this offline method is easier. "
I did figure how to make this less disruptive. Found this site : https://travisofthenorth.com/blog/2017/2/2/postgres-adding-foreign-keys-with-zero-downtime
I was able to achieve the goal of partitioning while maintaining no locks. I only have the exposure of dropping the fk at the time of maintenance.
Beta Was this translation helpful? Give feedback.
All reactions