BUG #16272: Index expression can refer to wrong attributes if index is created via CREATE TABLE LIKE
The Post Office <noreply@postgresql.org>
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: tom@intevation.de
Date: 2020-02-21T19:37:55Z
Lists: pgsql-bugs
The following bug has been logged on the website:
Bug reference: 16272
Logged by: Tom Gottfried
Email address: tom@intevation.de
PostgreSQL version: 11.7
Operating system: Ubuntu 18.04
Description:
Dear PostgreSQL developers,
consider the following to reproduce:
/* Works: */
CREATE TABLE test (
testp varchar,
testc varchar
);
CREATE INDEX test_idx ON test
((CAST((testp, testc) AS test)));
INSERT INTO test (testp) VALUES ('test');
CREATE TABLE test_ext (
newcol int,
LIKE test INCLUDING ALL
);
INSERT INTO test_ext SELECT 1, * FROM test;
/* Does not work: */
\set VERBOSITY verbose
CREATE TABLE test_parent (
testp varchar
);
CREATE TABLE test_child (
testc varchar
) INHERITS (test_parent);
CREATE INDEX test_child_idx ON test_child
((CAST((testp, testc) AS test_child)));
INSERT INTO test_child (testp) VALUES ('test');
CREATE TABLE test_parent_ext (
newcol int,
LIKE test_parent
);
CREATE TABLE test_child_ext (LIKE test_child INCLUDING INDEXES)
INHERITS (test_parent_ext);
/* =>
NOTICE: 00000: moving and merging column "testp" with inherited
definition
DETAIL: User-specified column moved to the position of the inherited
column.
LOCATION: MergeAttributes, tablecmds.c:2378
*/
INSERT INTO test_child_ext SELECT 1, * FROM test_child;
/* =>
ERROR: 42804: attribute 1 of type record has wrong type
DETAIL: Table has type integer, but query expects character varying.
LOCATION: CheckVarSlotCompatibility, execExprInterp.c:1898
*/
\d test_child_idx
\d test_child_ext_row_idx
/* =>
Index "public.test_child_idx"
Column | Type | Key? | Definition
--------+------------+------+---------------------------------
row | test_child | yes | (ROW(testp, testc)::test_child)
btree, for table "public.test_child"
Index "public.test_child_ext_row_idx"
Column | Type | Key? | Definition
--------+------------+------+----------------------------------
row | test_child | yes | (ROW(newcol, testp)::test_child)
btree, for table "public.test_child_ext"
*/
SELECT version();
/* =>
PostgreSQL 11.7 (Ubuntu 11.7-1.pgdg18.04+1) on x86_64-pc-linux-gnu, compiled
by gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0, 64-bit
*/
The index expression in the index created via LIKE ... INCLUDING INDEXES
still refers to the first two attributes of the table, although an attribute
has been put in place before the columns the expression referred to in the
original index.
I expected the new index expression to refer to the same (now
merged/inherited) columns as the original index (here: testp, testc) as it
actually does in the first example without inheritance.
Thanks and best regards,
Tom
Commits
-
Use the properly transformed RangeVar for expandTableLikeClause().
- f11210013d02 11.10 landed
- b380484a850b 13.0 landed
- 86b8ef139457 9.5.24 landed
- 783a21eff37e 10.15 landed
- 1a9c93ec3b9a 9.6.20 landed
- 19f5a37b9fc4 14.0 landed
- 1371a1e4161a 12.5 landed
-
Fix handling of CREATE TABLE LIKE with inheritance.
- 894f5dea76e1 13.0 landed
- 50289819230d 14.0 landed
- e22e29c258a1 10.15 landed
- d9253df12e4c 12.5 landed
- 37681d7e942e 9.6.20 landed
- 3737965249cd 11.10 landed
- 21f8b1811ac1 9.5.24 landed