Thread: Re: [PATCHES] Inherited Constraints
On Wed, 2005-12-07 at 21:24 +0000, Simon Riggs wrote: > Following patch implements record of whether a constraint is inherited > or not, and prevents dropping of inherited constraints. Patch posted to -patches list. > What it doesn't do: > It doesn't yet prevent dropping the parent constraint, which is wrong, > clearly, but what to do about it? > 1. make dropping a constraint drop all constraints dependent upon it > (without any explicit cascade) > 2. add a new clause to ALTER TABLE .... DROP CONSTRAINT .... CASCADE > > I prefer (1), since it is SQL Standard compliant, easier to remember and > automatic de-inheritance is the natural opposite of the automatic > inheritance process. Comments, please -hackers? Which implementation should I pick (or another)? > Further patch will utilise this new knowledge to reduce the number of > tests made during constraint_exclusion. Best Regards, Simon Riggs
Ühel kenal päeval, N, 2005-12-08 kell 11:10, kirjutas Simon Riggs: > On Wed, 2005-12-07 at 21:24 +0000, Simon Riggs wrote: > > Following patch implements record of whether a constraint is inherited > > or not, and prevents dropping of inherited constraints. > > Patch posted to -patches list. > > > What it doesn't do: > > It doesn't yet prevent dropping the parent constraint, which is wrong, > > clearly, but what to do about it? > > 1. make dropping a constraint drop all constraints dependent upon it > > (without any explicit cascade) > > 2. add a new clause to ALTER TABLE .... DROP CONSTRAINT .... CASCADE > > > > I prefer (1), since it is SQL Standard compliant, easier to remember and > > automatic de-inheritance is the natural opposite of the automatic > > inheritance process. > > Comments, please -hackers? It would be logical to do the same as DROP TABLE does, i.e (2). hannu=# create table parent(i int); CREATE TABLE hannu=# create table child() inherits(parent); CREATE TABLE hannu=# drop table parent; NOTICE: table child depends on table parent ERROR: cannot drop table parent because other objects depend on it HINT: Use DROP ... CASCADE to drop the dependent objects too. hannu=# Maybe there should be another option in addition to CASCADE, say DISINHERIT, which leaves all child constraints as heads of new ingeritance hierarchies. DROP CASCADE + ADD BACK ALL CHILD CONSTRAINTS may be prohibitively expensive for biggish tables. Another nice (but no doubt more complex) thing would be ability to add multiple constraints at once, needing only one seqscan to check for compliance with added constraints and/or making constraint checks smarter, so that for.ex. "ADD CONSTRAINT CHECK i > 0" could make use of index on i instead of doing a seqscan. Or if there is a constraint "CHECK i > 0" then adding another like "CHECK i > -1" would not need to check actual data either. > Which implementation should I pick (or another)? > > > Further patch will utilise this new knowledge to reduce the number of > > tests made during constraint_exclusion. So will hierarchical inheritance be the thing to do to take advantage of i then ? year+- month1|+-day1|+-day2 .....|\-day31+- month2 etc. btw, will your DROP patch support multiple inheritance ? ----------- Hannu
> Another nice (but no doubt more complex) thing would be ability to add > multiple constraints at once, needing only one seqscan to check for > compliance with added constraints and/or making constraint checks > smarter, so that for.ex. "ADD CONSTRAINT CHECK i > 0" could make use of > index on i instead of doing a seqscan. Or if there is a constraint > "CHECK i > 0" then adding another like "CHECK i > -1" would not need to > check actual data either. Check out the comma in alter table. ALTER TABLE tab ADD COLUMN serial NOT NULL UNIQUE,ADD CHECK (foo > 24),ADD CHECK (baz < 18),ADD COLUMN integer NOT NULL DEFAULT32; Table tab (and each of the tables that inherits from it) is scanned and rewritten once. I believe this was added for 8.0. --
On Thu, 2005-12-08 at 11:10 +0000, Simon Riggs wrote: > On Wed, 2005-12-07 at 21:24 +0000, Simon Riggs wrote: > > Following patch implements record of whether a constraint is inherited > > or not, and prevents dropping of inherited constraints. > > Patch posted to -patches list. > > > What it doesn't do: > > It doesn't yet prevent dropping the parent constraint, which is wrong, > > clearly, but what to do about it? > > 1. make dropping a constraint drop all constraints dependent upon it > > (without any explicit cascade) > > 2. add a new clause to ALTER TABLE .... DROP CONSTRAINT .... CASCADE > > > > I prefer (1), since it is SQL Standard compliant, easier to remember and > > automatic de-inheritance is the natural opposite of the automatic > > inheritance process. > > Comments, please -hackers? Late night hacking again.... ALTER TABLE .... DROP CONSTRAINT .... CASCADE does of course already exist, so the following should cause dependency violation ERRORs: - omitting the CASCADE when attempting to delete parent constraint - attempting to drop the child constraint Best Regards, Simon Riggs
On Thursday 2005-12-08 15:47, Simon Riggs wrote: > does of course already exist, so the following should cause dependency > violation ERRORs: > - omitting the CASCADE when attempting to delete parent constraint > - attempting to drop the child constraint Why should dropping the child constraint fail? Child tables are supposed to be able to over-ride parent constraints. Dropping a parent's constraint sounds like just a way to over-ride a constraint with no constraint at all. (Making the column unconstrained.)
Trent Shipley <tshipley@deru.com> writes: > Child tables are supposed to be able to over-ride parent constraints. Says who? If we allow that, then reading the parent table will produce rows that violate the parent's constraint. This does not seem very wise. regards, tom lane
Where are we on this patch? My testing shows it is still shows we have a problem:test=> CREATE TABLE x(y INT CHECK(y > 0));CREATE TABLEtest=> CREATE TABLE z(a INT) inherits (x);CREATE TABLEtest=>ALTER TABLE z DROP CONSTRAINT "x_y_check";ALTER TABLEtest=> ALTER TABLE x DROP CONSTRAINT "x_y_check";ALTER TABLE Deleting the parent constraint first does not require CASCADE, as it should, I think:test=> CREATE TABLE x(y INT CHECK(y > 0));CREATE TABLEtest=> CREATE TABLE z(a INT) inherits (x);CREATE TABLEtest=>ALTER TABLE x DROP CONSTRAINT "x_y_check";ALTER TABLEtest=> ALTER TABLE z DROP CONSTRAINT "x_y_check";ERROR: CONSTRAINT "x_y_check" does NOT exist --------------------------------------------------------------------------- Simon Riggs wrote: > On Thu, 2005-12-08 at 11:10 +0000, Simon Riggs wrote: > > On Wed, 2005-12-07 at 21:24 +0000, Simon Riggs wrote: > > > Following patch implements record of whether a constraint is inherited > > > or not, and prevents dropping of inherited constraints. > > > > Patch posted to -patches list. > > > > > What it doesn't do: > > > It doesn't yet prevent dropping the parent constraint, which is wrong, > > > clearly, but what to do about it? > > > 1. make dropping a constraint drop all constraints dependent upon it > > > (without any explicit cascade) > > > 2. add a new clause to ALTER TABLE .... DROP CONSTRAINT .... CASCADE > > > > > > I prefer (1), since it is SQL Standard compliant, easier to remember and > > > automatic de-inheritance is the natural opposite of the automatic > > > inheritance process. > > > > Comments, please -hackers? > > Late night hacking again.... > > ALTER TABLE .... DROP CONSTRAINT .... CASCADE > > does of course already exist, so the following should cause dependency > violation ERRORs: > - omitting the CASCADE when attempting to delete parent constraint > - attempting to drop the child constraint > > Best Regards, Simon Riggs > > > ---------------------------(end of broadcast)--------------------------- > TIP 4: Have you searched our list archives? > > http://archives.postgresql.org > -- Bruce Momjian http://candle.pha.pa.us SRA OSS, Inc. http://www.sraoss.com + If your life is a hard drive, Christ can be your backup. +
Added to TODO: o Prevent parent tables from altering or dropping constraints like CHECK that are inherited by child tables Dropping constraints should only be possible with CASCADE. and we already have this in TODO: o %Prevent child tables from altering or dropping constraints like CHECK that were inherited from the parenttable so I think we now have all the failure cases documented. --------------------------------------------------------------------------- Bruce Momjian wrote: > > Where are we on this patch? My testing shows it is still shows we have > a problem: > > test=> CREATE TABLE x(y INT CHECK(y > 0)); > CREATE TABLE > test=> CREATE TABLE z(a INT) inherits (x); > CREATE TABLE > test=> ALTER TABLE z DROP CONSTRAINT "x_y_check"; > ALTER TABLE > test=> ALTER TABLE x DROP CONSTRAINT "x_y_check"; > ALTER TABLE > > Deleting the parent constraint first does not require CASCADE, as it > should, I think: > > test=> CREATE TABLE x(y INT CHECK(y > 0)); > CREATE TABLE > test=> CREATE TABLE z(a INT) inherits (x); > CREATE TABLE > test=> ALTER TABLE x DROP CONSTRAINT "x_y_check"; > ALTER TABLE > test=> ALTER TABLE z DROP CONSTRAINT "x_y_check"; > ERROR: CONSTRAINT "x_y_check" does NOT exist > > --------------------------------------------------------------------------- > > Simon Riggs wrote: > > On Thu, 2005-12-08 at 11:10 +0000, Simon Riggs wrote: > > > On Wed, 2005-12-07 at 21:24 +0000, Simon Riggs wrote: > > > > Following patch implements record of whether a constraint is inherited > > > > or not, and prevents dropping of inherited constraints. > > > > > > Patch posted to -patches list. > > > > > > > What it doesn't do: > > > > It doesn't yet prevent dropping the parent constraint, which is wrong, > > > > clearly, but what to do about it? > > > > 1. make dropping a constraint drop all constraints dependent upon it > > > > (without any explicit cascade) > > > > 2. add a new clause to ALTER TABLE .... DROP CONSTRAINT .... CASCADE > > > > > > > > I prefer (1), since it is SQL Standard compliant, easier to remember and > > > > automatic de-inheritance is the natural opposite of the automatic > > > > inheritance process. > > > > > > Comments, please -hackers? > > > > Late night hacking again.... > > > > ALTER TABLE .... DROP CONSTRAINT .... CASCADE > > > > does of course already exist, so the following should cause dependency > > violation ERRORs: > > - omitting the CASCADE when attempting to delete parent constraint > > - attempting to drop the child constraint > > > > Best Regards, Simon Riggs > > > > > > ---------------------------(end of broadcast)--------------------------- > > TIP 4: Have you searched our list archives? > > > > http://archives.postgresql.org > > > > -- > Bruce Momjian http://candle.pha.pa.us > SRA OSS, Inc. http://www.sraoss.com > > + If your life is a hard drive, Christ can be your backup. + > > ---------------------------(end of broadcast)--------------------------- > TIP 2: Don't 'kill -9' the postmaster > -- Bruce Momjian http://candle.pha.pa.us SRA OSS, Inc. http://www.sraoss.com + If your life is a hard drive, Christ can be your backup. +
Ühel kenal päeval, E, 2006-03-06 kell 12:12, kirjutas Bruce Momjian: > Added to TODO: > > o Prevent parent tables from altering or dropping constraints > like CHECK that are inherited by child tables > > Dropping constraints should only be possible with CASCADE. > > and we already have this in TODO: > > o %Prevent child tables from altering or dropping constraints > like CHECK that were inherited from the parent table > > so I think we now have all the failure cases documented. If you want to be consistent, then ALTER TABLE ONLY ADD CONSTRAINT .. should also be forbidden, so you can't create non-inherited constraints --------------- Hannu
Hannu Krosing wrote: > ?hel kenal p?eval, E, 2006-03-06 kell 12:12, kirjutas Bruce Momjian: > > Added to TODO: > > > > o Prevent parent tables from altering or dropping constraints > > like CHECK that are inherited by child tables > > > > Dropping constraints should only be possible with CASCADE. > > > > and we already have this in TODO: > > > > o %Prevent child tables from altering or dropping constraints > > like CHECK that were inherited from the parent table > > > > so I think we now have all the failure cases documented. > > If you want to be consistent, then ALTER TABLE ONLY ADD CONSTRAINT .. > should also be forbidden, so you can't create non-inherited constraints I don't have a problem with creating ONLY constraints on parents and children. We just don't want them to be removed/modified if they are shared. -- Bruce Momjian http://candle.pha.pa.us SRA OSS, Inc. http://www.sraoss.com + If your life is a hard drive, Christ can be your backup. +
Ühel kenal päeval, E, 2006-03-06 kell 17:25, kirjutas Bruce Momjian: > Hannu Krosing wrote: > > ?hel kenal p?eval, E, 2006-03-06 kell 12:12, kirjutas Bruce Momjian: > > > Added to TODO: > > > > > > o Prevent parent tables from altering or dropping constraints > > > like CHECK that are inherited by child tables > > > > > > Dropping constraints should only be possible with CASCADE. > > > > > > and we already have this in TODO: > > > > > > o %Prevent child tables from altering or dropping constraints > > > like CHECK that were inherited from the parent table > > > > > > so I think we now have all the failure cases documented. > > > > If you want to be consistent, then ALTER TABLE ONLY ADD CONSTRAINT .. > > should also be forbidden, so you can't create non-inherited constraints > > I don't have a problem with creating ONLY constraints on parents and > children. We just don't want them to be removed/modified if they are > shared. Well, when you delete a constraint from child, the constraint becomes an "ONLY" constraint on parent. If you allow ONLY constraints on parents, then why disallow dropping them from childs ? IIRC the original complaint about being able to drop constraints from children was that inherited tables not being bound by constraints on parents was unexpected/broken. I.E when you have CREATE TABLE T(i int check (i>0)); then you would be really surprised by getting -1 out from that table. --------------- Hannu
On Wed, 8 Mar 2006, Hannu Krosing wrote: > Ühel kenal päeval, E, 2006-03-06 kell 17:25, kirjutas Bruce Momjian: > > Hannu Krosing wrote: > > > ?hel kenal p?eval, E, 2006-03-06 kell 12:12, kirjutas Bruce Momjian: > > > > Added to TODO: > > > > > > > > o Prevent parent tables from altering or dropping constraints > > > > like CHECK that are inherited by child tables > > > > > > > > Dropping constraints should only be possible with CASCADE. > > > > > > > > and we already have this in TODO: > > > > > > > > o %Prevent child tables from altering or dropping constraints > > > > like CHECK that were inherited from the parent table > > > > > > > > so I think we now have all the failure cases documented. > > > > > > If you want to be consistent, then ALTER TABLE ONLY ADD CONSTRAINT .. > > > should also be forbidden, so you can't create non-inherited constraints > > > > I don't have a problem with creating ONLY constraints on parents and > > children. We just don't want them to be removed/modified if they are > > shared. > > Well, when you delete a constraint from child, the constraint becomes an > "ONLY" constraint on parent. Only if there's a single child, otherwise you have a partially-ONLY constraint unless you made it ONLY constraints on the parent and all other children (but then removing the parent constraint wouldn't remove it from the other children presumably). > If you allow ONLY constraints on parents, then why disallow dropping > them from childs ? I agree with this in any case. I think both are fairly broken.