pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Julien Tachoires <julmon@gmail.com>
Subject: [PATCH 1/2] Add test for CTL ... INCLUDING STATS
Date: Wed, 15 Apr 2026 12:35:32 +0200

With the help of this new test we ensure that a table created using
CREATE TABLE ... LIKE ... INCLUDING STATS; will get the statistics
object of the parent properly cloned.
---
 .../regress/expected/create_table_like.out    | 25 +++++++++++++++++++
 src/test/regress/sql/create_table_like.sql    | 21 ++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/src/test/regress/expected/create_table_like.out b/src/test/regress/expected/create_table_like.out
index 5720d160f05..e6637ee8344 100644
--- a/src/test/regress/expected/create_table_like.out
+++ b/src/test/regress/expected/create_table_like.out
@@ -698,6 +698,31 @@ SELECT attname, attcompression FROM pg_attribute
  e       | 
 (5 rows)
 
+-- Test CREATE TABLE  ... LIKE ... INCLUDING STATISTICS
+-- When the parent table definitions changed before creating statistics
+CREATE TABLE parent_like_stats (a int, b int, c int);
+ALTER TABLE parent_like_stats DROP COLUMN b;
+CREATE STATISTICS s_parent ON a, c FROM parent_like_stats;
+CREATE TABLE child_like_stats (LIKE parent_like_stats INCLUDING STATISTICS);
+-- Verify what columns the cloned statistics cover: both columns must be 'a'
+-- and 'c'
+SELECT
+  stxname,
+  array_agg(a.attname ORDER BY u.ord) AS stats_columns
+FROM pg_statistic_ext s
+CROSS JOIN LATERAL
+  unnest(s.stxkeys::int2[]) WITH ORDINALITY AS u(attnum, ord)
+JOIN pg_attribute a
+  ON a.attrelid = s.stxrelid AND a.attnum = u.attnum
+WHERE s.stxrelid = 'child_like_stats'::regclass
+GROUP BY stxname;
+          stxname          | stats_columns 
+---------------------------+---------------
+ child_like_stats_a_c_stat | {a,c}
+(1 row)
+
+DROP TABLE parent_like_stats CASCADE;
+DROP TABLE child_like_stats;
 DROP TABLE ctl_table;
 DROP FOREIGN TABLE ctl_foreign_table1;
 DROP FOREIGN TABLE ctl_foreign_table2;
diff --git a/src/test/regress/sql/create_table_like.sql b/src/test/regress/sql/create_table_like.sql
index 93389b57dbf..63fdaab0d2e 100644
--- a/src/test/regress/sql/create_table_like.sql
+++ b/src/test/regress/sql/create_table_like.sql
@@ -276,6 +276,27 @@ CREATE FOREIGN TABLE ctl_foreign_table2(LIKE ctl_table INCLUDING ALL) SERVER ctl
 SELECT attname, attcompression FROM pg_attribute
   WHERE attrelid = 'ctl_foreign_table2'::regclass and attnum > 0 ORDER BY attnum;
 
+-- Test CREATE TABLE  ... LIKE ... INCLUDING STATISTICS
+-- When the parent table definitions changed before creating statistics
+CREATE TABLE parent_like_stats (a int, b int, c int);
+ALTER TABLE parent_like_stats DROP COLUMN b;
+CREATE STATISTICS s_parent ON a, c FROM parent_like_stats;
+CREATE TABLE child_like_stats (LIKE parent_like_stats INCLUDING STATISTICS);
+-- Verify what columns the cloned statistics cover: both columns must be 'a'
+-- and 'c'
+SELECT
+  stxname,
+  array_agg(a.attname ORDER BY u.ord) AS stats_columns
+FROM pg_statistic_ext s
+CROSS JOIN LATERAL
+  unnest(s.stxkeys::int2[]) WITH ORDINALITY AS u(attnum, ord)
+JOIN pg_attribute a
+  ON a.attrelid = s.stxrelid AND a.attnum = u.attnum
+WHERE s.stxrelid = 'child_like_stats'::regclass
+GROUP BY stxname;
+DROP TABLE parent_like_stats CASCADE;
+DROP TABLE child_like_stats;
+
 DROP TABLE ctl_table;
 DROP FOREIGN TABLE ctl_foreign_table1;
 DROP FOREIGN TABLE ctl_foreign_table2;
-- 
2.51.2


--ly2yk5uozlo3oxwh
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment;
	filename="v1-0002-Fix-attnum-remapping-in-generateClonedExtStatsStmt.patch"



Message-ID: <no-message-id-25310@localhost>
Permalink:  ../no-message-id-25310@localhost/
Also on:    postgresql.org/message-id/no-message-id-25310@localhost

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: julmon@gmail.com
  Subject: Re: [PATCH 1/2] Add test for CTL ... INCLUDING STATS
  In-Reply-To: <no-message-id-25310@localhost>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox