1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-02 09:02:37 +03:00

Fix handling of inherited check constraints in ALTER COLUMN TYPE (again).

The previous way of reconstructing check constraints was to do a separate
"ALTER TABLE ONLY tab ADD CONSTRAINT" for each table in an inheritance
hierarchy.  However, that way has no hope of reconstructing the check
constraints' own inheritance properties correctly, as pointed out in
bug #13779 from Jan Dirk Zijlstra.  What we should do instead is to do
a regular "ALTER TABLE", allowing recursion, at the topmost table that
has a particular constraint, and then suppress the work queue entries
for inherited instances of the constraint.

Annoyingly, we'd tried to fix this behavior before, in commit 5ed6546cf,
but we failed to notice that it wasn't reconstructing the pg_constraint
field values correctly.

As long as I'm touching pg_get_constraintdef_worker anyway, tweak it to
always schema-qualify the target table name; this seems like useful backup
to the protections installed by commit 5f173040.

In HEAD/9.5, get rid of get_constraint_relation_oids, which is now unused.
(I could alternatively have modified it to also return conislocal, but that
seemed like a pretty single-purpose API, so let's not pretend it has some
other use.)  It's unused in the back branches as well, but I left it in
place just in case some third-party code has decided to use it.

In HEAD/9.5, also rename pg_get_constraintdef_string to
pg_get_constraintdef_command, as the previous name did nothing to explain
what that entry point did differently from others (and its comment was
equally useless).  Again, that change doesn't seem like material for
back-patching.

I did a bit of re-pgindenting in tablecmds.c in HEAD/9.5, as well.

Otherwise, back-patch to all supported branches.
This commit is contained in:
Tom Lane
2015-11-20 14:55:28 -05:00
parent 6c878a7553
commit 074c5cfbfb
7 changed files with 252 additions and 78 deletions

View File

@ -1810,16 +1810,125 @@ where oid = 'test_storage'::regclass;
t
(1 row)
-- ALTER TYPE with a check constraint and a child table (bug before Nov 2012)
CREATE TABLE test_inh_check (a float check (a > 10.2));
-- ALTER COLUMN TYPE with a check constraint and a child table (bug #13779)
CREATE TABLE test_inh_check (a float check (a > 10.2), b float);
CREATE TABLE test_inh_check_child() INHERITS(test_inh_check);
\d test_inh_check
Table "public.test_inh_check"
Column | Type | Modifiers
--------+------------------+-----------
a | double precision |
b | double precision |
Check constraints:
"test_inh_check_a_check" CHECK (a > 10.2::double precision)
Number of child tables: 1 (Use \d+ to list them.)
\d test_inh_check_child
Table "public.test_inh_check_child"
Column | Type | Modifiers
--------+------------------+-----------
a | double precision |
b | double precision |
Check constraints:
"test_inh_check_a_check" CHECK (a > 10.2::double precision)
Inherits: test_inh_check
select relname, conname, coninhcount, conislocal, connoinherit
from pg_constraint c, pg_class r
where relname like 'test_inh_check%' and c.conrelid = r.oid
order by 1, 2;
relname | conname | coninhcount | conislocal | connoinherit
----------------------+------------------------+-------------+------------+--------------
test_inh_check | test_inh_check_a_check | 0 | t | f
test_inh_check_child | test_inh_check_a_check | 1 | f | f
(2 rows)
ALTER TABLE test_inh_check ALTER COLUMN a TYPE numeric;
\d test_inh_check
Table "public.test_inh_check"
Column | Type | Modifiers
--------+------------------+-----------
a | numeric |
b | double precision |
Check constraints:
"test_inh_check_a_check" CHECK (a::double precision > 10.2::double precision)
Number of child tables: 1 (Use \d+ to list them.)
\d test_inh_check_child
Table "public.test_inh_check_child"
Column | Type | Modifiers
--------+------------------+-----------
a | numeric |
b | double precision |
Check constraints:
"test_inh_check_a_check" CHECK (a::double precision > 10.2::double precision)
Inherits: test_inh_check
select relname, conname, coninhcount, conislocal, connoinherit
from pg_constraint c, pg_class r
where relname like 'test_inh_check%' and c.conrelid = r.oid
order by 1, 2;
relname | conname | coninhcount | conislocal | connoinherit
----------------------+------------------------+-------------+------------+--------------
test_inh_check | test_inh_check_a_check | 0 | t | f
test_inh_check_child | test_inh_check_a_check | 1 | f | f
(2 rows)
-- also try noinherit, local, and local+inherited cases
ALTER TABLE test_inh_check ADD CONSTRAINT bnoinherit CHECK (b > 100) NO INHERIT;
ALTER TABLE test_inh_check_child ADD CONSTRAINT blocal CHECK (b < 1000);
ALTER TABLE test_inh_check_child ADD CONSTRAINT bmerged CHECK (b > 1);
ALTER TABLE test_inh_check ADD CONSTRAINT bmerged CHECK (b > 1);
NOTICE: merging constraint "bmerged" with inherited definition
\d test_inh_check
Table "public.test_inh_check"
Column | Type | Modifiers
--------+------------------+-----------
a | numeric |
b | double precision |
Check constraints:
"bmerged" CHECK (b > 1::double precision)
"bnoinherit" CHECK (b > 100::double precision) NO INHERIT
"test_inh_check_a_check" CHECK (a::double precision > 10.2::double precision)
Number of child tables: 1 (Use \d+ to list them.)
\d test_inh_check_child
Table "public.test_inh_check_child"
Column | Type | Modifiers
--------+------------------+-----------
a | numeric |
b | double precision |
Check constraints:
"blocal" CHECK (b < 1000::double precision)
"bmerged" CHECK (b > 1::double precision)
"test_inh_check_a_check" CHECK (a::double precision > 10.2::double precision)
Inherits: test_inh_check
select relname, conname, coninhcount, conislocal, connoinherit
from pg_constraint c, pg_class r
where relname like 'test_inh_check%' and c.conrelid = r.oid
order by 1, 2;
relname | conname | coninhcount | conislocal | connoinherit
----------------------+------------------------+-------------+------------+--------------
test_inh_check | bmerged | 0 | t | f
test_inh_check | bnoinherit | 0 | t | t
test_inh_check | test_inh_check_a_check | 0 | t | f
test_inh_check_child | blocal | 0 | t | f
test_inh_check_child | bmerged | 1 | t | f
test_inh_check_child | test_inh_check_a_check | 1 | f | f
(6 rows)
ALTER TABLE test_inh_check ALTER COLUMN b TYPE numeric;
NOTICE: merging constraint "bmerged" with inherited definition
\d test_inh_check
Table "public.test_inh_check"
Column | Type | Modifiers
--------+---------+-----------
a | numeric |
b | numeric |
Check constraints:
"bmerged" CHECK (b::double precision > 1::double precision)
"bnoinherit" CHECK (b::double precision > 100::double precision) NO INHERIT
"test_inh_check_a_check" CHECK (a::double precision > 10.2::double precision)
Number of child tables: 1 (Use \d+ to list them.)
@ -1828,10 +1937,27 @@ Table "public.test_inh_check_child"
Column | Type | Modifiers
--------+---------+-----------
a | numeric |
b | numeric |
Check constraints:
"blocal" CHECK (b::double precision < 1000::double precision)
"bmerged" CHECK (b::double precision > 1::double precision)
"test_inh_check_a_check" CHECK (a::double precision > 10.2::double precision)
Inherits: test_inh_check
select relname, conname, coninhcount, conislocal, connoinherit
from pg_constraint c, pg_class r
where relname like 'test_inh_check%' and c.conrelid = r.oid
order by 1, 2;
relname | conname | coninhcount | conislocal | connoinherit
----------------------+------------------------+-------------+------------+--------------
test_inh_check | bmerged | 0 | t | f
test_inh_check | bnoinherit | 0 | t | t
test_inh_check | test_inh_check_a_check | 0 | t | f
test_inh_check_child | blocal | 0 | t | f
test_inh_check_child | bmerged | 1 | t | f
test_inh_check_child | test_inh_check_a_check | 1 | f | f
(6 rows)
-- check for rollback of ANALYZE corrupting table property flags (bug #11638)
CREATE TABLE check_fk_presence_1 (id int PRIMARY KEY, t text);
CREATE TABLE check_fk_presence_2 (id int REFERENCES check_fk_presence_1, t text);

View File

@ -1253,12 +1253,40 @@ select reltoastrelid <> 0 as has_toast_table
from pg_class
where oid = 'test_storage'::regclass;
-- ALTER TYPE with a check constraint and a child table (bug before Nov 2012)
CREATE TABLE test_inh_check (a float check (a > 10.2));
-- ALTER COLUMN TYPE with a check constraint and a child table (bug #13779)
CREATE TABLE test_inh_check (a float check (a > 10.2), b float);
CREATE TABLE test_inh_check_child() INHERITS(test_inh_check);
\d test_inh_check
\d test_inh_check_child
select relname, conname, coninhcount, conislocal, connoinherit
from pg_constraint c, pg_class r
where relname like 'test_inh_check%' and c.conrelid = r.oid
order by 1, 2;
ALTER TABLE test_inh_check ALTER COLUMN a TYPE numeric;
\d test_inh_check
\d test_inh_check_child
select relname, conname, coninhcount, conislocal, connoinherit
from pg_constraint c, pg_class r
where relname like 'test_inh_check%' and c.conrelid = r.oid
order by 1, 2;
-- also try noinherit, local, and local+inherited cases
ALTER TABLE test_inh_check ADD CONSTRAINT bnoinherit CHECK (b > 100) NO INHERIT;
ALTER TABLE test_inh_check_child ADD CONSTRAINT blocal CHECK (b < 1000);
ALTER TABLE test_inh_check_child ADD CONSTRAINT bmerged CHECK (b > 1);
ALTER TABLE test_inh_check ADD CONSTRAINT bmerged CHECK (b > 1);
\d test_inh_check
\d test_inh_check_child
select relname, conname, coninhcount, conislocal, connoinherit
from pg_constraint c, pg_class r
where relname like 'test_inh_check%' and c.conrelid = r.oid
order by 1, 2;
ALTER TABLE test_inh_check ALTER COLUMN b TYPE numeric;
\d test_inh_check
\d test_inh_check_child
select relname, conname, coninhcount, conislocal, connoinherit
from pg_constraint c, pg_class r
where relname like 'test_inh_check%' and c.conrelid = r.oid
order by 1, 2;
-- check for rollback of ANALYZE corrupting table property flags (bug #11638)
CREATE TABLE check_fk_presence_1 (id int PRIMARY KEY, t text);