BUG #16272: Index expression can refer to wrong attributes if index is created via CREATE TABLE LIKE

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: tom(at)intevation(dot)de
Subject: BUG #16272: Index expression can refer to wrong attributes if index is created via CREATE TABLE LIKE
Date: 2020-02-21 19:37:55
Message-ID: 16272-6e32da020e9a9381@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 16272
Logged by: Tom Gottfried
Email address: tom(at)intevation(dot)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

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message duvall 2020-02-21 19:45:21 Re: CREATE TABLE IF NOT EXISTS fails with privilege exception when table exists
Previous Message Dmitry Dolgov 2020-02-21 18:58:03 Re: [Bus error] huge_pages default value (try) not fall back