From e2e8d84fb6facb71cdb508162466ffbd5822acf4 Mon Sep 17 00:00:00 2001
From: Corey Huinker <corey.huinker@gmail.com>
Date: Sun, 25 Jan 2026 05:12:54 -0500
Subject: [PATCH v31 8/9] Add privs tests for pg_restore_extended_stats.

---
 src/test/regress/expected/stats_import.out | 53 ++++++++++++++++++++++
 src/test/regress/sql/stats_import.sql      | 41 +++++++++++++++++
 2 files changed, 94 insertions(+)

diff --git a/src/test/regress/expected/stats_import.out b/src/test/regress/expected/stats_import.out
index 57a7cb1a7d8..ee5ed8dfceb 100644
--- a/src/test/regress/expected/stats_import.out
+++ b/src/test/regress/expected/stats_import.out
@@ -1614,6 +1614,59 @@ WHERE e.schemaname = 'stats_import';
 (1 row)
 
 -- Tests for pg_restore_extended_stats().
+-- Check that MAINTAIN is required when restoring statistics.
+CREATE ROLE regress_test_extstat_restore;
+GRANT ALL ON SCHEMA stats_import TO regress_test_extstat_restore;
+SET ROLE regress_test_extstat_restore;
+-- No data restore, but it does not matter as this fails on a permission
+-- failure.
+SELECT pg_catalog.pg_restore_extended_stats(
+  'schemaname', 'stats_import',
+  'relname', 'test_clone',
+  'statistics_schemaname', 'stats_import',
+  'statistics_name', 'test_stat_clone',
+  'inherited', false);
+ERROR:  permission denied for table test_clone
+RESET ROLE;
+GRANT MAINTAIN ON stats_import.test_clone TO regress_test_extstat_clear;
+ERROR:  role "regress_test_extstat_clear" does not exist
+SET ROLE regress_test_extstat_clear;
+ERROR:  role "regress_test_extstat_clear" does not exist
+-- This works, check the lock on the relation while on it.
+BEGIN;
+SELECT pg_catalog.pg_restore_extended_stats(
+  'schemaname', 'stats_import',
+  'relname', 'test_clone',
+  'statistics_schemaname', 'stats_import',
+  'statistics_name', 'test_stat_clone',
+  'inherited', false,
+  'n_distinct', '[{"attributes" : [2,3], "ndistinct" : 4},
+                  {"attributes" : [2,-1], "ndistinct" : 4},
+                  {"attributes" : [3,-1], "ndistinct" : 4},
+                  {"attributes" : [3,-2], "ndistinct" : 4},
+                  {"attributes" : [-1,-2], "ndistinct" : 3},
+                  {"attributes" : [2,3,-1], "ndistinct" : 4},
+                  {"attributes" : [2,3,-2], "ndistinct" : 4},
+                  {"attributes" : [2,-1,-2], "ndistinct" : 4},
+                  {"attributes" : [2,3,-1,-2], "ndistinct" : 4}]'::pg_ndistinct);
+ pg_restore_extended_stats 
+---------------------------
+ t
+(1 row)
+
+SELECT mode FROM pg_locks WHERE locktype = 'relation' AND
+  relation = 'stats_import.test_clone'::regclass AND
+  pid = pg_backend_pid();
+           mode           
+--------------------------
+ ShareUpdateExclusiveLock
+(1 row)
+
+COMMIT;
+RESET ROLE;
+REVOKE MAINTAIN ON stats_import.test_clone FROM regress_test_extstat_restore;
+REVOKE ALL ON SCHEMA stats_import FROM regress_test_extstat_restore;
+DROP ROLE regress_test_extstat_restore;
 --  Invalid argument values.
 SELECT pg_catalog.pg_restore_extended_stats(
   'schemaname', NULL,
diff --git a/src/test/regress/sql/stats_import.sql b/src/test/regress/sql/stats_import.sql
index 3fcdf9a78a4..a4f91c04d00 100644
--- a/src/test/regress/sql/stats_import.sql
+++ b/src/test/regress/sql/stats_import.sql
@@ -1160,6 +1160,47 @@ CROSS JOIN LATERAL pg_catalog.pg_clear_extended_stats(
 WHERE e.schemaname = 'stats_import';
 
 -- Tests for pg_restore_extended_stats().
+-- Check that MAINTAIN is required when restoring statistics.
+CREATE ROLE regress_test_extstat_restore;
+GRANT ALL ON SCHEMA stats_import TO regress_test_extstat_restore;
+SET ROLE regress_test_extstat_restore;
+-- No data restore, but it does not matter as this fails on a permission
+-- failure.
+SELECT pg_catalog.pg_restore_extended_stats(
+  'schemaname', 'stats_import',
+  'relname', 'test_clone',
+  'statistics_schemaname', 'stats_import',
+  'statistics_name', 'test_stat_clone',
+  'inherited', false);
+RESET ROLE;
+GRANT MAINTAIN ON stats_import.test_clone TO regress_test_extstat_clear;
+SET ROLE regress_test_extstat_clear;
+-- This works, check the lock on the relation while on it.
+BEGIN;
+SELECT pg_catalog.pg_restore_extended_stats(
+  'schemaname', 'stats_import',
+  'relname', 'test_clone',
+  'statistics_schemaname', 'stats_import',
+  'statistics_name', 'test_stat_clone',
+  'inherited', false,
+  'n_distinct', '[{"attributes" : [2,3], "ndistinct" : 4},
+                  {"attributes" : [2,-1], "ndistinct" : 4},
+                  {"attributes" : [3,-1], "ndistinct" : 4},
+                  {"attributes" : [3,-2], "ndistinct" : 4},
+                  {"attributes" : [-1,-2], "ndistinct" : 3},
+                  {"attributes" : [2,3,-1], "ndistinct" : 4},
+                  {"attributes" : [2,3,-2], "ndistinct" : 4},
+                  {"attributes" : [2,-1,-2], "ndistinct" : 4},
+                  {"attributes" : [2,3,-1,-2], "ndistinct" : 4}]'::pg_ndistinct);
+SELECT mode FROM pg_locks WHERE locktype = 'relation' AND
+  relation = 'stats_import.test_clone'::regclass AND
+  pid = pg_backend_pid();
+COMMIT;
+RESET ROLE;
+REVOKE MAINTAIN ON stats_import.test_clone FROM regress_test_extstat_restore;
+REVOKE ALL ON SCHEMA stats_import FROM regress_test_extstat_restore;
+DROP ROLE regress_test_extstat_restore;
+
 --  Invalid argument values.
 SELECT pg_catalog.pg_restore_extended_stats(
   'schemaname', NULL,
-- 
2.52.0

