From debfe72bd36d009a504aee6f4ba809e0780c105f Mon Sep 17 00:00:00 2001
From: Sami Imseih <simseih@amazon.com>
Date: Mon, 9 Mar 2026 12:36:35 -0500
Subject: [PATCH v3 3/3] Add tableid column to pg_stats_ext and
 pg_stats_ext_exprs

Same as dcccf721929, add a tableid column to pg_stats_ext and
pg_stats_ext_exprs.  For now, this is done to be consistent with
pg_stats, but there could also be future cases where having the
OID of the table will be useful.

Discussion: https://www.postgresql.org/message-id/aa7sZcuRrlkeVJGX%40nathan
---
 doc/src/sgml/system-views.sgml             | 20 ++++++++++++++++++++
 src/backend/catalog/system_views.sql       |  2 ++
 src/test/regress/expected/rules.out        |  2 ++
 src/test/regress/expected/stats_import.out |  4 ++--
 4 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/doc/src/sgml/system-views.sgml b/doc/src/sgml/system-views.sgml
index 8ec5a23e3cf..15ecadb493e 100644
--- a/doc/src/sgml/system-views.sgml
+++ b/doc/src/sgml/system-views.sgml
@@ -4686,6 +4686,16 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
       </para></entry>
      </row>
 
+     <row>
+      <entry role="catalog_table_entry"><para role="column_definition">
+       <structfield>tableid</structfield> <type>oid</type>
+       (references <link linkend="catalog-pg-class"><structname>pg_class</structname></link>.<structfield>oid</structfield>)
+      </para>
+      <para>
+       ID of the table
+      </para></entry>
+     </row>
+
      <row>
       <entry role="catalog_table_entry"><para role="column_definition">
        <structfield>statistics_schemaname</structfield> <type>name</type>
@@ -4897,6 +4907,16 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
       </para></entry>
      </row>
 
+     <row>
+      <entry role="catalog_table_entry"><para role="column_definition">
+       <structfield>tableid</structfield> <type>oid</type>
+       (references <link linkend="catalog-pg-class"><structname>pg_class</structname></link>.<structfield>oid</structfield>)
+      </para>
+      <para>
+       ID of the table
+      </para></entry>
+     </row>
+
      <row>
       <entry role="catalog_table_entry"><para role="column_definition">
        <structfield>statistics_schemaname</structfield> <type>name</type>
diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql
index 70846f1555d..20ed78fddb0 100644
--- a/src/backend/catalog/system_views.sql
+++ b/src/backend/catalog/system_views.sql
@@ -280,6 +280,7 @@ REVOKE ALL ON pg_statistic FROM public;
 CREATE VIEW pg_stats_ext WITH (security_barrier) AS
     SELECT cn.nspname AS schemaname,
            c.relname AS tablename,
+           c.oid AS tableid,
            sn.nspname AS statistics_schemaname,
            s.stxname AS statistics_name,
            pg_get_userbyid(s.stxowner) AS statistics_owner,
@@ -314,6 +315,7 @@ CREATE VIEW pg_stats_ext WITH (security_barrier) AS
 CREATE VIEW pg_stats_ext_exprs WITH (security_barrier) AS
     SELECT cn.nspname AS schemaname,
            c.relname AS tablename,
+           c.oid AS tableid,
            sn.nspname AS statistics_schemaname,
            s.stxname AS statistics_name,
            pg_get_userbyid(s.stxowner) AS statistics_owner,
diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out
index 46be5c7ac90..7bc7fab7191 100644
--- a/src/test/regress/expected/rules.out
+++ b/src/test/regress/expected/rules.out
@@ -2618,6 +2618,7 @@ pg_stats| SELECT n.nspname AS schemaname,
   WHERE ((NOT a.attisdropped) AND has_column_privilege(c.oid, a.attnum, 'select'::text) AND ((c.relrowsecurity = false) OR (NOT row_security_active(c.oid))));
 pg_stats_ext| SELECT cn.nspname AS schemaname,
     c.relname AS tablename,
+    c.oid AS tableid,
     sn.nspname AS statistics_schemaname,
     s.stxname AS statistics_name,
     pg_get_userbyid(s.stxowner) AS statistics_owner,
@@ -2646,6 +2647,7 @@ pg_stats_ext| SELECT cn.nspname AS schemaname,
   WHERE (pg_has_role(c.relowner, 'USAGE'::text) AND ((c.relrowsecurity = false) OR (NOT row_security_active(c.oid))));
 pg_stats_ext_exprs| SELECT cn.nspname AS schemaname,
     c.relname AS tablename,
+    c.oid AS tableid,
     sn.nspname AS statistics_schemaname,
     s.stxname AS statistics_name,
     pg_get_userbyid(s.stxowner) AS statistics_owner,
diff --git a/src/test/regress/expected/stats_import.out b/src/test/regress/expected/stats_import.out
index 8d66d4d9329..fa1ba01f272 100644
--- a/src/test/regress/expected/stats_import.out
+++ b/src/test/regress/expected/stats_import.out
@@ -66,7 +66,7 @@ SELECT COUNT(*) FROM pg_attribute
     attnum > 0;
  count 
 -------
-    15
+    16
 (1 row)
 
 -- Create a view that is used purely for the type based on pg_stats_ext.
@@ -107,7 +107,7 @@ SELECT COUNT(*) FROM pg_attribute
     attnum > 0;
  count 
 -------
-    20
+    21
 (1 row)
 
 -- Create a view that is used purely for the type based on pg_stats_ext_exprs.
-- 
2.50.1 (Apple Git-155)

