From ff199dee22838be31a1428cec7f983f3f8e16ff6 Mon Sep 17 00:00:00 2001
From: Julien Tachoires <julmon@gmail.com>
Date: Wed, 15 Apr 2026 12:35:32 +0200
Subject: [PATCH 1/2] Add test for CTL ... INCLUDING STATS

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

