2 -- Tests for privileges on GUCs.
3 -- This is unsafe because changes will affect other databases in the cluster.
5 -- Test with a superuser role.
6 CREATE ROLE regress_admin SUPERUSER;
7 -- Perform operations as user 'regress_admin'.
8 SET SESSION AUTHORIZATION regress_admin;
10 SET ignore_system_indexes = OFF; -- fail, cannot be set after connection start
11 ERROR: parameter "ignore_system_indexes" cannot be set after connection start
12 RESET ignore_system_indexes; -- fail, cannot be set after connection start
13 ERROR: parameter "ignore_system_indexes" cannot be set after connection start
14 ALTER SYSTEM SET ignore_system_indexes = OFF; -- ok
15 ALTER SYSTEM RESET ignore_system_indexes; -- ok
17 SET block_size = 50; -- fail, cannot be changed
18 ERROR: parameter "block_size" cannot be changed
19 RESET block_size; -- fail, cannot be changed
20 ERROR: parameter "block_size" cannot be changed
21 ALTER SYSTEM SET block_size = 50; -- fail, cannot be changed
22 ERROR: parameter "block_size" cannot be changed
23 ALTER SYSTEM RESET block_size; -- fail, cannot be changed
24 ERROR: parameter "block_size" cannot be changed
26 SET autovacuum_freeze_max_age = 1000050000; -- fail, requires restart
27 ERROR: parameter "autovacuum_freeze_max_age" cannot be changed without restarting the server
28 RESET autovacuum_freeze_max_age; -- fail, requires restart
29 ERROR: parameter "autovacuum_freeze_max_age" cannot be changed without restarting the server
30 ALTER SYSTEM SET autovacuum_freeze_max_age = 1000050000; -- ok
31 ALTER SYSTEM RESET autovacuum_freeze_max_age; -- ok
32 ALTER SYSTEM SET config_file = '/usr/local/data/postgresql.conf'; -- fail, cannot be changed
33 ERROR: parameter "config_file" cannot be changed
34 ALTER SYSTEM RESET config_file; -- fail, cannot be changed
35 ERROR: parameter "config_file" cannot be changed
37 SET autovacuum = OFF; -- fail, requires reload
38 ERROR: parameter "autovacuum" cannot be changed now
39 RESET autovacuum; -- fail, requires reload
40 ERROR: parameter "autovacuum" cannot be changed now
41 ALTER SYSTEM SET autovacuum = OFF; -- ok
42 ALTER SYSTEM RESET autovacuum; -- ok
44 SET lc_messages = 'C'; -- ok
45 RESET lc_messages; -- ok
46 ALTER SYSTEM SET lc_messages = 'C'; -- ok
47 ALTER SYSTEM RESET lc_messages; -- ok
49 SET jit_debugging_support = OFF; -- fail, cannot be set after connection start
50 ERROR: parameter "jit_debugging_support" cannot be set after connection start
51 RESET jit_debugging_support; -- fail, cannot be set after connection start
52 ERROR: parameter "jit_debugging_support" cannot be set after connection start
53 ALTER SYSTEM SET jit_debugging_support = OFF; -- ok
54 ALTER SYSTEM RESET jit_debugging_support; -- ok
56 SET DateStyle = 'ISO, MDY'; -- ok
57 RESET DateStyle; -- ok
58 ALTER SYSTEM SET DateStyle = 'ISO, MDY'; -- ok
59 ALTER SYSTEM RESET DateStyle; -- ok
60 ALTER SYSTEM SET ssl_renegotiation_limit = 0; -- fail, cannot be changed
61 ERROR: parameter "ssl_renegotiation_limit" cannot be changed
62 ALTER SYSTEM RESET ssl_renegotiation_limit; -- fail, cannot be changed
63 ERROR: parameter "ssl_renegotiation_limit" cannot be changed
64 -- Finished testing superuser
65 -- Create non-superuser with privileges to configure host resource usage
66 CREATE ROLE regress_host_resource_admin NOSUPERUSER;
67 -- Revoke privileges not yet granted
68 REVOKE SET, ALTER SYSTEM ON PARAMETER work_mem FROM regress_host_resource_admin;
69 REVOKE SET, ALTER SYSTEM ON PARAMETER zero_damaged_pages FROM regress_host_resource_admin;
70 -- Check the new role does not yet have privileges on parameters
71 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'SET, ALTER SYSTEM');
72 has_parameter_privilege
73 -------------------------
77 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'SET');
78 has_parameter_privilege
79 -------------------------
83 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'ALTER SYSTEM');
84 has_parameter_privilege
85 -------------------------
89 -- Check inappropriate and nonsense privilege types
90 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'SELECT, UPDATE, CREATE');
91 ERROR: unrecognized privilege type: "SELECT"
92 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'USAGE');
93 ERROR: unrecognized privilege type: "USAGE"
94 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'WHATEVER');
95 ERROR: unrecognized privilege type: "WHATEVER"
96 -- Revoke, grant, and revoke again a SUSET parameter not yet granted
97 SELECT has_parameter_privilege('regress_host_resource_admin', 'zero_damaged_pages', 'SET');
98 has_parameter_privilege
99 -------------------------
103 SELECT has_parameter_privilege('regress_host_resource_admin', 'zero_damaged_pages', 'ALTER SYSTEM');
104 has_parameter_privilege
105 -------------------------
109 REVOKE SET ON PARAMETER zero_damaged_pages FROM regress_host_resource_admin;
110 SELECT has_parameter_privilege('regress_host_resource_admin', 'zero_damaged_pages', 'SET');
111 has_parameter_privilege
112 -------------------------
116 SELECT has_parameter_privilege('regress_host_resource_admin', 'zero_damaged_pages', 'ALTER SYSTEM');
117 has_parameter_privilege
118 -------------------------
122 GRANT SET ON PARAMETER zero_damaged_pages TO regress_host_resource_admin;
123 SELECT has_parameter_privilege('regress_host_resource_admin', 'zero_damaged_pages', 'SET');
124 has_parameter_privilege
125 -------------------------
129 SELECT has_parameter_privilege('regress_host_resource_admin', 'zero_damaged_pages', 'ALTER SYSTEM');
130 has_parameter_privilege
131 -------------------------
135 REVOKE SET ON PARAMETER zero_damaged_pages FROM regress_host_resource_admin;
136 SELECT has_parameter_privilege('regress_host_resource_admin', 'zero_damaged_pages', 'SET');
137 has_parameter_privilege
138 -------------------------
142 SELECT has_parameter_privilege('regress_host_resource_admin', 'zero_damaged_pages', 'ALTER SYSTEM');
143 has_parameter_privilege
144 -------------------------
148 -- Revoke, grant, and revoke again a USERSET parameter not yet granted
149 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'SET');
150 has_parameter_privilege
151 -------------------------
155 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'ALTER SYSTEM');
156 has_parameter_privilege
157 -------------------------
161 REVOKE SET ON PARAMETER work_mem FROM regress_host_resource_admin;
162 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'SET');
163 has_parameter_privilege
164 -------------------------
168 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'ALTER SYSTEM');
169 has_parameter_privilege
170 -------------------------
174 GRANT SET ON PARAMETER work_mem TO regress_host_resource_admin;
175 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'SET');
176 has_parameter_privilege
177 -------------------------
181 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'ALTER SYSTEM');
182 has_parameter_privilege
183 -------------------------
187 REVOKE SET ON PARAMETER work_mem FROM regress_host_resource_admin;
188 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'SET');
189 has_parameter_privilege
190 -------------------------
194 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'ALTER SYSTEM');
195 has_parameter_privilege
196 -------------------------
200 -- Revoke privileges from a non-existent custom GUC. This should not create
201 -- entries in the catalog.
202 REVOKE ALL ON PARAMETER "none.such" FROM regress_host_resource_admin;
203 SELECT 1 FROM pg_parameter_acl WHERE parname = 'none.such';
208 -- Grant and then revoke privileges on the non-existent custom GUC. Check that
209 -- a do-nothing entry is not left in the catalogs after the revoke.
210 GRANT ALL ON PARAMETER none.such TO regress_host_resource_admin;
211 SELECT 1 FROM pg_parameter_acl WHERE parname = 'none.such';
217 REVOKE ALL ON PARAMETER "None.Such" FROM regress_host_resource_admin;
218 SELECT 1 FROM pg_parameter_acl WHERE parname = 'none.such';
223 -- Superuser should be able to ALTER SYSTEM SET a non-existent custom GUC.
224 ALTER SYSTEM SET none.such = 'whiz bang';
225 -- None of the above should have created a placeholder GUC for none.such.
226 SHOW none.such; -- error
227 ERROR: unrecognized configuration parameter "none.such"
228 -- However, if we reload ...
229 SELECT pg_reload_conf();
235 -- and start a new session to avoid race condition ...
237 SET SESSION AUTHORIZATION regress_admin;
238 -- then it should be there.
245 -- Can't grant on a non-existent core GUC.
246 GRANT ALL ON PARAMETER no_such_guc TO regress_host_resource_admin; -- fail
247 ERROR: unrecognized configuration parameter "no_such_guc"
248 -- Initially there are no privileges and no catalog entry for this GUC.
249 SELECT has_parameter_privilege('regress_host_resource_admin', 'enable_material', 'SET');
250 has_parameter_privilege
251 -------------------------
255 SELECT has_parameter_privilege('regress_host_resource_admin', 'enable_material', 'ALTER SYSTEM');
256 has_parameter_privilege
257 -------------------------
261 SELECT has_parameter_privilege('regress_host_resource_admin', 'enable_material', 'SET, ALTER SYSTEM');
262 has_parameter_privilege
263 -------------------------
267 SELECT 1 FROM pg_parameter_acl WHERE parname = 'enable_material';
272 -- GRANT SET creates an entry:
273 GRANT SET ON PARAMETER enable_material TO PUBLIC;
274 SELECT has_parameter_privilege('regress_host_resource_admin', 'enable_material', 'SET');
275 has_parameter_privilege
276 -------------------------
280 SELECT has_parameter_privilege('regress_host_resource_admin', 'enable_material', 'ALTER SYSTEM');
281 has_parameter_privilege
282 -------------------------
286 SELECT has_parameter_privilege('regress_host_resource_admin', 'enable_material', 'SET, ALTER SYSTEM');
287 has_parameter_privilege
288 -------------------------
292 SELECT 1 FROM pg_parameter_acl WHERE parname = 'enable_material';
298 -- Now grant ALTER SYSTEM:
299 GRANT ALL ON PARAMETER enable_material TO PUBLIC;
300 SELECT has_parameter_privilege('regress_host_resource_admin', 'enable_material', 'SET');
301 has_parameter_privilege
302 -------------------------
306 SELECT has_parameter_privilege('regress_host_resource_admin', 'enable_material', 'ALTER SYSTEM');
307 has_parameter_privilege
308 -------------------------
312 SELECT has_parameter_privilege('regress_host_resource_admin', 'enable_material', 'SET, ALTER SYSTEM');
313 has_parameter_privilege
314 -------------------------
318 SELECT 1 FROM pg_parameter_acl WHERE parname = 'enable_material';
324 -- REVOKE ALTER SYSTEM brings us back to just the SET privilege:
325 REVOKE ALTER SYSTEM ON PARAMETER enable_material FROM PUBLIC;
326 SELECT has_parameter_privilege('regress_host_resource_admin', 'enable_material', 'SET');
327 has_parameter_privilege
328 -------------------------
332 SELECT has_parameter_privilege('regress_host_resource_admin', 'enable_material', 'ALTER SYSTEM');
333 has_parameter_privilege
334 -------------------------
338 SELECT has_parameter_privilege('regress_host_resource_admin', 'enable_material', 'SET, ALTER SYSTEM');
339 has_parameter_privilege
340 -------------------------
344 SELECT 1 FROM pg_parameter_acl WHERE parname = 'enable_material';
350 -- And this should remove the entry altogether:
351 REVOKE SET ON PARAMETER enable_material FROM PUBLIC;
352 SELECT 1 FROM pg_parameter_acl WHERE parname = 'enable_material';
357 -- Grant privileges on parameters to the new non-superuser role
358 GRANT SET, ALTER SYSTEM ON PARAMETER
359 autovacuum_work_mem, hash_mem_multiplier, max_stack_depth,
360 shared_buffers, temp_file_limit, work_mem
361 TO regress_host_resource_admin;
362 -- Check the new role now has privileges on parameters
363 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'SET, ALTER SYSTEM');
364 has_parameter_privilege
365 -------------------------
369 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'SET');
370 has_parameter_privilege
371 -------------------------
375 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'ALTER SYSTEM');
376 has_parameter_privilege
377 -------------------------
381 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'SET WITH GRANT OPTION, ALTER SYSTEM WITH GRANT OPTION');
382 has_parameter_privilege
383 -------------------------
387 -- Check again the inappropriate and nonsense privilege types. The prior
388 -- similar check was performed before any entry for work_mem existed.
389 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'SELECT, UPDATE, CREATE');
390 ERROR: unrecognized privilege type: "SELECT"
391 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'USAGE');
392 ERROR: unrecognized privilege type: "USAGE"
393 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'WHATEVER');
394 ERROR: unrecognized privilege type: "WHATEVER"
395 SELECT has_parameter_privilege('regress_host_resource_admin', 'work_mem', 'WHATEVER WITH GRANT OPTION');
396 ERROR: unrecognized privilege type: "WHATEVER WITH GRANT OPTION"
397 -- Check other function signatures
398 SELECT has_parameter_privilege((SELECT oid FROM pg_catalog.pg_authid WHERE rolname = 'regress_host_resource_admin'),
401 has_parameter_privilege
402 -------------------------
406 SELECT has_parameter_privilege('hash_mem_multiplier', 'set');
407 has_parameter_privilege
408 -------------------------
412 -- Check object identity functions
413 SELECT pg_describe_object(tableoid, oid, 0)
414 FROM pg_parameter_acl WHERE parname = 'work_mem';
420 SELECT pg_identify_object(tableoid, oid, 0)
421 FROM pg_parameter_acl WHERE parname = 'work_mem';
423 ------------------------------
424 ("parameter ACL",,,work_mem)
427 SELECT pg_identify_object_as_address(tableoid, oid, 0)
428 FROM pg_parameter_acl WHERE parname = 'work_mem';
429 pg_identify_object_as_address
430 ---------------------------------
431 ("parameter ACL",{work_mem},{})
434 SELECT classid::regclass,
435 (SELECT parname FROM pg_parameter_acl WHERE oid = goa.objid) AS parname,
437 FROM pg_get_object_address('parameter ACL', '{work_mem}', '{}') goa;
438 classid | parname | objsubid
439 ------------------+----------+----------
440 pg_parameter_acl | work_mem | 0
443 -- Make a per-role setting that regress_host_resource_admin can't change
444 ALTER ROLE regress_host_resource_admin SET lc_messages = 'C';
445 -- Perform some operations as user 'regress_host_resource_admin'
446 SET SESSION AUTHORIZATION regress_host_resource_admin;
447 ALTER SYSTEM SET autovacuum_work_mem = 32; -- ok, privileges have been granted
448 ALTER SYSTEM SET ignore_system_indexes = OFF; -- fail, insufficient privileges
449 ERROR: permission denied to set parameter "ignore_system_indexes"
450 ALTER SYSTEM RESET autovacuum_multixact_freeze_max_age; -- fail, insufficient privileges
451 ERROR: permission denied to set parameter "autovacuum_multixact_freeze_max_age"
452 SET jit_provider = 'llvmjit'; -- fail, insufficient privileges
453 ERROR: parameter "jit_provider" cannot be changed without restarting the server
454 SELECT set_config ('jit_provider', 'llvmjit', true); -- fail, insufficient privileges
455 ERROR: parameter "jit_provider" cannot be changed without restarting the server
456 ALTER SYSTEM SET shared_buffers = 50; -- ok
457 ALTER SYSTEM RESET shared_buffers; -- ok
458 SET autovacuum_work_mem = 50; -- cannot be changed now
459 ERROR: parameter "autovacuum_work_mem" cannot be changed now
460 ALTER SYSTEM RESET temp_file_limit; -- ok
461 SET TimeZone = 'Europe/Helsinki'; -- ok
462 RESET TimeZone; -- ok
463 SET max_stack_depth = '100kB'; -- ok, privileges have been granted
464 RESET max_stack_depth; -- ok, privileges have been granted
465 ALTER SYSTEM SET max_stack_depth = '100kB'; -- ok, privileges have been granted
466 ALTER SYSTEM RESET max_stack_depth; -- ok, privileges have been granted
467 SET lc_messages = 'C'; -- fail, insufficient privileges
468 ERROR: permission denied to set parameter "lc_messages"
469 RESET lc_messages; -- fail, insufficient privileges
470 ERROR: permission denied to set parameter "lc_messages"
471 ALTER SYSTEM SET lc_messages = 'C'; -- fail, insufficient privileges
472 ERROR: permission denied to set parameter "lc_messages"
473 ALTER SYSTEM RESET lc_messages; -- fail, insufficient privileges
474 ERROR: permission denied to set parameter "lc_messages"
475 SELECT set_config ('temp_buffers', '8192', false); -- ok
481 ALTER SYSTEM RESET autovacuum_work_mem; -- ok, privileges have been granted
482 ALTER SYSTEM RESET ALL; -- fail, insufficient privileges
483 ERROR: permission denied to perform ALTER SYSTEM RESET ALL
484 ALTER SYSTEM SET none.such2 = 'whiz bang'; -- fail, not superuser
485 ERROR: permission denied to set parameter "none.such2"
486 ALTER ROLE regress_host_resource_admin SET lc_messages = 'POSIX'; -- fail
487 ERROR: permission denied to set parameter "lc_messages"
488 ALTER ROLE regress_host_resource_admin SET max_stack_depth = '1MB'; -- ok
489 SELECT setconfig FROM pg_db_role_setting
490 WHERE setrole = 'regress_host_resource_admin'::regrole;
492 -------------------------------------
493 {lc_messages=C,max_stack_depth=1MB}
496 ALTER ROLE regress_host_resource_admin RESET max_stack_depth; -- ok
497 SELECT setconfig FROM pg_db_role_setting
498 WHERE setrole = 'regress_host_resource_admin'::regrole;
504 ALTER ROLE regress_host_resource_admin SET max_stack_depth = '1MB'; -- ok
505 SELECT setconfig FROM pg_db_role_setting
506 WHERE setrole = 'regress_host_resource_admin'::regrole;
508 -------------------------------------
509 {lc_messages=C,max_stack_depth=1MB}
512 ALTER ROLE regress_host_resource_admin RESET ALL; -- doesn't reset lc_messages
513 SELECT setconfig FROM pg_db_role_setting
514 WHERE setrole = 'regress_host_resource_admin'::regrole;
520 -- Check dropping/revoking behavior
521 SET SESSION AUTHORIZATION regress_admin;
522 DROP ROLE regress_host_resource_admin; -- fail, privileges remain
523 ERROR: role "regress_host_resource_admin" cannot be dropped because some objects depend on it
524 DETAIL: privileges for parameter autovacuum_work_mem
525 privileges for parameter hash_mem_multiplier
526 privileges for parameter max_stack_depth
527 privileges for parameter shared_buffers
528 privileges for parameter temp_file_limit
529 privileges for parameter work_mem
530 -- Use "revoke" to remove the privileges and allow the role to be dropped
531 REVOKE SET, ALTER SYSTEM ON PARAMETER
532 autovacuum_work_mem, hash_mem_multiplier, max_stack_depth,
533 shared_buffers, temp_file_limit, work_mem
534 FROM regress_host_resource_admin;
535 DROP ROLE regress_host_resource_admin; -- ok
536 -- Try that again, but use "drop owned by" instead of "revoke"
537 CREATE ROLE regress_host_resource_admin NOSUPERUSER;
538 SET SESSION AUTHORIZATION regress_host_resource_admin;
539 ALTER SYSTEM SET autovacuum_work_mem = 32; -- fail, privileges not yet granted
540 ERROR: permission denied to set parameter "autovacuum_work_mem"
541 SET SESSION AUTHORIZATION regress_admin;
542 GRANT SET, ALTER SYSTEM ON PARAMETER
543 autovacuum_work_mem, hash_mem_multiplier, max_stack_depth,
544 shared_buffers, temp_file_limit, work_mem
545 TO regress_host_resource_admin;
546 DROP ROLE regress_host_resource_admin; -- fail, privileges remain
547 ERROR: role "regress_host_resource_admin" cannot be dropped because some objects depend on it
548 DETAIL: privileges for parameter autovacuum_work_mem
549 privileges for parameter hash_mem_multiplier
550 privileges for parameter max_stack_depth
551 privileges for parameter shared_buffers
552 privileges for parameter temp_file_limit
553 privileges for parameter work_mem
554 DROP OWNED BY regress_host_resource_admin RESTRICT; -- cascade should not be needed
555 SET SESSION AUTHORIZATION regress_host_resource_admin;
556 ALTER SYSTEM SET autovacuum_work_mem = 32; -- fail, "drop owned" has dropped privileges
557 ERROR: permission denied to set parameter "autovacuum_work_mem"
558 SET SESSION AUTHORIZATION regress_admin;
559 DROP ROLE regress_host_resource_admin; -- ok
560 -- Check that "reassign owned" doesn't affect privileges
561 CREATE ROLE regress_host_resource_admin NOSUPERUSER;
562 CREATE ROLE regress_host_resource_newadmin NOSUPERUSER;
563 GRANT SET, ALTER SYSTEM ON PARAMETER
564 autovacuum_work_mem, hash_mem_multiplier, max_stack_depth,
565 shared_buffers, temp_file_limit, work_mem
566 TO regress_host_resource_admin;
567 REASSIGN OWNED BY regress_host_resource_admin TO regress_host_resource_newadmin;
568 SET SESSION AUTHORIZATION regress_host_resource_admin;
569 ALTER SYSTEM SET autovacuum_work_mem = 32; -- ok, "reassign owned" did not change privileges
570 ALTER SYSTEM RESET autovacuum_work_mem; -- ok
571 SET SESSION AUTHORIZATION regress_admin;
572 DROP ROLE regress_host_resource_admin; -- fail, privileges remain
573 ERROR: role "regress_host_resource_admin" cannot be dropped because some objects depend on it
574 DETAIL: privileges for parameter autovacuum_work_mem
575 privileges for parameter hash_mem_multiplier
576 privileges for parameter max_stack_depth
577 privileges for parameter shared_buffers
578 privileges for parameter temp_file_limit
579 privileges for parameter work_mem
580 DROP ROLE regress_host_resource_newadmin; -- ok, nothing was transferred
581 -- Use "drop owned by" so we can drop the role
582 DROP OWNED BY regress_host_resource_admin; -- ok
583 DROP ROLE regress_host_resource_admin; -- ok
585 RESET SESSION AUTHORIZATION;
586 DROP ROLE regress_admin; -- ok