]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix StatisticsObjIsVisibleExt() for pg_temp.
authorNoah Misch <noah@leadboat.com>
Mon, 29 Sep 2025 18:15:44 +0000 (11:15 -0700)
committerNoah Misch <noah@leadboat.com>
Mon, 29 Sep 2025 18:15:48 +0000 (11:15 -0700)
Neighbor get_statistics_object_oid() ignores objects in pg_temp, as has
been the standard for non-relation, non-type namespace searches since
CVE-2007-2138.  Hence, most operations that name a statistics object
correctly decline to map an unqualified name to a statistics object in
pg_temp.  StatisticsObjIsVisibleExt() did not.  Consequently,
pg_statistics_obj_is_visible() wrongly returned true for such objects,
psql \dX wrongly listed them, and getObjectDescription()-based ereport()
and pg_describe_object() wrongly omitted namespace qualification.  Any
malfunction beyond that would depend on how a human or application acts
on those wrong indications.  Commit
d99d58cdc8c0b5b50ee92995e8575c100b1a458a introduced this.  Back-patch to
v13 (all supported versions).

Reviewed-by: Nathan Bossart <nathandbossart@gmail.com>
Discussion: https://postgr.es/m/20250920162116.2e.nmisch@google.com
Backpatch-through: 13

src/backend/catalog/namespace.c
src/test/regress/expected/stats_ext.out
src/test/regress/sql/stats_ext.sql

index 2ea7c1393ffa91a8bdedc673a965871ef2ce1914..aa1d05eb57915677a5d1ddeeea3a143ecf3517c2 100644 (file)
@@ -2316,6 +2316,9 @@ StatisticsObjIsVisible(Oid relid)
                {
                        Oid                     namespaceId = lfirst_oid(l);
 
+                       if (namespaceId == myTempNamespace)
+                               continue;               /* do not look in temp namespace */
+
                        if (namespaceId == stxnamespace)
                        {
                                /* Found it first in path */
index dff364e4158098392df1f44101eeb05df74d3ea7..93e4efe92b6934938d4b83bca136aa1cb855b972 100644 (file)
@@ -97,6 +97,20 @@ ALTER STATISTICS ab1_a_b_stats RENAME TO ab1_a_b_stats_new;
 ERROR:  must be owner of statistics object ab1_a_b_stats
 RESET SESSION AUTHORIZATION;
 DROP ROLE regress_stats_ext;
+CREATE STATISTICS pg_temp.stats_ext_temp ON a, b FROM ab1;
+SELECT regexp_replace(pg_describe_object(tableoid, oid, 0),
+                      'pg_temp_[0-9]*', 'pg_temp_REDACTED') AS descr,
+       pg_statistics_obj_is_visible(oid) AS visible
+  FROM pg_statistic_ext
+ WHERE stxname = 'stats_ext_temp';
+                       descr                       | visible 
+---------------------------------------------------+---------
+ statistics object pg_temp_REDACTED.stats_ext_temp | f
+(1 row)
+
+DROP STATISTICS stats_ext_temp;  -- shall fail
+ERROR:  statistics object "stats_ext_temp" does not exist
+DROP STATISTICS pg_temp.stats_ext_temp;
 CREATE STATISTICS IF NOT EXISTS ab1_a_b_stats ON a, b FROM ab1;
 NOTICE:  statistics object "ab1_a_b_stats" already exists, skipping
 DROP STATISTICS ab1_a_b_stats;
index 03550a689533d3fa3f0192ee7d660f20e40b9763..279fe46c08545441c0a1d9d4086a78398ecf7093 100644 (file)
@@ -68,6 +68,14 @@ DROP STATISTICS ab1_a_b_stats;
 ALTER STATISTICS ab1_a_b_stats RENAME TO ab1_a_b_stats_new;
 RESET SESSION AUTHORIZATION;
 DROP ROLE regress_stats_ext;
+CREATE STATISTICS pg_temp.stats_ext_temp ON a, b FROM ab1;
+SELECT regexp_replace(pg_describe_object(tableoid, oid, 0),
+                      'pg_temp_[0-9]*', 'pg_temp_REDACTED') AS descr,
+       pg_statistics_obj_is_visible(oid) AS visible
+  FROM pg_statistic_ext
+ WHERE stxname = 'stats_ext_temp';
+DROP STATISTICS stats_ext_temp;  -- shall fail
+DROP STATISTICS pg_temp.stats_ext_temp;
 
 CREATE STATISTICS IF NOT EXISTS ab1_a_b_stats ON a, b FROM ab1;
 DROP STATISTICS ab1_a_b_stats;