mirror of
https://github.com/postgres/postgres.git
synced 2025-07-30 11:03:19 +03:00
Fix ALTER TABLE / SET TYPE for irregular inheritance
If inherited tables don't have exactly the same schema, the USING clause
in an ALTER TABLE / SET DATA TYPE misbehaves when applied to the
children tables since commit 9550e8348b
. Starting with that commit,
the attribute numbers in the USING expression are fixed during parse
analysis. This can lead to bogus errors being reported during
execution, such as:
ERROR: attribute 2 has wrong type
DETAIL: Table has type smallint, but query expects integer.
Since it wouldn't do to revert to the original coding, we now apply a
transformation to map the attribute numbers to the correct ones for each
child.
Reported by Justin Pryzby
Analysis by Tom Lane; patch by me.
Discussion: https://postgr.es/m/20170102225618.GA10071@telsasoft.com
This commit is contained in:
@ -1335,6 +1335,28 @@ select relname, conname, coninhcount, conislocal, connoinherit
|
||||
where relname like 'test_inh_check%' and c.conrelid = r.oid
|
||||
order by 1, 2;
|
||||
|
||||
-- ALTER COLUMN TYPE with different schema in children
|
||||
-- Bug at https://postgr.es/m/20170102225618.GA10071@telsasoft.com
|
||||
CREATE TABLE test_type_diff (f1 int);
|
||||
CREATE TABLE test_type_diff_c (extra smallint) INHERITS (test_type_diff);
|
||||
ALTER TABLE test_type_diff ADD COLUMN f2 int;
|
||||
INSERT INTO test_type_diff_c VALUES (1, 2, 3);
|
||||
ALTER TABLE test_type_diff ALTER COLUMN f2 TYPE bigint USING f2::bigint;
|
||||
|
||||
CREATE TABLE test_type_diff2 (int_two int2, int_four int4, int_eight int8);
|
||||
CREATE TABLE test_type_diff2_c1 (int_four int4, int_eight int8, int_two int2);
|
||||
CREATE TABLE test_type_diff2_c2 (int_eight int8, int_two int2, int_four int4);
|
||||
CREATE TABLE test_type_diff2_c3 (int_two int2, int_four int4, int_eight int8);
|
||||
ALTER TABLE test_type_diff2_c1 INHERIT test_type_diff2;
|
||||
ALTER TABLE test_type_diff2_c2 INHERIT test_type_diff2;
|
||||
ALTER TABLE test_type_diff2_c3 INHERIT test_type_diff2;
|
||||
INSERT INTO test_type_diff2_c1 VALUES (1, 2, 3);
|
||||
INSERT INTO test_type_diff2_c2 VALUES (4, 5, 6);
|
||||
INSERT INTO test_type_diff2_c3 VALUES (7, 8, 9);
|
||||
ALTER TABLE test_type_diff2 ALTER COLUMN int_four TYPE int8 USING int_four::int8;
|
||||
-- whole-row references are disallowed
|
||||
ALTER TABLE test_type_diff2 ALTER COLUMN int_four TYPE int4 USING (pg_column_size(test_type_diff2));
|
||||
|
||||
-- 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);
|
||||
|
Reference in New Issue
Block a user