Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d9abd9e

Browse files
committed
Add test for MAINTAIN permission with pg_restore_extended_stats()
Like its cousin functions for the restore of relation and attribute stats, pg_restore_extended_stats() needs to be run by a user that is the database owner or has MAINTAIN privileges on the table whose stats are restored. This commit adds a regression test ensuring that MAINTAIN is required when calling the function. This test also checks that a ShareUpdateExclusive lock is taken on the table whose stats are restored. This has been split from the commit that has introduced pg_restore_extended_stats(), for clarity. Author: Corey Huinker <[email protected]> Reviewed-by: Chao Li <[email protected]> Reviewed-by: Michael Paquier <[email protected]> Discussion: https://postgr.es/m/CADkLM=dpz3KFnqP-dgJ-zvRvtjsa8UZv8wDAQdqho=qN3kX0Zg@mail.gmail.com
1 parent 114e84c commit d9abd9e

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

src/test/regress/expected/stats_import.out

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,48 @@ WARNING: could not restore extended statistics object "stats_import"."test_stat
16851685
f
16861686
(1 row)
16871687

1688+
-- Check that MAINTAIN is required when restoring statistics.
1689+
CREATE ROLE regress_test_extstat_restore;
1690+
GRANT ALL ON SCHEMA stats_import TO regress_test_extstat_restore;
1691+
SET ROLE regress_test_extstat_restore;
1692+
-- No data to restore; this fails on a permission failure.
1693+
SELECT pg_catalog.pg_restore_extended_stats(
1694+
'schemaname', 'stats_import',
1695+
'relname', 'test_clone',
1696+
'statistics_schemaname', 'stats_import',
1697+
'statistics_name', 'test_stat_clone',
1698+
'inherited', false);
1699+
ERROR: permission denied for table test_clone
1700+
RESET ROLE;
1701+
GRANT MAINTAIN ON stats_import.test_clone TO regress_test_extstat_restore;
1702+
SET ROLE regress_test_extstat_restore;
1703+
-- This works, check the lock on the relation while on it.
1704+
BEGIN;
1705+
SELECT pg_catalog.pg_restore_extended_stats(
1706+
'schemaname', 'stats_import',
1707+
'relname', 'test_clone',
1708+
'statistics_schemaname', 'stats_import',
1709+
'statistics_name', 'test_stat_clone',
1710+
'inherited', false,
1711+
'n_distinct', '[{"attributes" : [2,3], "ndistinct" : 4}]'::pg_ndistinct);
1712+
pg_restore_extended_stats
1713+
---------------------------
1714+
t
1715+
(1 row)
1716+
1717+
SELECT mode FROM pg_locks WHERE locktype = 'relation' AND
1718+
relation = 'stats_import.test_clone'::regclass AND
1719+
pid = pg_backend_pid();
1720+
mode
1721+
--------------------------
1722+
ShareUpdateExclusiveLock
1723+
(1 row)
1724+
1725+
COMMIT;
1726+
RESET ROLE;
1727+
REVOKE MAINTAIN ON stats_import.test_clone FROM regress_test_extstat_restore;
1728+
REVOKE ALL ON SCHEMA stats_import FROM regress_test_extstat_restore;
1729+
DROP ROLE regress_test_extstat_restore;
16881730
-- ndistinct value doesn't match object definition
16891731
SELECT pg_catalog.pg_restore_extended_stats(
16901732
'schemaname', 'stats_import',

src/test/regress/sql/stats_import.sql

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,38 @@ SELECT pg_catalog.pg_restore_extended_stats(
12101210
'statistics_name', 'test_stat_clone',
12111211
'inherited', false);
12121212

1213+
-- Check that MAINTAIN is required when restoring statistics.
1214+
CREATE ROLE regress_test_extstat_restore;
1215+
GRANT ALL ON SCHEMA stats_import TO regress_test_extstat_restore;
1216+
SET ROLE regress_test_extstat_restore;
1217+
-- No data to restore; this fails on a permission failure.
1218+
SELECT pg_catalog.pg_restore_extended_stats(
1219+
'schemaname', 'stats_import',
1220+
'relname', 'test_clone',
1221+
'statistics_schemaname', 'stats_import',
1222+
'statistics_name', 'test_stat_clone',
1223+
'inherited', false);
1224+
RESET ROLE;
1225+
GRANT MAINTAIN ON stats_import.test_clone TO regress_test_extstat_restore;
1226+
SET ROLE regress_test_extstat_restore;
1227+
-- This works, check the lock on the relation while on it.
1228+
BEGIN;
1229+
SELECT pg_catalog.pg_restore_extended_stats(
1230+
'schemaname', 'stats_import',
1231+
'relname', 'test_clone',
1232+
'statistics_schemaname', 'stats_import',
1233+
'statistics_name', 'test_stat_clone',
1234+
'inherited', false,
1235+
'n_distinct', '[{"attributes" : [2,3], "ndistinct" : 4}]'::pg_ndistinct);
1236+
SELECT mode FROM pg_locks WHERE locktype = 'relation' AND
1237+
relation = 'stats_import.test_clone'::regclass AND
1238+
pid = pg_backend_pid();
1239+
COMMIT;
1240+
RESET ROLE;
1241+
REVOKE MAINTAIN ON stats_import.test_clone FROM regress_test_extstat_restore;
1242+
REVOKE ALL ON SCHEMA stats_import FROM regress_test_extstat_restore;
1243+
DROP ROLE regress_test_extstat_restore;
1244+
12131245
-- ndistinct value doesn't match object definition
12141246
SELECT pg_catalog.pg_restore_extended_stats(
12151247
'schemaname', 'stats_import',

0 commit comments

Comments
 (0)