diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2024-05-22 17:54:17 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2024-05-22 17:54:17 -0400 |
commit | 2aa90c02dc6983bf0066bf6df18b713fde916cf7 (patch) | |
tree | 2a0c934cc104f66802b781b5aa1a810b45eff76f /src/test | |
parent | ce0d1654463e4f6080cfe83df74ed978945608a1 (diff) | |
download | postgresql-2aa90c02dc6983bf0066bf6df18b713fde916cf7.tar.gz postgresql-2aa90c02dc6983bf0066bf6df18b713fde916cf7.zip |
Fix handling of extended expression statistics in CREATE TABLE LIKE.
transformTableLikeClause believed that it could process extended
statistics immediately because "the representation of CreateStatsStmt
doesn't depend on column numbers". That was true when extended stats
were first introduced, but it was falsified by the addition of
extended stats on expressions: the parsed expression tree is fed
forward by the LIKE option, and that will contain Vars. So if the
new table doesn't have attnums identical to the old one's (typically
because there are some dropped columns in the old one), that doesn't
work. The CREATE goes through, but it emits invalid statistics
objects that will cause problems later.
Fortunately, we already have logic that can adapt expression trees
to the possibly-new column numbering. To use it, we have to delay
processing of CREATE_TABLE_LIKE_STATISTICS into expandTableLikeClause,
just as for other LIKE options that involve expressions.
Per bug #18468 from Alexander Lakhin. Back-patch to v14 where
extended statistics on expressions were added.
Discussion: https://postgr.es/m/18468-f5add190e3fa5902@postgresql.org
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/regress/expected/create_table_like.out | 15 | ||||
-rw-r--r-- | src/test/regress/sql/create_table_like.sql | 8 |
2 files changed, 23 insertions, 0 deletions
diff --git a/src/test/regress/expected/create_table_like.out b/src/test/regress/expected/create_table_like.out index 0ed94f1d2fb..6bfc6d040ff 100644 --- a/src/test/regress/expected/create_table_like.out +++ b/src/test/regress/expected/create_table_like.out @@ -261,8 +261,23 @@ Check constraints: Inherits: test_like_5, test_like_5x +-- Test updating of column numbers in statistics expressions (bug #18468) +CREATE TABLE test_like_6 (a int, c text, b text); +CREATE STATISTICS ext_stat ON (a || b) FROM test_like_6; +ALTER TABLE test_like_6 DROP COLUMN c; +CREATE TABLE test_like_6c (LIKE test_like_6 INCLUDING ALL); +\d+ test_like_6c + Table "public.test_like_6c" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + a | integer | | | | plain | | + b | text | | | | extended | | +Statistics objects: + "public.test_like_6c_expr_stat" ON (a || b) FROM test_like_6c + DROP TABLE test_like_4, test_like_4a, test_like_4b, test_like_4c, test_like_4d; DROP TABLE test_like_5, test_like_5x, test_like_5c; +DROP TABLE test_like_6, test_like_6c; CREATE TABLE inhg (x text, LIKE inhx INCLUDING INDEXES, y text); /* copies indexes */ INSERT INTO inhg VALUES (5, 10); INSERT INTO inhg VALUES (20, 10); -- should fail diff --git a/src/test/regress/sql/create_table_like.sql b/src/test/regress/sql/create_table_like.sql index 4929d373a2f..04008a027b8 100644 --- a/src/test/regress/sql/create_table_like.sql +++ b/src/test/regress/sql/create_table_like.sql @@ -95,8 +95,16 @@ CREATE TABLE test_like_5c (LIKE test_like_4 INCLUDING ALL) INHERITS (test_like_5, test_like_5x); \d test_like_5c +-- Test updating of column numbers in statistics expressions (bug #18468) +CREATE TABLE test_like_6 (a int, c text, b text); +CREATE STATISTICS ext_stat ON (a || b) FROM test_like_6; +ALTER TABLE test_like_6 DROP COLUMN c; +CREATE TABLE test_like_6c (LIKE test_like_6 INCLUDING ALL); +\d+ test_like_6c + DROP TABLE test_like_4, test_like_4a, test_like_4b, test_like_4c, test_like_4d; DROP TABLE test_like_5, test_like_5x, test_like_5c; +DROP TABLE test_like_6, test_like_6c; CREATE TABLE inhg (x text, LIKE inhx INCLUDING INDEXES, y text); /* copies indexes */ INSERT INTO inhg VALUES (5, 10); |