2 -- Test access privileges
4 -- Clean up in case a prior regression run failed
5 -- Suppress NOTICE messages when users/groups don't exist
6 SET client_min_messages TO 'warning';
7 DROP ROLE IF EXISTS regress_priv_group1;
8 DROP ROLE IF EXISTS regress_priv_group2;
9 DROP ROLE IF EXISTS regress_priv_user1;
10 DROP ROLE IF EXISTS regress_priv_user2;
11 DROP ROLE IF EXISTS regress_priv_user3;
12 DROP ROLE IF EXISTS regress_priv_user4;
13 DROP ROLE IF EXISTS regress_priv_user5;
14 DROP ROLE IF EXISTS regress_priv_user6;
15 DROP ROLE IF EXISTS regress_priv_user7;
16 SELECT lo_unlink(oid) FROM pg_largeobject_metadata WHERE oid >= 1000 AND oid < 3000 ORDER BY oid;
21 RESET client_min_messages;
22 -- test proper begins here
23 CREATE USER regress_priv_user1;
24 CREATE USER regress_priv_user2;
25 CREATE USER regress_priv_user3;
26 CREATE USER regress_priv_user4;
27 CREATE USER regress_priv_user5;
28 CREATE USER regress_priv_user5; -- duplicate
29 ERROR: role "regress_priv_user5" already exists
30 CREATE USER regress_priv_user6;
31 CREATE USER regress_priv_user7;
32 CREATE USER regress_priv_user8;
33 CREATE USER regress_priv_user9;
34 CREATE USER regress_priv_user10;
35 CREATE ROLE regress_priv_role;
36 -- circular ADMIN OPTION grants should be disallowed
37 GRANT regress_priv_user1 TO regress_priv_user2 WITH ADMIN OPTION;
38 GRANT regress_priv_user1 TO regress_priv_user3 WITH ADMIN OPTION GRANTED BY regress_priv_user2;
39 GRANT regress_priv_user1 TO regress_priv_user2 WITH ADMIN OPTION GRANTED BY regress_priv_user3;
40 ERROR: ADMIN option cannot be granted back to your own grantor
41 -- need CASCADE to revoke grant or admin option if dependent grants exist
42 REVOKE ADMIN OPTION FOR regress_priv_user1 FROM regress_priv_user2; -- fail
43 ERROR: dependent privileges exist
44 HINT: Use CASCADE to revoke them too.
45 REVOKE regress_priv_user1 FROM regress_priv_user2; -- fail
46 ERROR: dependent privileges exist
47 HINT: Use CASCADE to revoke them too.
48 SELECT member::regrole, admin_option FROM pg_auth_members WHERE roleid = 'regress_priv_user1'::regrole;
50 --------------------+--------------
51 regress_priv_user2 | t
52 regress_priv_user3 | t
56 REVOKE ADMIN OPTION FOR regress_priv_user1 FROM regress_priv_user2 CASCADE;
57 SELECT member::regrole, admin_option FROM pg_auth_members WHERE roleid = 'regress_priv_user1'::regrole;
59 --------------------+--------------
60 regress_priv_user2 | f
64 REVOKE regress_priv_user1 FROM regress_priv_user2 CASCADE;
65 SELECT member::regrole, admin_option FROM pg_auth_members WHERE roleid = 'regress_priv_user1'::regrole;
67 --------+--------------
70 -- inferred grantor must be a role with ADMIN OPTION
71 GRANT regress_priv_user1 TO regress_priv_user2 WITH ADMIN OPTION;
72 GRANT regress_priv_user2 TO regress_priv_user3;
73 SET ROLE regress_priv_user3;
74 GRANT regress_priv_user1 TO regress_priv_user4;
75 SELECT grantor::regrole FROM pg_auth_members WHERE roleid = 'regress_priv_user1'::regrole and member = 'regress_priv_user4'::regrole;
82 REVOKE regress_priv_user2 FROM regress_priv_user3;
83 REVOKE regress_priv_user1 FROM regress_priv_user2 CASCADE;
84 -- test GRANTED BY with DROP OWNED and REASSIGN OWNED
85 GRANT regress_priv_user1 TO regress_priv_user2 WITH ADMIN OPTION;
86 GRANT regress_priv_user1 TO regress_priv_user3 GRANTED BY regress_priv_user2;
87 DROP ROLE regress_priv_user2; -- fail, dependency
88 ERROR: role "regress_priv_user2" cannot be dropped because some objects depend on it
89 DETAIL: privileges for membership of role regress_priv_user3 in role regress_priv_user1
90 REASSIGN OWNED BY regress_priv_user2 TO regress_priv_user4;
91 DROP ROLE regress_priv_user2; -- still fail, REASSIGN OWNED doesn't help
92 ERROR: role "regress_priv_user2" cannot be dropped because some objects depend on it
93 DETAIL: privileges for membership of role regress_priv_user3 in role regress_priv_user1
94 DROP OWNED BY regress_priv_user2;
95 DROP ROLE regress_priv_user2; -- ok now, DROP OWNED does the job
96 -- test that removing granted role or grantee role removes dependency
97 GRANT regress_priv_user1 TO regress_priv_user3 WITH ADMIN OPTION;
98 GRANT regress_priv_user1 TO regress_priv_user4 GRANTED BY regress_priv_user3;
99 DROP ROLE regress_priv_user3; -- should fail, dependency
100 ERROR: role "regress_priv_user3" cannot be dropped because some objects depend on it
101 DETAIL: privileges for membership of role regress_priv_user4 in role regress_priv_user1
102 DROP ROLE regress_priv_user4; -- ok
103 DROP ROLE regress_priv_user3; -- ok now
104 GRANT regress_priv_user1 TO regress_priv_user5 WITH ADMIN OPTION;
105 GRANT regress_priv_user1 TO regress_priv_user6 GRANTED BY regress_priv_user5;
106 DROP ROLE regress_priv_user5; -- should fail, dependency
107 ERROR: role "regress_priv_user5" cannot be dropped because some objects depend on it
108 DETAIL: privileges for membership of role regress_priv_user6 in role regress_priv_user1
109 DROP ROLE regress_priv_user1, regress_priv_user5; -- ok, despite order
110 -- recreate the roles we just dropped
111 CREATE USER regress_priv_user1;
112 CREATE USER regress_priv_user2;
113 CREATE USER regress_priv_user3;
114 CREATE USER regress_priv_user4;
115 CREATE USER regress_priv_user5;
116 GRANT pg_read_all_data TO regress_priv_user6;
117 GRANT pg_write_all_data TO regress_priv_user7;
118 GRANT pg_read_all_settings TO regress_priv_user8 WITH ADMIN OPTION;
119 GRANT regress_priv_user9 TO regress_priv_user8;
120 SET SESSION AUTHORIZATION regress_priv_user8;
121 GRANT pg_read_all_settings TO regress_priv_user9 WITH ADMIN OPTION;
122 SET SESSION AUTHORIZATION regress_priv_user9;
123 GRANT pg_read_all_settings TO regress_priv_user10;
124 SET SESSION AUTHORIZATION regress_priv_user8;
125 REVOKE pg_read_all_settings FROM regress_priv_user10 GRANTED BY regress_priv_user9;
126 REVOKE ADMIN OPTION FOR pg_read_all_settings FROM regress_priv_user9;
127 REVOKE pg_read_all_settings FROM regress_priv_user9;
128 RESET SESSION AUTHORIZATION;
129 REVOKE regress_priv_user9 FROM regress_priv_user8;
130 REVOKE ADMIN OPTION FOR pg_read_all_settings FROM regress_priv_user8;
131 SET SESSION AUTHORIZATION regress_priv_user8;
132 SET ROLE pg_read_all_settings;
134 RESET SESSION AUTHORIZATION;
135 REVOKE SET OPTION FOR pg_read_all_settings FROM regress_priv_user8;
136 GRANT pg_read_all_stats TO regress_priv_user8 WITH SET FALSE;
137 SET SESSION AUTHORIZATION regress_priv_user8;
138 SET ROLE pg_read_all_settings; -- fail, no SET option any more
139 ERROR: permission denied to set role "pg_read_all_settings"
140 SET ROLE pg_read_all_stats; -- fail, granted without SET option
141 ERROR: permission denied to set role "pg_read_all_stats"
143 RESET SESSION AUTHORIZATION;
144 -- test interaction of SET SESSION AUTHORIZATION and SET ROLE,
145 -- as well as propagation of these settings to parallel workers
146 GRANT regress_priv_user9 TO regress_priv_user8;
147 SET SESSION AUTHORIZATION regress_priv_user8;
148 SET ROLE regress_priv_user9;
149 SET debug_parallel_query = 0;
150 SELECT session_user, current_role, current_user, current_setting('role') as role;
151 session_user | current_role | current_user | role
152 --------------------+--------------------+--------------------+--------------------
153 regress_priv_user8 | regress_priv_user9 | regress_priv_user9 | regress_priv_user9
156 SET debug_parallel_query = 1;
157 SELECT session_user, current_role, current_user, current_setting('role') as role;
158 session_user | current_role | current_user | role
159 --------------------+--------------------+--------------------+--------------------
160 regress_priv_user8 | regress_priv_user9 | regress_priv_user9 | regress_priv_user9
164 SET SESSION AUTHORIZATION regress_priv_user10;
165 SET debug_parallel_query = 0;
166 SELECT session_user, current_role, current_user, current_setting('role') as role;
167 session_user | current_role | current_user | role
168 ---------------------+---------------------+---------------------+------
169 regress_priv_user10 | regress_priv_user10 | regress_priv_user10 | none
172 SET debug_parallel_query = 1;
173 SELECT session_user, current_role, current_user, current_setting('role') as role;
174 session_user | current_role | current_user | role
175 ---------------------+---------------------+---------------------+------
176 regress_priv_user10 | regress_priv_user10 | regress_priv_user10 | none
180 SET debug_parallel_query = 0;
181 SELECT session_user, current_role, current_user, current_setting('role') as role;
182 session_user | current_role | current_user | role
183 --------------------+--------------------+--------------------+--------------------
184 regress_priv_user8 | regress_priv_user9 | regress_priv_user9 | regress_priv_user9
187 SET debug_parallel_query = 1;
188 SELECT session_user, current_role, current_user, current_setting('role') as role;
189 session_user | current_role | current_user | role
190 --------------------+--------------------+--------------------+--------------------
191 regress_priv_user8 | regress_priv_user9 | regress_priv_user9 | regress_priv_user9
194 RESET SESSION AUTHORIZATION;
195 -- session_user at this point is installation-dependent
196 SET debug_parallel_query = 0;
197 SELECT session_user = current_role as c_r_ok, session_user = current_user as c_u_ok, current_setting('role') as role;
198 c_r_ok | c_u_ok | role
199 --------+--------+------
203 SET debug_parallel_query = 1;
204 SELECT session_user = current_role as c_r_ok, session_user = current_user as c_u_ok, current_setting('role') as role;
205 c_r_ok | c_u_ok | role
206 --------+--------+------
210 RESET debug_parallel_query;
211 REVOKE pg_read_all_settings FROM regress_priv_user8;
212 DROP USER regress_priv_user10;
213 DROP USER regress_priv_user9;
214 DROP USER regress_priv_user8;
215 CREATE GROUP regress_priv_group1;
216 CREATE GROUP regress_priv_group2 WITH ADMIN regress_priv_user1 USER regress_priv_user2;
217 ALTER GROUP regress_priv_group1 ADD USER regress_priv_user4;
218 GRANT regress_priv_group2 TO regress_priv_user2 GRANTED BY regress_priv_user1;
219 SET SESSION AUTHORIZATION regress_priv_user1;
220 ALTER GROUP regress_priv_group2 ADD USER regress_priv_user2;
221 NOTICE: role "regress_priv_user2" has already been granted membership in role "regress_priv_group2" by role "regress_priv_user1"
222 ALTER GROUP regress_priv_group2 ADD USER regress_priv_user2; -- duplicate
223 NOTICE: role "regress_priv_user2" has already been granted membership in role "regress_priv_group2" by role "regress_priv_user1"
224 ALTER GROUP regress_priv_group2 DROP USER regress_priv_user2;
225 ALTER USER regress_priv_user2 PASSWORD 'verysecret'; -- not permitted
226 ERROR: permission denied to alter role
227 DETAIL: To change another role's password, the current user must have the CREATEROLE attribute and the ADMIN option on the role.
228 RESET SESSION AUTHORIZATION;
229 ALTER GROUP regress_priv_group2 DROP USER regress_priv_user2;
230 REVOKE ADMIN OPTION FOR regress_priv_group2 FROM regress_priv_user1;
231 GRANT regress_priv_group2 TO regress_priv_user4 WITH ADMIN OPTION;
232 -- prepare non-leakproof function for later
233 CREATE FUNCTION leak(integer,integer) RETURNS boolean
235 LANGUAGE internal IMMUTABLE STRICT; -- but deliberately not LEAKPROOF
236 ALTER FUNCTION leak(integer,integer) OWNER TO regress_priv_user1;
237 -- test owner privileges
238 GRANT regress_priv_role TO regress_priv_user1 WITH ADMIN OPTION GRANTED BY regress_priv_role; -- error, doesn't have ADMIN OPTION
239 ERROR: permission denied to grant privileges as role "regress_priv_role"
240 DETAIL: The grantor must have the ADMIN option on role "regress_priv_role".
241 GRANT regress_priv_role TO regress_priv_user1 WITH ADMIN OPTION GRANTED BY CURRENT_ROLE;
242 REVOKE ADMIN OPTION FOR regress_priv_role FROM regress_priv_user1 GRANTED BY foo; -- error
243 ERROR: role "foo" does not exist
244 REVOKE ADMIN OPTION FOR regress_priv_role FROM regress_priv_user1 GRANTED BY regress_priv_user2; -- warning, noop
245 WARNING: role "regress_priv_user1" has not been granted membership in role "regress_priv_role" by role "regress_priv_user2"
246 REVOKE ADMIN OPTION FOR regress_priv_role FROM regress_priv_user1 GRANTED BY CURRENT_USER;
247 REVOKE regress_priv_role FROM regress_priv_user1 GRANTED BY CURRENT_ROLE;
248 DROP ROLE regress_priv_role;
249 SET SESSION AUTHORIZATION regress_priv_user1;
250 SELECT session_user, current_user;
251 session_user | current_user
252 --------------------+--------------------
253 regress_priv_user1 | regress_priv_user1
256 CREATE TABLE atest1 ( a int, b text );
257 SELECT * FROM atest1;
262 INSERT INTO atest1 VALUES (1, 'one');
264 UPDATE atest1 SET a = 1 WHERE b = 'blech';
267 LOCK atest1 IN ACCESS EXCLUSIVE MODE;
269 REVOKE ALL ON atest1 FROM PUBLIC;
270 SELECT * FROM atest1;
275 GRANT ALL ON atest1 TO regress_priv_user2;
276 GRANT SELECT ON atest1 TO regress_priv_user3, regress_priv_user4;
277 SELECT * FROM atest1;
282 CREATE TABLE atest2 (col1 varchar(10), col2 boolean);
283 SELECT pg_get_acl('pg_class'::regclass, 'atest2'::regclass::oid, 0);
289 GRANT SELECT ON atest2 TO regress_priv_user2;
290 GRANT UPDATE ON atest2 TO regress_priv_user3;
291 GRANT INSERT ON atest2 TO regress_priv_user4 GRANTED BY CURRENT_USER;
292 GRANT TRUNCATE ON atest2 TO regress_priv_user5 GRANTED BY CURRENT_ROLE;
293 SELECT unnest(pg_get_acl('pg_class'::regclass, 'atest2'::regclass::oid, 0));
295 ------------------------------------------------
296 regress_priv_user1=arwdDxtm/regress_priv_user1
297 regress_priv_user2=r/regress_priv_user1
298 regress_priv_user3=w/regress_priv_user1
299 regress_priv_user4=a/regress_priv_user1
300 regress_priv_user5=D/regress_priv_user1
304 SELECT pg_get_acl('pg_class'::regclass, 0, 0); -- null
310 SELECT pg_get_acl(0, 0, 0); -- null
316 GRANT TRUNCATE ON atest2 TO regress_priv_user4 GRANTED BY regress_priv_user5; -- error
317 ERROR: grantor must be current user
318 SET SESSION AUTHORIZATION regress_priv_user2;
319 SELECT session_user, current_user;
320 session_user | current_user
321 --------------------+--------------------
322 regress_priv_user2 | regress_priv_user2
325 -- try various combinations of queries on atest1 and atest2
326 SELECT * FROM atest1; -- ok
331 SELECT * FROM atest2; -- ok
336 INSERT INTO atest1 VALUES (2, 'two'); -- ok
337 INSERT INTO atest2 VALUES ('foo', true); -- fail
338 ERROR: permission denied for table atest2
339 INSERT INTO atest1 SELECT 1, b FROM atest1; -- ok
340 UPDATE atest1 SET a = 1 WHERE a = 2; -- ok
341 UPDATE atest2 SET col2 = NOT col2; -- fail
342 ERROR: permission denied for table atest2
343 SELECT * FROM atest1 FOR UPDATE; -- ok
350 SELECT * FROM atest2 FOR UPDATE; -- fail
351 ERROR: permission denied for table atest2
352 DELETE FROM atest2; -- fail
353 ERROR: permission denied for table atest2
354 TRUNCATE atest2; -- fail
355 ERROR: permission denied for table atest2
357 LOCK atest2 IN ACCESS EXCLUSIVE MODE; -- fail
358 ERROR: permission denied for table atest2
360 COPY atest2 FROM stdin; -- fail
361 ERROR: permission denied for table atest2
362 GRANT ALL ON atest1 TO PUBLIC; -- fail
363 WARNING: no privileges were granted for "atest1"
364 -- checks in subquery, both ok
365 SELECT * FROM atest1 WHERE ( b IN ( SELECT col1 FROM atest2 ) );
370 SELECT * FROM atest2 WHERE ( col1 IN ( SELECT b FROM atest1 ) );
375 SET SESSION AUTHORIZATION regress_priv_user6;
376 SELECT * FROM atest1; -- ok
383 SELECT * FROM atest2; -- ok
388 INSERT INTO atest2 VALUES ('foo', true); -- fail
389 ERROR: permission denied for table atest2
390 SET SESSION AUTHORIZATION regress_priv_user7;
391 SELECT * FROM atest1; -- fail
392 ERROR: permission denied for table atest1
393 SELECT * FROM atest2; -- fail
394 ERROR: permission denied for table atest2
395 INSERT INTO atest2 VALUES ('foo', true); -- ok
396 UPDATE atest2 SET col2 = true; -- ok
397 DELETE FROM atest2; -- ok
398 -- Make sure we are not able to modify system catalogs
399 UPDATE pg_catalog.pg_class SET relname = '123'; -- fail
400 ERROR: permission denied for table pg_class
401 DELETE FROM pg_catalog.pg_class; -- fail
402 ERROR: permission denied for table pg_class
403 UPDATE pg_toast.pg_toast_1213 SET chunk_id = 1; -- fail
404 ERROR: permission denied for table pg_toast_1213
405 SET SESSION AUTHORIZATION regress_priv_user3;
406 SELECT session_user, current_user;
407 session_user | current_user
408 --------------------+--------------------
409 regress_priv_user3 | regress_priv_user3
412 SELECT * FROM atest1; -- ok
419 SELECT * FROM atest2; -- fail
420 ERROR: permission denied for table atest2
421 INSERT INTO atest1 VALUES (2, 'two'); -- fail
422 ERROR: permission denied for table atest1
423 INSERT INTO atest2 VALUES ('foo', true); -- fail
424 ERROR: permission denied for table atest2
425 INSERT INTO atest1 SELECT 1, b FROM atest1; -- fail
426 ERROR: permission denied for table atest1
427 UPDATE atest1 SET a = 1 WHERE a = 2; -- fail
428 ERROR: permission denied for table atest1
429 UPDATE atest2 SET col2 = NULL; -- ok
430 UPDATE atest2 SET col2 = NOT col2; -- fails; requires SELECT on atest2
431 ERROR: permission denied for table atest2
432 UPDATE atest2 SET col2 = true FROM atest1 WHERE atest1.a = 5; -- ok
433 SELECT * FROM atest1 FOR UPDATE; -- fail
434 ERROR: permission denied for table atest1
435 SELECT * FROM atest2 FOR UPDATE; -- fail
436 ERROR: permission denied for table atest2
437 DELETE FROM atest2; -- fail
438 ERROR: permission denied for table atest2
439 TRUNCATE atest2; -- fail
440 ERROR: permission denied for table atest2
442 LOCK atest2 IN ACCESS EXCLUSIVE MODE; -- ok
444 COPY atest2 FROM stdin; -- fail
445 ERROR: permission denied for table atest2
446 -- checks in subquery, both fail
447 SELECT * FROM atest1 WHERE ( b IN ( SELECT col1 FROM atest2 ) );
448 ERROR: permission denied for table atest2
449 SELECT * FROM atest2 WHERE ( col1 IN ( SELECT b FROM atest1 ) );
450 ERROR: permission denied for table atest2
451 SET SESSION AUTHORIZATION regress_priv_user4;
452 COPY atest2 FROM stdin; -- ok
453 SELECT * FROM atest1; -- ok
460 -- test leaky-function protections in selfuncs
461 -- regress_priv_user1 will own a table and provide views for it.
462 SET SESSION AUTHORIZATION regress_priv_user1;
463 CREATE TABLE atest12 as
464 SELECT x AS a, 10001 - x AS b FROM generate_series(1,10000) x;
465 CREATE INDEX ON atest12 (a);
466 CREATE INDEX ON atest12 (abs(a));
467 -- results below depend on having quite accurate stats for atest12, so...
468 ALTER TABLE atest12 SET (autovacuum_enabled = off);
469 SET default_statistics_target = 10000;
470 VACUUM ANALYZE atest12;
471 RESET default_statistics_target;
472 CREATE OPERATOR <<< (procedure = leak, leftarg = integer, rightarg = integer,
473 restrict = scalarltsel);
474 -- views with leaky operator
475 CREATE VIEW atest12v AS
476 SELECT * FROM atest12 WHERE b <<< 5;
477 CREATE VIEW atest12sbv WITH (security_barrier=true) AS
478 SELECT * FROM atest12 WHERE b <<< 5;
479 GRANT SELECT ON atest12v TO PUBLIC;
480 GRANT SELECT ON atest12sbv TO PUBLIC;
481 -- This plan should use nestloop, knowing that few rows will be selected.
482 EXPLAIN (COSTS OFF) SELECT * FROM atest12v x, atest12v y WHERE x.a = y.b;
484 -------------------------------------------------
486 -> Seq Scan on atest12 atest12_1
488 -> Index Scan using atest12_a_idx on atest12
489 Index Cond: (a = atest12_1.b)
494 EXPLAIN (COSTS OFF) SELECT * FROM atest12 x, atest12 y
495 WHERE x.a = y.b and abs(y.a) <<< 5;
497 ---------------------------------------------------
499 -> Seq Scan on atest12 y
500 Filter: (abs(a) <<< 5)
501 -> Index Scan using atest12_a_idx on atest12 x
502 Index Cond: (a = y.b)
505 -- This should also be a nestloop, but the security barrier forces the inner
506 -- scan to be materialized
507 EXPLAIN (COSTS OFF) SELECT * FROM atest12sbv x, atest12sbv y WHERE x.a = y.b;
509 -------------------------------------------
511 Join Filter: (atest12.a = atest12_1.b)
512 -> Seq Scan on atest12
515 -> Seq Scan on atest12 atest12_1
519 -- Check if regress_priv_user2 can break security.
520 SET SESSION AUTHORIZATION regress_priv_user2;
521 CREATE FUNCTION leak2(integer,integer) RETURNS boolean
522 AS $$begin raise notice 'leak % %', $1, $2; return $1 > $2; end$$
523 LANGUAGE plpgsql immutable;
524 CREATE OPERATOR >>> (procedure = leak2, leftarg = integer, rightarg = integer,
525 restrict = scalargtsel);
526 -- This should not show any "leak" notices before failing.
527 EXPLAIN (COSTS OFF) SELECT * FROM atest12 WHERE a >>> 0;
528 ERROR: permission denied for table atest12
529 -- These plans should continue to use a nestloop, since they execute with the
530 -- privileges of the view owner.
531 EXPLAIN (COSTS OFF) SELECT * FROM atest12v x, atest12v y WHERE x.a = y.b;
533 -------------------------------------------------
535 -> Seq Scan on atest12 atest12_1
537 -> Index Scan using atest12_a_idx on atest12
538 Index Cond: (a = atest12_1.b)
542 EXPLAIN (COSTS OFF) SELECT * FROM atest12sbv x, atest12sbv y WHERE x.a = y.b;
544 -------------------------------------------
546 Join Filter: (atest12.a = atest12_1.b)
547 -> Seq Scan on atest12
550 -> Seq Scan on atest12 atest12_1
554 -- A non-security barrier view does not guard against information leakage.
555 EXPLAIN (COSTS OFF) SELECT * FROM atest12v x, atest12v y
556 WHERE x.a = y.b and abs(y.a) <<< 5;
558 -------------------------------------------------
560 -> Seq Scan on atest12 atest12_1
561 Filter: ((b <<< 5) AND (abs(a) <<< 5))
562 -> Index Scan using atest12_a_idx on atest12
563 Index Cond: (a = atest12_1.b)
567 -- But a security barrier view isolates the leaky operator.
568 EXPLAIN (COSTS OFF) SELECT * FROM atest12sbv x, atest12sbv y
569 WHERE x.a = y.b and abs(y.a) <<< 5;
571 -------------------------------------
573 Join Filter: (atest12_1.a = y.b)
574 -> Subquery Scan on y
575 Filter: (abs(y.a) <<< 5)
576 -> Seq Scan on atest12
578 -> Seq Scan on atest12 atest12_1
582 -- Now regress_priv_user1 grants sufficient access to regress_priv_user2.
583 SET SESSION AUTHORIZATION regress_priv_user1;
584 GRANT SELECT (a, b) ON atest12 TO PUBLIC;
585 SET SESSION AUTHORIZATION regress_priv_user2;
586 -- regress_priv_user2 should continue to get a good row estimate.
587 EXPLAIN (COSTS OFF) SELECT * FROM atest12v x, atest12v y WHERE x.a = y.b;
589 -------------------------------------------------
591 -> Seq Scan on atest12 atest12_1
593 -> Index Scan using atest12_a_idx on atest12
594 Index Cond: (a = atest12_1.b)
598 -- But not for this, due to lack of table-wide permissions needed
599 -- to make use of the expression index's statistics.
600 EXPLAIN (COSTS OFF) SELECT * FROM atest12 x, atest12 y
601 WHERE x.a = y.b and abs(y.a) <<< 5;
603 --------------------------------------
605 Hash Cond: (x.a = y.b)
606 -> Seq Scan on atest12 x
608 -> Seq Scan on atest12 y
609 Filter: (abs(a) <<< 5)
612 -- clean up (regress_priv_user1's objects are all dropped later)
613 DROP FUNCTION leak2(integer, integer) CASCADE;
614 NOTICE: drop cascades to operator >>>(integer,integer)
616 SET SESSION AUTHORIZATION regress_priv_user3;
617 CREATE TABLE atest3 (one int, two int, three int);
618 GRANT DELETE ON atest3 TO GROUP regress_priv_group2;
619 SET SESSION AUTHORIZATION regress_priv_user1;
620 SELECT * FROM atest3; -- fail
621 ERROR: permission denied for table atest3
622 DELETE FROM atest3; -- ok
624 RESET SESSION AUTHORIZATION;
625 ALTER ROLE regress_priv_user1 NOINHERIT;
626 SET SESSION AUTHORIZATION regress_priv_user1;
628 DELETE FROM atest3; -- ok because grant-level option is unchanged
630 RESET SESSION AUTHORIZATION;
631 GRANT regress_priv_group2 TO regress_priv_user1 WITH INHERIT FALSE;
632 SET SESSION AUTHORIZATION regress_priv_user1;
633 DELETE FROM atest3; -- fail
634 ERROR: permission denied for table atest3
636 RESET SESSION AUTHORIZATION;
637 REVOKE INHERIT OPTION FOR regress_priv_group2 FROM regress_priv_user1;
638 SET SESSION AUTHORIZATION regress_priv_user1;
639 DELETE FROM atest3; -- also fail
640 ERROR: permission denied for table atest3
643 SET SESSION AUTHORIZATION regress_priv_user3;
644 CREATE VIEW atestv1 AS SELECT * FROM atest1; -- ok
645 /* The next *should* fail, but it's not implemented that way yet. */
646 CREATE VIEW atestv2 AS SELECT * FROM atest2;
647 CREATE VIEW atestv3 AS SELECT * FROM atest3; -- ok
648 /* Empty view is a corner case that failed in 9.2. */
649 CREATE VIEW atestv0 AS SELECT 0 as x WHERE false; -- ok
650 SELECT * FROM atestv1; -- ok
657 SELECT * FROM atestv2; -- fail
658 ERROR: permission denied for table atest2
659 GRANT SELECT ON atestv1, atestv3 TO regress_priv_user4;
660 GRANT SELECT ON atestv2 TO regress_priv_user2;
661 SET SESSION AUTHORIZATION regress_priv_user4;
662 SELECT * FROM atestv1; -- ok
669 SELECT * FROM atestv2; -- fail
670 ERROR: permission denied for view atestv2
671 SELECT * FROM atestv3; -- ok
676 SELECT * FROM atestv0; -- fail
677 ERROR: permission denied for view atestv0
678 -- Appendrels excluded by constraints failed to check permissions in 8.4-9.2.
680 ((select a.q1 as x from int8_tbl a offset 0)
682 (select b.q2 as x from int8_tbl b offset 0)) ss
684 ERROR: permission denied for table int8_tbl
685 set constraint_exclusion = on;
687 ((select a.q1 as x, random() from int8_tbl a where q1 > 0)
689 (select b.q2 as x, random() from int8_tbl b where q2 > 0)) ss
691 ERROR: permission denied for table int8_tbl
692 reset constraint_exclusion;
693 CREATE VIEW atestv4 AS SELECT * FROM atestv3; -- nested view
694 SELECT * FROM atestv4; -- ok
699 GRANT SELECT ON atestv4 TO regress_priv_user2;
700 SET SESSION AUTHORIZATION regress_priv_user2;
701 -- Two complex cases:
702 SELECT * FROM atestv3; -- fail
703 ERROR: permission denied for view atestv3
704 SELECT * FROM atestv4; -- ok (even though regress_priv_user2 cannot access underlying atestv3)
709 SELECT * FROM atest2; -- ok
715 SELECT * FROM atestv2; -- fail (even though regress_priv_user2 can access underlying atest2)
716 ERROR: permission denied for table atest2
717 -- Test column level permissions
718 SET SESSION AUTHORIZATION regress_priv_user1;
719 CREATE TABLE atest5 (one int, two int unique, three int, four int unique);
720 CREATE TABLE atest6 (one int, two int, blue int);
721 GRANT SELECT (one), INSERT (two), UPDATE (three) ON atest5 TO regress_priv_user4;
722 GRANT ALL (one) ON atest5 TO regress_priv_user3;
723 SELECT unnest(pg_get_acl('pg_class'::regclass, 'atest5'::regclass::oid, 1));
725 --------------------------------------------
726 regress_priv_user4=r/regress_priv_user1
727 regress_priv_user3=arwx/regress_priv_user1
730 SELECT unnest(pg_get_acl('pg_class'::regclass, 'atest5'::regclass::oid, 2));
732 -----------------------------------------
733 regress_priv_user4=a/regress_priv_user1
736 SELECT unnest(pg_get_acl('pg_class'::regclass, 'atest5'::regclass::oid, 3));
738 -----------------------------------------
739 regress_priv_user4=w/regress_priv_user1
742 SELECT unnest(pg_get_acl('pg_class'::regclass, 'atest5'::regclass::oid, 4));
747 INSERT INTO atest5 VALUES (1,2,3);
748 SET SESSION AUTHORIZATION regress_priv_user4;
749 SELECT * FROM atest5; -- fail
750 ERROR: permission denied for table atest5
751 SELECT one FROM atest5; -- ok
757 COPY atest5 (one) TO stdout; -- ok
759 SELECT two FROM atest5; -- fail
760 ERROR: permission denied for table atest5
761 COPY atest5 (two) TO stdout; -- fail
762 ERROR: permission denied for table atest5
763 SELECT atest5 FROM atest5; -- fail
764 ERROR: permission denied for table atest5
765 COPY atest5 (one,two) TO stdout; -- fail
766 ERROR: permission denied for table atest5
767 SELECT 1 FROM atest5; -- ok
773 SELECT 1 FROM atest5 a JOIN atest5 b USING (one); -- ok
779 SELECT 1 FROM atest5 a JOIN atest5 b USING (two); -- fail
780 ERROR: permission denied for table atest5
781 SELECT 1 FROM atest5 a NATURAL JOIN atest5 b; -- fail
782 ERROR: permission denied for table atest5
783 SELECT * FROM (atest5 a JOIN atest5 b USING (one)) j; -- fail
784 ERROR: permission denied for table atest5
785 SELECT j.* FROM (atest5 a JOIN atest5 b USING (one)) j; -- fail
786 ERROR: permission denied for table atest5
787 SELECT (j.*) IS NULL FROM (atest5 a JOIN atest5 b USING (one)) j; -- fail
788 ERROR: permission denied for table atest5
789 SELECT one FROM (atest5 a JOIN atest5 b(one,x,y,z) USING (one)) j; -- ok
795 SELECT j.one FROM (atest5 a JOIN atest5 b(one,x,y,z) USING (one)) j; -- ok
801 SELECT two FROM (atest5 a JOIN atest5 b(one,x,y,z) USING (one)) j; -- fail
802 ERROR: permission denied for table atest5
803 SELECT j.two FROM (atest5 a JOIN atest5 b(one,x,y,z) USING (one)) j; -- fail
804 ERROR: permission denied for table atest5
805 SELECT y FROM (atest5 a JOIN atest5 b(one,x,y,z) USING (one)) j; -- fail
806 ERROR: permission denied for table atest5
807 SELECT j.y FROM (atest5 a JOIN atest5 b(one,x,y,z) USING (one)) j; -- fail
808 ERROR: permission denied for table atest5
809 SELECT * FROM (atest5 a JOIN atest5 b USING (one)); -- fail
810 ERROR: permission denied for table atest5
811 SELECT a.* FROM (atest5 a JOIN atest5 b USING (one)); -- fail
812 ERROR: permission denied for table atest5
813 SELECT (a.*) IS NULL FROM (atest5 a JOIN atest5 b USING (one)); -- fail
814 ERROR: permission denied for table atest5
815 SELECT two FROM (atest5 a JOIN atest5 b(one,x,y,z) USING (one)); -- fail
816 ERROR: permission denied for table atest5
817 SELECT a.two FROM (atest5 a JOIN atest5 b(one,x,y,z) USING (one)); -- fail
818 ERROR: permission denied for table atest5
819 SELECT y FROM (atest5 a JOIN atest5 b(one,x,y,z) USING (one)); -- fail
820 ERROR: permission denied for table atest5
821 SELECT b.y FROM (atest5 a JOIN atest5 b(one,x,y,z) USING (one)); -- fail
822 ERROR: permission denied for table atest5
823 SELECT y FROM (atest5 a LEFT JOIN atest5 b(one,x,y,z) USING (one)); -- fail
824 ERROR: permission denied for table atest5
825 SELECT b.y FROM (atest5 a LEFT JOIN atest5 b(one,x,y,z) USING (one)); -- fail
826 ERROR: permission denied for table atest5
827 SELECT y FROM (atest5 a FULL JOIN atest5 b(one,x,y,z) USING (one)); -- fail
828 ERROR: permission denied for table atest5
829 SELECT b.y FROM (atest5 a FULL JOIN atest5 b(one,x,y,z) USING (one)); -- fail
830 ERROR: permission denied for table atest5
831 SELECT 1 FROM atest5 WHERE two = 2; -- fail
832 ERROR: permission denied for table atest5
833 SELECT * FROM atest1, atest5; -- fail
834 ERROR: permission denied for table atest5
835 SELECT atest1.* FROM atest1, atest5; -- ok
842 SELECT atest1.*,atest5.one FROM atest1, atest5; -- ok
849 SELECT atest1.*,atest5.one FROM atest1 JOIN atest5 ON (atest1.a = atest5.two); -- fail
850 ERROR: permission denied for table atest5
851 SELECT atest1.*,atest5.one FROM atest1 JOIN atest5 ON (atest1.a = atest5.one); -- ok
858 SELECT one, two FROM atest5; -- fail
859 ERROR: permission denied for table atest5
860 SET SESSION AUTHORIZATION regress_priv_user1;
861 GRANT SELECT (one,two) ON atest6 TO regress_priv_user4;
862 SET SESSION AUTHORIZATION regress_priv_user4;
863 SELECT one, two FROM atest5 NATURAL JOIN atest6; -- fail still
864 ERROR: permission denied for table atest5
865 SET SESSION AUTHORIZATION regress_priv_user1;
866 GRANT SELECT (two) ON atest5 TO regress_priv_user4;
867 SET SESSION AUTHORIZATION regress_priv_user4;
868 SELECT one, two FROM atest5 NATURAL JOIN atest6; -- ok now
873 -- test column-level privileges for INSERT and UPDATE
874 INSERT INTO atest5 (two) VALUES (3); -- ok
875 COPY atest5 FROM stdin; -- fail
876 ERROR: permission denied for table atest5
877 COPY atest5 (two) FROM stdin; -- ok
878 INSERT INTO atest5 (three) VALUES (4); -- fail
879 ERROR: permission denied for table atest5
880 INSERT INTO atest5 VALUES (5,5,5); -- fail
881 ERROR: permission denied for table atest5
882 UPDATE atest5 SET three = 10; -- ok
883 UPDATE atest5 SET one = 8; -- fail
884 ERROR: permission denied for table atest5
885 UPDATE atest5 SET three = 5, one = 2; -- fail
886 ERROR: permission denied for table atest5
887 -- Check that column level privs are enforced in RETURNING
889 INSERT INTO atest5(two) VALUES (6) ON CONFLICT (two) DO UPDATE set three = 10;
890 -- Error. No SELECT on column three.
891 INSERT INTO atest5(two) VALUES (6) ON CONFLICT (two) DO UPDATE set three = 10 RETURNING atest5.three;
892 ERROR: permission denied for table atest5
893 -- Ok. May SELECT on column "one":
894 INSERT INTO atest5(two) VALUES (6) ON CONFLICT (two) DO UPDATE set three = 10 RETURNING atest5.one;
900 -- Check that column level privileges are enforced for EXCLUDED
901 -- Ok. we may select one
902 INSERT INTO atest5(two) VALUES (6) ON CONFLICT (two) DO UPDATE set three = EXCLUDED.one;
903 -- Error. No select rights on three
904 INSERT INTO atest5(two) VALUES (6) ON CONFLICT (two) DO UPDATE set three = EXCLUDED.three;
905 ERROR: permission denied for table atest5
906 INSERT INTO atest5(two) VALUES (6) ON CONFLICT (two) DO UPDATE set one = 8; -- fails (due to UPDATE)
907 ERROR: permission denied for table atest5
908 INSERT INTO atest5(three) VALUES (4) ON CONFLICT (two) DO UPDATE set three = 10; -- fails (due to INSERT)
909 ERROR: permission denied for table atest5
910 -- Check that the columns in the inference require select privileges
911 INSERT INTO atest5(four) VALUES (4); -- fail
912 ERROR: permission denied for table atest5
913 SET SESSION AUTHORIZATION regress_priv_user1;
914 GRANT INSERT (four) ON atest5 TO regress_priv_user4;
915 SET SESSION AUTHORIZATION regress_priv_user4;
916 INSERT INTO atest5(four) VALUES (4) ON CONFLICT (four) DO UPDATE set three = 3; -- fails (due to SELECT)
917 ERROR: permission denied for table atest5
918 INSERT INTO atest5(four) VALUES (4) ON CONFLICT ON CONSTRAINT atest5_four_key DO UPDATE set three = 3; -- fails (due to SELECT)
919 ERROR: permission denied for table atest5
920 INSERT INTO atest5(four) VALUES (4); -- ok
921 SET SESSION AUTHORIZATION regress_priv_user1;
922 GRANT SELECT (four) ON atest5 TO regress_priv_user4;
923 SET SESSION AUTHORIZATION regress_priv_user4;
924 INSERT INTO atest5(four) VALUES (4) ON CONFLICT (four) DO UPDATE set three = 3; -- ok
925 INSERT INTO atest5(four) VALUES (4) ON CONFLICT ON CONSTRAINT atest5_four_key DO UPDATE set three = 3; -- ok
926 SET SESSION AUTHORIZATION regress_priv_user1;
927 REVOKE ALL (one) ON atest5 FROM regress_priv_user4;
928 GRANT SELECT (one,two,blue) ON atest6 TO regress_priv_user4;
929 SET SESSION AUTHORIZATION regress_priv_user4;
930 SELECT one FROM atest5; -- fail
931 ERROR: permission denied for table atest5
932 UPDATE atest5 SET one = 1; -- fail
933 ERROR: permission denied for table atest5
934 SELECT atest6 FROM atest6; -- ok
939 COPY atest6 TO stdout; -- ok
940 -- test column privileges with MERGE
941 SET SESSION AUTHORIZATION regress_priv_user1;
942 CREATE TABLE mtarget (a int, b text);
943 CREATE TABLE msource (a int, b text);
944 INSERT INTO mtarget VALUES (1, 'init1'), (2, 'init2');
945 INSERT INTO msource VALUES (1, 'source1'), (2, 'source2'), (3, 'source3');
946 GRANT SELECT (a) ON msource TO regress_priv_user4;
947 GRANT SELECT (a) ON mtarget TO regress_priv_user4;
948 GRANT INSERT (a,b) ON mtarget TO regress_priv_user4;
949 GRANT UPDATE (b) ON mtarget TO regress_priv_user4;
950 SET SESSION AUTHORIZATION regress_priv_user4;
952 -- test source privileges
954 -- fail (no SELECT priv on s.b)
955 MERGE INTO mtarget t USING msource s ON t.a = s.a
958 WHEN NOT MATCHED THEN
959 INSERT VALUES (a, NULL);
960 ERROR: permission denied for table msource
961 -- fail (s.b used in the INSERTed values)
962 MERGE INTO mtarget t USING msource s ON t.a = s.a
965 WHEN NOT MATCHED THEN
966 INSERT VALUES (a, b);
967 ERROR: permission denied for table msource
968 -- fail (s.b used in the WHEN quals)
969 MERGE INTO mtarget t USING msource s ON t.a = s.a
970 WHEN MATCHED AND s.b = 'x' THEN
972 WHEN NOT MATCHED THEN
973 INSERT VALUES (a, NULL);
974 ERROR: permission denied for table msource
975 -- this should be ok since only s.a is accessed
977 MERGE INTO mtarget t USING msource s ON t.a = s.a
980 WHEN NOT MATCHED THEN
981 INSERT VALUES (a, NULL);
983 SET SESSION AUTHORIZATION regress_priv_user1;
984 GRANT SELECT (b) ON msource TO regress_priv_user4;
985 SET SESSION AUTHORIZATION regress_priv_user4;
988 MERGE INTO mtarget t USING msource s ON t.a = s.a
991 WHEN NOT MATCHED THEN
992 INSERT VALUES (a, b);
995 -- test target privileges
997 -- fail (no SELECT priv on t.b)
998 MERGE INTO mtarget t USING msource s ON t.a = s.a
1001 WHEN NOT MATCHED THEN
1002 INSERT VALUES (a, NULL);
1003 ERROR: permission denied for table mtarget
1004 -- fail (no UPDATE on t.a)
1005 MERGE INTO mtarget t USING msource s ON t.a = s.a
1007 UPDATE SET b = s.b, a = t.a + 1
1008 WHEN NOT MATCHED THEN
1009 INSERT VALUES (a, b);
1010 ERROR: permission denied for table mtarget
1011 -- fail (no SELECT on t.b)
1012 MERGE INTO mtarget t USING msource s ON t.a = s.a
1013 WHEN MATCHED AND t.b IS NOT NULL THEN
1015 WHEN NOT MATCHED THEN
1016 INSERT VALUES (a, b);
1017 ERROR: permission denied for table mtarget
1020 MERGE INTO mtarget t USING msource s ON t.a = s.a
1025 MERGE INTO mtarget t USING msource s ON t.a = s.a
1026 WHEN MATCHED AND t.b IS NOT NULL THEN
1028 ERROR: permission denied for table mtarget
1029 -- grant delete privileges
1030 SET SESSION AUTHORIZATION regress_priv_user1;
1031 GRANT DELETE ON mtarget TO regress_priv_user4;
1034 MERGE INTO mtarget t USING msource s ON t.a = s.a
1035 WHEN MATCHED AND t.b IS NOT NULL THEN
1038 -- check error reporting with column privs
1039 SET SESSION AUTHORIZATION regress_priv_user1;
1040 CREATE TABLE t1 (c1 int, c2 int, c3 int check (c3 < 5), primary key (c1, c2));
1041 GRANT SELECT (c1) ON t1 TO regress_priv_user2;
1042 GRANT INSERT (c1, c2, c3) ON t1 TO regress_priv_user2;
1043 GRANT UPDATE (c1, c2, c3) ON t1 TO regress_priv_user2;
1045 INSERT INTO t1 VALUES (1, 1, 1);
1046 INSERT INTO t1 VALUES (1, 2, 1);
1047 INSERT INTO t1 VALUES (2, 1, 2);
1048 INSERT INTO t1 VALUES (2, 2, 2);
1049 INSERT INTO t1 VALUES (3, 1, 3);
1050 SET SESSION AUTHORIZATION regress_priv_user2;
1051 INSERT INTO t1 (c1, c2) VALUES (1, 1); -- fail, but row not shown
1052 ERROR: duplicate key value violates unique constraint "t1_pkey"
1053 UPDATE t1 SET c2 = 1; -- fail, but row not shown
1054 ERROR: duplicate key value violates unique constraint "t1_pkey"
1055 INSERT INTO t1 (c1, c2) VALUES (null, null); -- fail, but see columns being inserted
1056 ERROR: null value in column "c1" of relation "t1" violates not-null constraint
1057 DETAIL: Failing row contains (c1, c2) = (null, null).
1058 INSERT INTO t1 (c3) VALUES (null); -- fail, but see columns being inserted or have SELECT
1059 ERROR: null value in column "c1" of relation "t1" violates not-null constraint
1060 DETAIL: Failing row contains (c1, c3) = (null, null).
1061 INSERT INTO t1 (c1) VALUES (5); -- fail, but see columns being inserted or have SELECT
1062 ERROR: null value in column "c2" of relation "t1" violates not-null constraint
1063 DETAIL: Failing row contains (c1) = (5).
1064 UPDATE t1 SET c3 = 10; -- fail, but see columns with SELECT rights, or being modified
1065 ERROR: new row for relation "t1" violates check constraint "t1_c3_check"
1066 DETAIL: Failing row contains (c1, c3) = (1, 10).
1067 SET SESSION AUTHORIZATION regress_priv_user1;
1069 -- check error reporting with column privs on a partitioned table
1070 CREATE TABLE errtst(a text, b text NOT NULL, c text, secret1 text, secret2 text) PARTITION BY LIST (a);
1071 CREATE TABLE errtst_part_1(secret2 text, c text, a text, b text NOT NULL, secret1 text);
1072 CREATE TABLE errtst_part_2(secret1 text, secret2 text, a text, c text, b text NOT NULL);
1073 ALTER TABLE errtst ATTACH PARTITION errtst_part_1 FOR VALUES IN ('aaa');
1074 ALTER TABLE errtst ATTACH PARTITION errtst_part_2 FOR VALUES IN ('aaaa');
1075 GRANT SELECT (a, b, c) ON TABLE errtst TO regress_priv_user2;
1076 GRANT UPDATE (a, b, c) ON TABLE errtst TO regress_priv_user2;
1077 GRANT INSERT (a, b, c) ON TABLE errtst TO regress_priv_user2;
1078 INSERT INTO errtst_part_1 (a, b, c, secret1, secret2)
1079 VALUES ('aaa', 'bbb', 'ccc', 'the body', 'is in the attic');
1080 SET SESSION AUTHORIZATION regress_priv_user2;
1081 -- Perform a few updates that violate the NOT NULL constraint. Make sure
1082 -- the error messages don't leak the secret fields.
1084 INSERT INTO errtst (a, b) VALUES ('aaa', NULL);
1085 ERROR: null value in column "b" of relation "errtst_part_1" violates not-null constraint
1086 DETAIL: Failing row contains (a, b, c) = (aaa, null, null).
1088 UPDATE errtst SET b = NULL;
1089 ERROR: null value in column "b" of relation "errtst_part_1" violates not-null constraint
1090 DETAIL: Failing row contains (a, b, c) = (aaa, null, ccc).
1091 -- partitioning key is updated, doesn't move the row.
1092 UPDATE errtst SET a = 'aaa', b = NULL;
1093 ERROR: null value in column "b" of relation "errtst_part_1" violates not-null constraint
1094 DETAIL: Failing row contains (a, b, c) = (aaa, null, ccc).
1095 -- row is moved to another partition.
1096 UPDATE errtst SET a = 'aaaa', b = NULL;
1097 ERROR: null value in column "b" of relation "errtst_part_2" violates not-null constraint
1098 DETAIL: Failing row contains (a, b, c) = (aaaa, null, ccc).
1099 -- row is moved to another partition. This differs from the previous case in
1100 -- that the new partition is excluded by constraint exclusion, so its
1101 -- ResultRelInfo is not created at ExecInitModifyTable, but needs to be
1102 -- constructed on the fly when the updated tuple is routed to it.
1103 UPDATE errtst SET a = 'aaaa', b = NULL WHERE a = 'aaa';
1104 ERROR: null value in column "b" of relation "errtst_part_2" violates not-null constraint
1105 DETAIL: Failing row contains (a, b, c) = (aaaa, null, ccc).
1106 SET SESSION AUTHORIZATION regress_priv_user1;
1108 -- test column-level privileges when involved with DELETE
1109 SET SESSION AUTHORIZATION regress_priv_user1;
1110 ALTER TABLE atest6 ADD COLUMN three integer;
1111 GRANT DELETE ON atest5 TO regress_priv_user3;
1112 GRANT SELECT (two) ON atest5 TO regress_priv_user3;
1113 REVOKE ALL (one) ON atest5 FROM regress_priv_user3;
1114 GRANT SELECT (one) ON atest5 TO regress_priv_user4;
1115 SET SESSION AUTHORIZATION regress_priv_user4;
1116 SELECT atest6 FROM atest6; -- fail
1117 ERROR: permission denied for table atest6
1118 SELECT one FROM atest5 NATURAL JOIN atest6; -- fail
1119 ERROR: permission denied for table atest5
1120 SET SESSION AUTHORIZATION regress_priv_user1;
1121 ALTER TABLE atest6 DROP COLUMN three;
1122 SET SESSION AUTHORIZATION regress_priv_user4;
1123 SELECT atest6 FROM atest6; -- ok
1128 SELECT one FROM atest5 NATURAL JOIN atest6; -- ok
1133 SET SESSION AUTHORIZATION regress_priv_user1;
1134 ALTER TABLE atest6 DROP COLUMN two;
1135 REVOKE SELECT (one,blue) ON atest6 FROM regress_priv_user4;
1136 SET SESSION AUTHORIZATION regress_priv_user4;
1137 SELECT * FROM atest6; -- fail
1138 ERROR: permission denied for table atest6
1139 SELECT 1 FROM atest6; -- fail
1140 ERROR: permission denied for table atest6
1141 SET SESSION AUTHORIZATION regress_priv_user3;
1142 DELETE FROM atest5 WHERE one = 1; -- fail
1143 ERROR: permission denied for table atest5
1144 DELETE FROM atest5 WHERE two = 2; -- ok
1145 -- check inheritance cases
1146 SET SESSION AUTHORIZATION regress_priv_user1;
1147 CREATE TABLE atestp1 (f1 int, f2 int);
1148 CREATE TABLE atestp2 (fx int, fy int);
1149 CREATE TABLE atestc (fz int) INHERITS (atestp1, atestp2);
1150 GRANT SELECT(fx,fy,tableoid) ON atestp2 TO regress_priv_user2;
1151 GRANT SELECT(fx) ON atestc TO regress_priv_user2;
1152 SET SESSION AUTHORIZATION regress_priv_user2;
1153 SELECT fx FROM atestp2; -- ok
1158 SELECT fy FROM atestp2; -- ok
1163 SELECT atestp2 FROM atestp2; -- ok
1168 SELECT tableoid FROM atestp2; -- ok
1173 SELECT fy FROM atestc; -- fail
1174 ERROR: permission denied for table atestc
1175 SET SESSION AUTHORIZATION regress_priv_user1;
1176 GRANT SELECT(fy,tableoid) ON atestc TO regress_priv_user2;
1177 SET SESSION AUTHORIZATION regress_priv_user2;
1178 SELECT fx FROM atestp2; -- still ok
1183 SELECT fy FROM atestp2; -- ok
1188 SELECT atestp2 FROM atestp2; -- ok
1193 SELECT tableoid FROM atestp2; -- ok
1198 -- child's permissions do not apply when operating on parent
1199 SET SESSION AUTHORIZATION regress_priv_user1;
1200 REVOKE ALL ON atestc FROM regress_priv_user2;
1201 GRANT ALL ON atestp1 TO regress_priv_user2;
1202 SET SESSION AUTHORIZATION regress_priv_user2;
1203 SELECT f2 FROM atestp1; -- ok
1208 SELECT f2 FROM atestc; -- fail
1209 ERROR: permission denied for table atestc
1210 DELETE FROM atestp1; -- ok
1211 DELETE FROM atestc; -- fail
1212 ERROR: permission denied for table atestc
1213 UPDATE atestp1 SET f1 = 1; -- ok
1214 UPDATE atestc SET f1 = 1; -- fail
1215 ERROR: permission denied for table atestc
1216 TRUNCATE atestp1; -- ok
1217 TRUNCATE atestc; -- fail
1218 ERROR: permission denied for table atestc
1224 ERROR: permission denied for table atestc
1226 -- privileges on functions, languages
1227 -- switch to superuser
1229 REVOKE ALL PRIVILEGES ON LANGUAGE sql FROM PUBLIC;
1230 GRANT USAGE ON LANGUAGE sql TO regress_priv_user1; -- ok
1231 GRANT USAGE ON LANGUAGE c TO PUBLIC; -- fail
1232 ERROR: language "c" is not trusted
1233 DETAIL: GRANT and REVOKE are not allowed on untrusted languages, because only superusers can use untrusted languages.
1234 SET SESSION AUTHORIZATION regress_priv_user1;
1235 GRANT USAGE ON LANGUAGE sql TO regress_priv_user2; -- fail
1236 WARNING: no privileges were granted for "sql"
1237 CREATE FUNCTION priv_testfunc1(int) RETURNS int AS 'select 2 * $1;' LANGUAGE sql;
1238 CREATE FUNCTION priv_testfunc2(int) RETURNS int AS 'select 3 * $1;' LANGUAGE sql;
1239 CREATE AGGREGATE priv_testagg1(int) (sfunc = int4pl, stype = int4);
1240 CREATE PROCEDURE priv_testproc1(int) AS 'select $1;' LANGUAGE sql;
1241 REVOKE ALL ON FUNCTION priv_testfunc1(int), priv_testfunc2(int), priv_testagg1(int) FROM PUBLIC;
1242 GRANT EXECUTE ON FUNCTION priv_testfunc1(int), priv_testfunc2(int), priv_testagg1(int) TO regress_priv_user2;
1243 REVOKE ALL ON FUNCTION priv_testproc1(int) FROM PUBLIC; -- fail, not a function
1244 ERROR: priv_testproc1(integer) is not a function
1245 REVOKE ALL ON PROCEDURE priv_testproc1(int) FROM PUBLIC;
1246 GRANT EXECUTE ON PROCEDURE priv_testproc1(int) TO regress_priv_user2;
1247 GRANT USAGE ON FUNCTION priv_testfunc1(int) TO regress_priv_user3; -- semantic error
1248 ERROR: invalid privilege type USAGE for function
1249 GRANT USAGE ON FUNCTION priv_testagg1(int) TO regress_priv_user3; -- semantic error
1250 ERROR: invalid privilege type USAGE for function
1251 GRANT USAGE ON PROCEDURE priv_testproc1(int) TO regress_priv_user3; -- semantic error
1252 ERROR: invalid privilege type USAGE for procedure
1253 GRANT ALL PRIVILEGES ON FUNCTION priv_testfunc1(int) TO regress_priv_user4;
1254 GRANT ALL PRIVILEGES ON FUNCTION priv_testfunc_nosuch(int) TO regress_priv_user4;
1255 ERROR: function priv_testfunc_nosuch(integer) does not exist
1256 GRANT ALL PRIVILEGES ON FUNCTION priv_testagg1(int) TO regress_priv_user4;
1257 GRANT ALL PRIVILEGES ON PROCEDURE priv_testproc1(int) TO regress_priv_user4;
1258 CREATE FUNCTION priv_testfunc4(boolean) RETURNS text
1259 AS 'select col1 from atest2 where col2 = $1;'
1260 LANGUAGE sql SECURITY DEFINER;
1261 GRANT EXECUTE ON FUNCTION priv_testfunc4(boolean) TO regress_priv_user3;
1262 SET SESSION AUTHORIZATION regress_priv_user2;
1263 SELECT priv_testfunc1(5), priv_testfunc2(5); -- ok
1264 priv_testfunc1 | priv_testfunc2
1265 ----------------+----------------
1269 CREATE FUNCTION priv_testfunc3(int) RETURNS int AS 'select 2 * $1;' LANGUAGE sql; -- fail
1270 ERROR: permission denied for language sql
1271 SELECT priv_testagg1(x) FROM (VALUES (1), (2), (3)) _(x); -- ok
1277 CALL priv_testproc1(6); -- ok
1278 SET SESSION AUTHORIZATION regress_priv_user3;
1279 SELECT priv_testfunc1(5); -- fail
1280 ERROR: permission denied for function priv_testfunc1
1281 SELECT priv_testagg1(x) FROM (VALUES (1), (2), (3)) _(x); -- fail
1282 ERROR: permission denied for aggregate priv_testagg1
1283 CALL priv_testproc1(6); -- fail
1284 ERROR: permission denied for procedure priv_testproc1
1285 SELECT col1 FROM atest2 WHERE col2 = true; -- fail
1286 ERROR: permission denied for table atest2
1287 SELECT priv_testfunc4(true); -- ok
1293 SET SESSION AUTHORIZATION regress_priv_user4;
1294 SELECT priv_testfunc1(5); -- ok
1300 SELECT priv_testagg1(x) FROM (VALUES (1), (2), (3)) _(x); -- ok
1306 CALL priv_testproc1(6); -- ok
1307 DROP FUNCTION priv_testfunc1(int); -- fail
1308 ERROR: must be owner of function priv_testfunc1
1309 DROP AGGREGATE priv_testagg1(int); -- fail
1310 ERROR: must be owner of aggregate priv_testagg1
1311 DROP PROCEDURE priv_testproc1(int); -- fail
1312 ERROR: must be owner of procedure priv_testproc1
1314 DROP FUNCTION priv_testfunc1(int); -- ok
1315 -- restore to sanity
1316 GRANT ALL PRIVILEGES ON LANGUAGE sql TO PUBLIC;
1317 -- verify privilege checks on array-element coercions
1319 SELECT '{1}'::int4[]::int8[];
1325 REVOKE ALL ON FUNCTION int8(integer) FROM PUBLIC;
1326 SELECT '{1}'::int4[]::int8[]; --superuser, succeed
1332 SET SESSION AUTHORIZATION regress_priv_user4;
1333 SELECT '{1}'::int4[]::int8[]; --other user, fail
1334 ERROR: permission denied for function int8
1336 -- privileges on types
1337 -- switch to superuser
1339 CREATE TYPE priv_testtype1 AS (a int, b text);
1340 REVOKE USAGE ON TYPE priv_testtype1 FROM PUBLIC;
1341 GRANT USAGE ON TYPE priv_testtype1 TO regress_priv_user2;
1342 GRANT USAGE ON TYPE _priv_testtype1 TO regress_priv_user2; -- fail
1343 ERROR: cannot set privileges of array types
1344 HINT: Set the privileges of the element type instead.
1345 GRANT USAGE ON DOMAIN priv_testtype1 TO regress_priv_user2; -- fail
1346 ERROR: "priv_testtype1" is not a domain
1347 CREATE DOMAIN priv_testdomain1 AS int;
1348 REVOKE USAGE on DOMAIN priv_testdomain1 FROM PUBLIC;
1349 GRANT USAGE ON DOMAIN priv_testdomain1 TO regress_priv_user2;
1350 GRANT USAGE ON TYPE priv_testdomain1 TO regress_priv_user2; -- ok
1351 SET SESSION AUTHORIZATION regress_priv_user1;
1352 -- commands that should fail
1353 CREATE AGGREGATE priv_testagg1a(priv_testdomain1) (sfunc = int4_sum, stype = bigint);
1354 ERROR: permission denied for type priv_testdomain1
1355 CREATE DOMAIN priv_testdomain2a AS priv_testdomain1;
1356 ERROR: permission denied for type priv_testdomain1
1357 CREATE DOMAIN priv_testdomain3a AS int;
1358 CREATE FUNCTION castfunc(int) RETURNS priv_testdomain3a AS $$ SELECT $1::priv_testdomain3a $$ LANGUAGE SQL;
1359 CREATE CAST (priv_testdomain1 AS priv_testdomain3a) WITH FUNCTION castfunc(int);
1360 ERROR: permission denied for type priv_testdomain1
1361 DROP FUNCTION castfunc(int) CASCADE;
1362 DROP DOMAIN priv_testdomain3a;
1363 CREATE FUNCTION priv_testfunc5a(a priv_testdomain1) RETURNS int LANGUAGE SQL AS $$ SELECT $1 $$;
1364 ERROR: permission denied for type priv_testdomain1
1365 CREATE FUNCTION priv_testfunc6a(b int) RETURNS priv_testdomain1 LANGUAGE SQL AS $$ SELECT $1::priv_testdomain1 $$;
1366 ERROR: permission denied for type priv_testdomain1
1367 CREATE OPERATOR !+! (PROCEDURE = int4pl, LEFTARG = priv_testdomain1, RIGHTARG = priv_testdomain1);
1368 ERROR: permission denied for type priv_testdomain1
1369 CREATE TABLE test5a (a int, b priv_testdomain1);
1370 ERROR: permission denied for type priv_testdomain1
1371 CREATE TABLE test6a OF priv_testtype1;
1372 ERROR: permission denied for type priv_testtype1
1373 CREATE TABLE test10a (a int[], b priv_testtype1[]);
1374 ERROR: permission denied for type priv_testtype1
1375 CREATE TABLE test9a (a int, b int);
1376 ALTER TABLE test9a ADD COLUMN c priv_testdomain1;
1377 ERROR: permission denied for type priv_testdomain1
1378 ALTER TABLE test9a ALTER COLUMN b TYPE priv_testdomain1;
1379 ERROR: permission denied for type priv_testdomain1
1380 CREATE TYPE test7a AS (a int, b priv_testdomain1);
1381 ERROR: permission denied for type priv_testdomain1
1382 CREATE TYPE test8a AS (a int, b int);
1383 ALTER TYPE test8a ADD ATTRIBUTE c priv_testdomain1;
1384 ERROR: permission denied for type priv_testdomain1
1385 ALTER TYPE test8a ALTER ATTRIBUTE b TYPE priv_testdomain1;
1386 ERROR: permission denied for type priv_testdomain1
1387 CREATE TABLE test11a AS (SELECT 1::priv_testdomain1 AS a);
1388 ERROR: permission denied for type priv_testdomain1
1389 REVOKE ALL ON TYPE priv_testtype1 FROM PUBLIC;
1390 ERROR: permission denied for type priv_testtype1
1391 SET SESSION AUTHORIZATION regress_priv_user2;
1392 -- commands that should succeed
1393 CREATE AGGREGATE priv_testagg1b(priv_testdomain1) (sfunc = int4_sum, stype = bigint);
1394 CREATE DOMAIN priv_testdomain2b AS priv_testdomain1;
1395 CREATE DOMAIN priv_testdomain3b AS int;
1396 CREATE FUNCTION castfunc(int) RETURNS priv_testdomain3b AS $$ SELECT $1::priv_testdomain3b $$ LANGUAGE SQL;
1397 CREATE CAST (priv_testdomain1 AS priv_testdomain3b) WITH FUNCTION castfunc(int);
1398 WARNING: cast will be ignored because the source data type is a domain
1399 CREATE FUNCTION priv_testfunc5b(a priv_testdomain1) RETURNS int LANGUAGE SQL AS $$ SELECT $1 $$;
1400 CREATE FUNCTION priv_testfunc6b(b int) RETURNS priv_testdomain1 LANGUAGE SQL AS $$ SELECT $1::priv_testdomain1 $$;
1401 CREATE OPERATOR !! (PROCEDURE = priv_testfunc5b, RIGHTARG = priv_testdomain1);
1402 CREATE TABLE test5b (a int, b priv_testdomain1);
1403 CREATE TABLE test6b OF priv_testtype1;
1404 CREATE TABLE test10b (a int[], b priv_testtype1[]);
1405 CREATE TABLE test9b (a int, b int);
1406 ALTER TABLE test9b ADD COLUMN c priv_testdomain1;
1407 ALTER TABLE test9b ALTER COLUMN b TYPE priv_testdomain1;
1408 CREATE TYPE test7b AS (a int, b priv_testdomain1);
1409 CREATE TYPE test8b AS (a int, b int);
1410 ALTER TYPE test8b ADD ATTRIBUTE c priv_testdomain1;
1411 ALTER TYPE test8b ALTER ATTRIBUTE b TYPE priv_testdomain1;
1412 CREATE TABLE test11b AS (SELECT 1::priv_testdomain1 AS a);
1413 REVOKE ALL ON TYPE priv_testtype1 FROM PUBLIC;
1414 WARNING: no privileges could be revoked for "priv_testtype1"
1416 DROP AGGREGATE priv_testagg1b(priv_testdomain1);
1417 DROP DOMAIN priv_testdomain2b;
1418 DROP OPERATOR !! (NONE, priv_testdomain1);
1419 DROP FUNCTION priv_testfunc5b(a priv_testdomain1);
1420 DROP FUNCTION priv_testfunc6b(b int);
1427 DROP CAST (priv_testdomain1 AS priv_testdomain3b);
1428 DROP FUNCTION castfunc(int) CASCADE;
1429 DROP DOMAIN priv_testdomain3b;
1431 DROP TYPE priv_testtype1; -- ok
1432 DROP DOMAIN priv_testdomain1; -- ok
1434 SET SESSION AUTHORIZATION regress_priv_user5;
1435 TRUNCATE atest2; -- ok
1436 TRUNCATE atest3; -- fail
1437 ERROR: permission denied for table atest3
1438 -- has_table_privilege function
1440 select has_table_privilege(NULL,'pg_authid','select');
1442 ---------------------
1446 select has_table_privilege('pg_shad','select');
1447 ERROR: relation "pg_shad" does not exist
1448 select has_table_privilege('nosuchuser','pg_authid','select');
1449 ERROR: role "nosuchuser" does not exist
1450 select has_table_privilege('pg_authid','sel');
1451 ERROR: unrecognized privilege type: "sel"
1452 select has_table_privilege(-999999,'pg_authid','update');
1454 ---------------------
1458 select has_table_privilege(1,'select');
1460 ---------------------
1466 select has_table_privilege(current_user,'pg_authid','select');
1468 ---------------------
1472 select has_table_privilege(current_user,'pg_authid','insert');
1474 ---------------------
1478 select has_table_privilege(t2.oid,'pg_authid','update')
1479 from (select oid from pg_roles where rolname = current_user) as t2;
1481 ---------------------
1485 select has_table_privilege(t2.oid,'pg_authid','delete')
1486 from (select oid from pg_roles where rolname = current_user) as t2;
1488 ---------------------
1492 select has_table_privilege(current_user,t1.oid,'references')
1493 from (select oid from pg_class where relname = 'pg_authid') as t1;
1495 ---------------------
1499 select has_table_privilege(t2.oid,t1.oid,'select')
1500 from (select oid from pg_class where relname = 'pg_authid') as t1,
1501 (select oid from pg_roles where rolname = current_user) as t2;
1503 ---------------------
1507 select has_table_privilege(t2.oid,t1.oid,'insert')
1508 from (select oid from pg_class where relname = 'pg_authid') as t1,
1509 (select oid from pg_roles where rolname = current_user) as t2;
1511 ---------------------
1515 select has_table_privilege('pg_authid','update');
1517 ---------------------
1521 select has_table_privilege('pg_authid','delete');
1523 ---------------------
1527 select has_table_privilege('pg_authid','truncate');
1529 ---------------------
1533 select has_table_privilege(t1.oid,'select')
1534 from (select oid from pg_class where relname = 'pg_authid') as t1;
1536 ---------------------
1540 select has_table_privilege(t1.oid,'trigger')
1541 from (select oid from pg_class where relname = 'pg_authid') as t1;
1543 ---------------------
1548 SET SESSION AUTHORIZATION regress_priv_user3;
1549 select has_table_privilege(current_user,'pg_class','select');
1551 ---------------------
1555 select has_table_privilege(current_user,'pg_class','insert');
1557 ---------------------
1561 select has_table_privilege(t2.oid,'pg_class','update')
1562 from (select oid from pg_roles where rolname = current_user) as t2;
1564 ---------------------
1568 select has_table_privilege(t2.oid,'pg_class','delete')
1569 from (select oid from pg_roles where rolname = current_user) as t2;
1571 ---------------------
1575 select has_table_privilege(current_user,t1.oid,'references')
1576 from (select oid from pg_class where relname = 'pg_class') as t1;
1578 ---------------------
1582 select has_table_privilege(t2.oid,t1.oid,'select')
1583 from (select oid from pg_class where relname = 'pg_class') as t1,
1584 (select oid from pg_roles where rolname = current_user) as t2;
1586 ---------------------
1590 select has_table_privilege(t2.oid,t1.oid,'insert')
1591 from (select oid from pg_class where relname = 'pg_class') as t1,
1592 (select oid from pg_roles where rolname = current_user) as t2;
1594 ---------------------
1598 select has_table_privilege('pg_class','update');
1600 ---------------------
1604 select has_table_privilege('pg_class','delete');
1606 ---------------------
1610 select has_table_privilege('pg_class','truncate');
1612 ---------------------
1616 select has_table_privilege(t1.oid,'select')
1617 from (select oid from pg_class where relname = 'pg_class') as t1;
1619 ---------------------
1623 select has_table_privilege(t1.oid,'trigger')
1624 from (select oid from pg_class where relname = 'pg_class') as t1;
1626 ---------------------
1630 select has_table_privilege(current_user,'atest1','select');
1632 ---------------------
1636 select has_table_privilege(current_user,'atest1','insert');
1638 ---------------------
1642 select has_table_privilege(t2.oid,'atest1','update')
1643 from (select oid from pg_roles where rolname = current_user) as t2;
1645 ---------------------
1649 select has_table_privilege(t2.oid,'atest1','delete')
1650 from (select oid from pg_roles where rolname = current_user) as t2;
1652 ---------------------
1656 select has_table_privilege(current_user,t1.oid,'references')
1657 from (select oid from pg_class where relname = 'atest1') as t1;
1659 ---------------------
1663 select has_table_privilege(t2.oid,t1.oid,'select')
1664 from (select oid from pg_class where relname = 'atest1') as t1,
1665 (select oid from pg_roles where rolname = current_user) as t2;
1667 ---------------------
1671 select has_table_privilege(t2.oid,t1.oid,'insert')
1672 from (select oid from pg_class where relname = 'atest1') as t1,
1673 (select oid from pg_roles where rolname = current_user) as t2;
1675 ---------------------
1679 select has_table_privilege('atest1','update');
1681 ---------------------
1685 select has_table_privilege('atest1','delete');
1687 ---------------------
1691 select has_table_privilege('atest1','truncate');
1693 ---------------------
1697 select has_table_privilege(t1.oid,'select')
1698 from (select oid from pg_class where relname = 'atest1') as t1;
1700 ---------------------
1704 select has_table_privilege(t1.oid,'trigger')
1705 from (select oid from pg_class where relname = 'atest1') as t1;
1707 ---------------------
1711 -- has_column_privilege function
1712 -- bad-input checks (as non-super-user)
1713 select has_column_privilege('pg_authid',NULL,'select');
1714 has_column_privilege
1715 ----------------------
1719 select has_column_privilege('pg_authid','nosuchcol','select');
1720 ERROR: column "nosuchcol" of relation "pg_authid" does not exist
1721 select has_column_privilege(9999,'nosuchcol','select');
1722 has_column_privilege
1723 ----------------------
1727 select has_column_privilege(9999,99::int2,'select');
1728 has_column_privilege
1729 ----------------------
1733 select has_column_privilege('pg_authid',99::int2,'select');
1734 has_column_privilege
1735 ----------------------
1739 select has_column_privilege(9999,99::int2,'select');
1740 has_column_privilege
1741 ----------------------
1745 create temp table mytable(f1 int, f2 int, f3 int);
1746 alter table mytable drop column f2;
1747 select has_column_privilege('mytable','f2','select');
1748 ERROR: column "f2" of relation "mytable" does not exist
1749 select has_column_privilege('mytable','........pg.dropped.2........','select');
1750 has_column_privilege
1751 ----------------------
1755 select has_column_privilege('mytable',2::int2,'select');
1756 has_column_privilege
1757 ----------------------
1761 select has_column_privilege('mytable',99::int2,'select');
1762 has_column_privilege
1763 ----------------------
1767 revoke select on table mytable from regress_priv_user3;
1768 select has_column_privilege('mytable',2::int2,'select');
1769 has_column_privilege
1770 ----------------------
1774 select has_column_privilege('mytable',99::int2,'select');
1775 has_column_privilege
1776 ----------------------
1782 SET SESSION AUTHORIZATION regress_priv_user1;
1783 CREATE TABLE atest4 (a int);
1784 GRANT SELECT ON atest4 TO regress_priv_user2 WITH GRANT OPTION;
1785 GRANT UPDATE ON atest4 TO regress_priv_user2;
1786 GRANT SELECT ON atest4 TO GROUP regress_priv_group1 WITH GRANT OPTION;
1787 SET SESSION AUTHORIZATION regress_priv_user2;
1788 GRANT SELECT ON atest4 TO regress_priv_user3;
1789 GRANT UPDATE ON atest4 TO regress_priv_user3; -- fail
1790 WARNING: no privileges were granted for "atest4"
1791 SET SESSION AUTHORIZATION regress_priv_user1;
1792 REVOKE SELECT ON atest4 FROM regress_priv_user3; -- does nothing
1793 SELECT has_table_privilege('regress_priv_user3', 'atest4', 'SELECT'); -- true
1795 ---------------------
1799 REVOKE SELECT ON atest4 FROM regress_priv_user2; -- fail
1800 ERROR: dependent privileges exist
1801 HINT: Use CASCADE to revoke them too.
1802 REVOKE GRANT OPTION FOR SELECT ON atest4 FROM regress_priv_user2 CASCADE; -- ok
1803 SELECT has_table_privilege('regress_priv_user2', 'atest4', 'SELECT'); -- true
1805 ---------------------
1809 SELECT has_table_privilege('regress_priv_user3', 'atest4', 'SELECT'); -- false
1811 ---------------------
1815 SELECT has_table_privilege('regress_priv_user1', 'atest4', 'SELECT WITH GRANT OPTION'); -- true
1817 ---------------------
1821 -- security-restricted operations
1823 CREATE ROLE regress_sro_user;
1824 -- Check that index expressions and predicates are run as the table's owner
1825 -- A dummy index function checking current_user
1826 CREATE FUNCTION sro_ifun(int) RETURNS int AS $$
1828 -- Below we set the table's owner to regress_sro_user
1829 ASSERT current_user = 'regress_sro_user',
1830 format('sro_ifun(%s) called by %s', $1, current_user);
1833 $$ LANGUAGE plpgsql IMMUTABLE;
1834 -- Create a table owned by regress_sro_user
1835 CREATE TABLE sro_tab (a int);
1836 ALTER TABLE sro_tab OWNER TO regress_sro_user;
1837 INSERT INTO sro_tab VALUES (1), (2), (3);
1838 -- Create an expression index with a predicate
1839 CREATE INDEX sro_idx ON sro_tab ((sro_ifun(a) + sro_ifun(0)))
1840 WHERE sro_ifun(a + 10) > sro_ifun(10);
1842 -- Do the same concurrently
1843 CREATE INDEX CONCURRENTLY sro_idx ON sro_tab ((sro_ifun(a) + sro_ifun(0)))
1844 WHERE sro_ifun(a + 10) > sro_ifun(10);
1846 REINDEX TABLE sro_tab;
1847 REINDEX INDEX sro_idx;
1848 REINDEX TABLE CONCURRENTLY sro_tab;
1851 CREATE INDEX sro_cluster_idx ON sro_tab ((sro_ifun(a) + sro_ifun(0)));
1852 CLUSTER sro_tab USING sro_cluster_idx;
1853 DROP INDEX sro_cluster_idx;
1855 CREATE INDEX sro_brin ON sro_tab USING brin ((sro_ifun(a) + sro_ifun(0)));
1856 SELECT brin_desummarize_range('sro_brin', 0);
1857 brin_desummarize_range
1858 ------------------------
1862 SELECT brin_summarize_range('sro_brin', 0);
1863 brin_summarize_range
1864 ----------------------
1869 -- Check with a partitioned table
1870 CREATE TABLE sro_ptab (a int) PARTITION BY RANGE (a);
1871 ALTER TABLE sro_ptab OWNER TO regress_sro_user;
1872 CREATE TABLE sro_part PARTITION OF sro_ptab FOR VALUES FROM (1) TO (10);
1873 ALTER TABLE sro_part OWNER TO regress_sro_user;
1874 INSERT INTO sro_ptab VALUES (1), (2), (3);
1875 CREATE INDEX sro_pidx ON sro_ptab ((sro_ifun(a) + sro_ifun(0)))
1876 WHERE sro_ifun(a + 10) > sro_ifun(10);
1877 REINDEX TABLE sro_ptab;
1878 REINDEX INDEX CONCURRENTLY sro_pidx;
1879 SET SESSION AUTHORIZATION regress_sro_user;
1880 CREATE FUNCTION unwanted_grant() RETURNS void LANGUAGE sql AS
1881 'GRANT regress_priv_group2 TO regress_sro_user';
1882 CREATE FUNCTION mv_action() RETURNS bool LANGUAGE sql AS
1883 'DECLARE c CURSOR WITH HOLD FOR SELECT public.unwanted_grant(); SELECT true';
1884 -- REFRESH of this MV will queue a GRANT at end of transaction
1885 CREATE MATERIALIZED VIEW sro_mv AS SELECT mv_action() WITH NO DATA;
1886 REFRESH MATERIALIZED VIEW sro_mv;
1887 ERROR: cannot create a cursor WITH HOLD within security-restricted operation
1888 CONTEXT: SQL function "mv_action" statement 1
1890 REFRESH MATERIALIZED VIEW sro_mv;
1891 ERROR: cannot create a cursor WITH HOLD within security-restricted operation
1892 CONTEXT: SQL function "mv_action" statement 1
1893 SET SESSION AUTHORIZATION regress_sro_user;
1894 -- INSERT to this table will queue a GRANT at end of transaction
1895 CREATE TABLE sro_trojan_table ();
1896 CREATE FUNCTION sro_trojan() RETURNS trigger LANGUAGE plpgsql AS
1897 'BEGIN PERFORM public.unwanted_grant(); RETURN NULL; END';
1898 CREATE CONSTRAINT TRIGGER t AFTER INSERT ON sro_trojan_table
1899 INITIALLY DEFERRED FOR EACH ROW EXECUTE PROCEDURE sro_trojan();
1900 -- Now, REFRESH will issue such an INSERT, queueing the GRANT
1901 CREATE OR REPLACE FUNCTION mv_action() RETURNS bool LANGUAGE sql AS
1902 'INSERT INTO public.sro_trojan_table DEFAULT VALUES; SELECT true';
1903 REFRESH MATERIALIZED VIEW sro_mv;
1904 ERROR: cannot fire deferred trigger within security-restricted operation
1905 CONTEXT: SQL function "mv_action" statement 1
1907 REFRESH MATERIALIZED VIEW sro_mv;
1908 ERROR: cannot fire deferred trigger within security-restricted operation
1909 CONTEXT: SQL function "mv_action" statement 1
1910 BEGIN; SET CONSTRAINTS ALL IMMEDIATE; REFRESH MATERIALIZED VIEW sro_mv; COMMIT;
1911 ERROR: permission denied to grant role "regress_priv_group2"
1912 DETAIL: Only roles with the ADMIN option on role "regress_priv_group2" may grant this role.
1913 CONTEXT: SQL function "unwanted_grant" statement 1
1914 SQL statement "SELECT public.unwanted_grant()"
1915 PL/pgSQL function public.sro_trojan() line 1 at PERFORM
1916 SQL function "mv_action" statement 1
1917 -- REFRESH MATERIALIZED VIEW CONCURRENTLY use of eval_const_expressions()
1918 SET SESSION AUTHORIZATION regress_sro_user;
1919 CREATE FUNCTION unwanted_grant_nofail(int) RETURNS int
1920 IMMUTABLE LANGUAGE plpgsql AS $$
1922 PERFORM public.unwanted_grant();
1923 RAISE WARNING 'owned';
1925 EXCEPTION WHEN OTHERS THEN
1928 CREATE MATERIALIZED VIEW sro_index_mv AS SELECT 1 AS c;
1929 CREATE UNIQUE INDEX ON sro_index_mv (c) WHERE unwanted_grant_nofail(1) > 0;
1931 REFRESH MATERIALIZED VIEW CONCURRENTLY sro_index_mv;
1932 REFRESH MATERIALIZED VIEW sro_index_mv;
1933 DROP OWNED BY regress_sro_user;
1934 DROP ROLE regress_sro_user;
1936 SET SESSION AUTHORIZATION regress_priv_user4;
1937 CREATE FUNCTION dogrant_ok() RETURNS void LANGUAGE sql SECURITY DEFINER AS
1938 'GRANT regress_priv_group2 TO regress_priv_user5';
1939 GRANT regress_priv_group2 TO regress_priv_user5; -- ok: had ADMIN OPTION
1940 SET ROLE regress_priv_group2;
1941 GRANT regress_priv_group2 TO regress_priv_user5; -- fails: SET ROLE suspended privilege
1942 ERROR: permission denied to grant role "regress_priv_group2"
1943 DETAIL: Only roles with the ADMIN option on role "regress_priv_group2" may grant this role.
1944 SET SESSION AUTHORIZATION regress_priv_user1;
1945 GRANT regress_priv_group2 TO regress_priv_user5; -- fails: no ADMIN OPTION
1946 ERROR: permission denied to grant role "regress_priv_group2"
1947 DETAIL: Only roles with the ADMIN option on role "regress_priv_group2" may grant this role.
1948 SELECT dogrant_ok(); -- ok: SECURITY DEFINER conveys ADMIN
1949 NOTICE: role "regress_priv_user5" has already been granted membership in role "regress_priv_group2" by role "regress_priv_user4"
1955 SET ROLE regress_priv_group2;
1956 GRANT regress_priv_group2 TO regress_priv_user5; -- fails: SET ROLE did not help
1957 ERROR: permission denied to grant role "regress_priv_group2"
1958 DETAIL: Only roles with the ADMIN option on role "regress_priv_group2" may grant this role.
1959 SET SESSION AUTHORIZATION regress_priv_group2;
1960 GRANT regress_priv_group2 TO regress_priv_user5; -- fails: no self-admin
1961 ERROR: permission denied to grant role "regress_priv_group2"
1962 DETAIL: Only roles with the ADMIN option on role "regress_priv_group2" may grant this role.
1963 SET SESSION AUTHORIZATION regress_priv_user4;
1964 DROP FUNCTION dogrant_ok();
1965 REVOKE regress_priv_group2 FROM regress_priv_user5;
1966 -- has_sequence_privilege tests
1968 CREATE SEQUENCE x_seq;
1969 GRANT USAGE on x_seq to regress_priv_user2;
1970 SELECT has_sequence_privilege('regress_priv_user1', 'atest1', 'SELECT');
1971 ERROR: "atest1" is not a sequence
1972 SELECT has_sequence_privilege('regress_priv_user1', 'x_seq', 'INSERT');
1973 ERROR: unrecognized privilege type: "INSERT"
1974 SELECT has_sequence_privilege('regress_priv_user1', 'x_seq', 'SELECT');
1975 has_sequence_privilege
1976 ------------------------
1980 SET SESSION AUTHORIZATION regress_priv_user2;
1981 SELECT has_sequence_privilege('x_seq', 'USAGE');
1982 has_sequence_privilege
1983 ------------------------
1987 -- largeobject privilege tests
1989 SET SESSION AUTHORIZATION regress_priv_user1;
1990 SELECT lo_create(1001);
1996 SELECT lo_create(1002);
2002 SELECT lo_create(1003);
2008 SELECT lo_create(1004);
2014 SELECT lo_create(1005);
2020 GRANT ALL ON LARGE OBJECT 1001 TO PUBLIC;
2021 GRANT SELECT ON LARGE OBJECT 1003 TO regress_priv_user2;
2022 GRANT SELECT,UPDATE ON LARGE OBJECT 1004 TO regress_priv_user2;
2023 GRANT ALL ON LARGE OBJECT 1005 TO regress_priv_user2;
2024 GRANT SELECT ON LARGE OBJECT 1005 TO regress_priv_user2 WITH GRANT OPTION;
2025 GRANT SELECT, INSERT ON LARGE OBJECT 1001 TO PUBLIC; -- to be failed
2026 ERROR: invalid privilege type INSERT for large object
2027 GRANT SELECT, UPDATE ON LARGE OBJECT 1001 TO nosuchuser; -- to be failed
2028 ERROR: role "nosuchuser" does not exist
2029 GRANT SELECT, UPDATE ON LARGE OBJECT 999 TO PUBLIC; -- to be failed
2030 ERROR: large object 999 does not exist
2032 SET SESSION AUTHORIZATION regress_priv_user2;
2033 SELECT lo_create(2001);
2039 SELECT lo_create(2002);
2045 SELECT loread(lo_open(1001, x'20000'::int), 32); -- allowed, for now
2051 SELECT lowrite(lo_open(1001, x'40000'::int), 'abcd'); -- fail, wrong mode
2052 ERROR: large object descriptor 0 was not opened for writing
2053 SELECT loread(lo_open(1001, x'40000'::int), 32);
2059 SELECT loread(lo_open(1002, x'40000'::int), 32); -- to be denied
2060 ERROR: permission denied for large object 1002
2061 SELECT loread(lo_open(1003, x'40000'::int), 32);
2067 SELECT loread(lo_open(1004, x'40000'::int), 32);
2073 SELECT lowrite(lo_open(1001, x'20000'::int), 'abcd');
2079 SELECT lowrite(lo_open(1002, x'20000'::int), 'abcd'); -- to be denied
2080 ERROR: permission denied for large object 1002
2081 SELECT lowrite(lo_open(1003, x'20000'::int), 'abcd'); -- to be denied
2082 ERROR: permission denied for large object 1003
2083 SELECT lowrite(lo_open(1004, x'20000'::int), 'abcd');
2089 GRANT SELECT ON LARGE OBJECT 1005 TO regress_priv_user3;
2090 GRANT UPDATE ON LARGE OBJECT 1006 TO regress_priv_user3; -- to be denied
2091 ERROR: large object 1006 does not exist
2092 REVOKE ALL ON LARGE OBJECT 2001, 2002 FROM PUBLIC;
2093 GRANT ALL ON LARGE OBJECT 2001 TO regress_priv_user3;
2094 SELECT lo_unlink(1001); -- to be denied
2095 ERROR: must be owner of large object 1001
2096 SELECT lo_unlink(2002);
2103 -- confirm ACL setting
2104 SELECT oid, pg_get_userbyid(lomowner) ownername, lomacl FROM pg_largeobject_metadata WHERE oid >= 1000 AND oid < 3000 ORDER BY oid;
2105 oid | ownername | lomacl
2106 ------+--------------------+------------------------------------------------------------------------------------------------------------------------------
2107 1001 | regress_priv_user1 | {regress_priv_user1=rw/regress_priv_user1,=rw/regress_priv_user1}
2108 1002 | regress_priv_user1 |
2109 1003 | regress_priv_user1 | {regress_priv_user1=rw/regress_priv_user1,regress_priv_user2=r/regress_priv_user1}
2110 1004 | regress_priv_user1 | {regress_priv_user1=rw/regress_priv_user1,regress_priv_user2=rw/regress_priv_user1}
2111 1005 | regress_priv_user1 | {regress_priv_user1=rw/regress_priv_user1,regress_priv_user2=r*w/regress_priv_user1,regress_priv_user3=r/regress_priv_user2}
2112 2001 | regress_priv_user2 | {regress_priv_user2=rw/regress_priv_user2,regress_priv_user3=rw/regress_priv_user2}
2115 SET SESSION AUTHORIZATION regress_priv_user3;
2116 SELECT loread(lo_open(1001, x'40000'::int), 32);
2122 SELECT loread(lo_open(1003, x'40000'::int), 32); -- to be denied
2123 ERROR: permission denied for large object 1003
2124 SELECT loread(lo_open(1005, x'40000'::int), 32);
2130 SELECT lo_truncate(lo_open(1005, x'20000'::int), 10); -- to be denied
2131 ERROR: permission denied for large object 1005
2132 SELECT lo_truncate(lo_open(2001, x'20000'::int), 10);
2138 -- has_largeobject_privilege function
2141 SELECT has_largeobject_privilege(1001, 'SELECT');
2142 has_largeobject_privilege
2143 ---------------------------
2147 SELECT has_largeobject_privilege(1002, 'SELECT');
2148 has_largeobject_privilege
2149 ---------------------------
2153 SELECT has_largeobject_privilege(1003, 'SELECT');
2154 has_largeobject_privilege
2155 ---------------------------
2159 SELECT has_largeobject_privilege(1004, 'SELECT');
2160 has_largeobject_privilege
2161 ---------------------------
2165 SELECT has_largeobject_privilege(1001, 'UPDATE');
2166 has_largeobject_privilege
2167 ---------------------------
2171 SELECT has_largeobject_privilege(1002, 'UPDATE');
2172 has_largeobject_privilege
2173 ---------------------------
2177 SELECT has_largeobject_privilege(1003, 'UPDATE');
2178 has_largeobject_privilege
2179 ---------------------------
2183 SELECT has_largeobject_privilege(1004, 'UPDATE');
2184 has_largeobject_privilege
2185 ---------------------------
2189 -- not-existing large object
2190 SELECT has_largeobject_privilege(9999, 'SELECT'); -- NULL
2191 has_largeobject_privilege
2192 ---------------------------
2197 SET SESSION AUTHORIZATION regress_priv_user2;
2198 SELECT has_largeobject_privilege(1001, 'SELECT');
2199 has_largeobject_privilege
2200 ---------------------------
2204 SELECT has_largeobject_privilege(1002, 'SELECT'); -- false
2205 has_largeobject_privilege
2206 ---------------------------
2210 SELECT has_largeobject_privilege(1003, 'SELECT');
2211 has_largeobject_privilege
2212 ---------------------------
2216 SELECT has_largeobject_privilege(1004, 'SELECT');
2217 has_largeobject_privilege
2218 ---------------------------
2222 SELECT has_largeobject_privilege(1001, 'UPDATE');
2223 has_largeobject_privilege
2224 ---------------------------
2228 SELECT has_largeobject_privilege(1002, 'UPDATE'); -- false
2229 has_largeobject_privilege
2230 ---------------------------
2234 SELECT has_largeobject_privilege(1003, 'UPDATE'); -- false
2235 has_largeobject_privilege
2236 ---------------------------
2240 SELECT has_largeobject_privilege(1004, 'UPDATE');
2241 has_largeobject_privilege
2242 ---------------------------
2246 SELECT has_largeobject_privilege('regress_priv_user3', 1001, 'SELECT');
2247 has_largeobject_privilege
2248 ---------------------------
2252 SELECT has_largeobject_privilege('regress_priv_user3', 1003, 'SELECT'); -- false
2253 has_largeobject_privilege
2254 ---------------------------
2258 SELECT has_largeobject_privilege('regress_priv_user3', 1005, 'SELECT');
2259 has_largeobject_privilege
2260 ---------------------------
2264 SELECT has_largeobject_privilege('regress_priv_user3', 1005, 'UPDATE'); -- false
2265 has_largeobject_privilege
2266 ---------------------------
2270 SELECT has_largeobject_privilege('regress_priv_user3', 2001, 'UPDATE');
2271 has_largeobject_privilege
2272 ---------------------------
2276 -- compatibility mode in largeobject permission
2278 SET lo_compat_privileges = false; -- default setting
2279 SET SESSION AUTHORIZATION regress_priv_user4;
2280 SELECT has_largeobject_privilege(1002, 'SELECT'); -- false
2281 has_largeobject_privilege
2282 ---------------------------
2286 SELECT has_largeobject_privilege(1002, 'UPDATE'); -- false
2287 has_largeobject_privilege
2288 ---------------------------
2292 SELECT loread(lo_open(1002, x'40000'::int), 32); -- to be denied
2293 ERROR: permission denied for large object 1002
2294 SELECT lowrite(lo_open(1002, x'20000'::int), 'abcd'); -- to be denied
2295 ERROR: permission denied for large object 1002
2296 SELECT lo_truncate(lo_open(1002, x'20000'::int), 10); -- to be denied
2297 ERROR: permission denied for large object 1002
2298 SELECT lo_put(1002, 1, 'abcd'); -- to be denied
2299 ERROR: permission denied for large object 1002
2300 SELECT lo_unlink(1002); -- to be denied
2301 ERROR: must be owner of large object 1002
2302 SELECT lo_export(1001, '/dev/null'); -- to be denied
2303 ERROR: permission denied for function lo_export
2304 SELECT lo_import('/dev/null'); -- to be denied
2305 ERROR: permission denied for function lo_import
2306 SELECT lo_import('/dev/null', 2003); -- to be denied
2307 ERROR: permission denied for function lo_import
2309 SET lo_compat_privileges = true; -- compatibility mode
2310 SET SESSION AUTHORIZATION regress_priv_user4;
2311 SELECT has_largeobject_privilege(1002, 'SELECT'); -- true
2312 has_largeobject_privilege
2313 ---------------------------
2317 SELECT has_largeobject_privilege(1002, 'UPDATE'); -- true
2318 has_largeobject_privilege
2319 ---------------------------
2323 SELECT loread(lo_open(1002, x'40000'::int), 32);
2329 SELECT lowrite(lo_open(1002, x'20000'::int), 'abcd');
2335 SELECT lo_truncate(lo_open(1002, x'20000'::int), 10);
2341 SELECT lo_unlink(1002);
2347 SELECT lo_export(1001, '/dev/null'); -- to be denied
2348 ERROR: permission denied for function lo_export
2349 -- don't allow unpriv users to access pg_largeobject contents
2351 SELECT * FROM pg_largeobject LIMIT 0;
2352 loid | pageno | data
2353 ------+--------+------
2356 SET SESSION AUTHORIZATION regress_priv_user1;
2357 SELECT * FROM pg_largeobject LIMIT 0; -- to be denied
2358 ERROR: permission denied for table pg_largeobject
2359 -- pg_signal_backend can't signal superusers
2360 RESET SESSION AUTHORIZATION;
2362 CREATE OR REPLACE FUNCTION terminate_nothrow(pid int) RETURNS bool
2363 LANGUAGE plpgsql SECURITY DEFINER SET client_min_messages = error AS $$
2365 RETURN pg_terminate_backend($1);
2366 EXCEPTION WHEN OTHERS THEN
2369 ALTER FUNCTION terminate_nothrow OWNER TO pg_signal_backend;
2370 SELECT backend_type FROM pg_stat_activity
2371 WHERE CASE WHEN COALESCE(usesysid, 10) = 10 THEN terminate_nothrow(pid) END;
2377 -- test pg_database_owner
2378 RESET SESSION AUTHORIZATION;
2379 GRANT pg_database_owner TO regress_priv_user1;
2380 ERROR: role "pg_database_owner" cannot have explicit members
2381 GRANT regress_priv_user1 TO pg_database_owner;
2382 ERROR: role "pg_database_owner" cannot be a member of any role
2383 CREATE TABLE datdba_only ();
2384 ALTER TABLE datdba_only OWNER TO pg_database_owner;
2385 REVOKE DELETE ON datdba_only FROM pg_database_owner;
2387 pg_has_role('regress_priv_user1', 'pg_database_owner', 'USAGE') as priv,
2388 pg_has_role('regress_priv_user1', 'pg_database_owner', 'MEMBER') as mem,
2389 pg_has_role('regress_priv_user1', 'pg_database_owner',
2390 'MEMBER WITH ADMIN OPTION') as admin;
2392 ------+-----+-------
2397 DO $$BEGIN EXECUTE format(
2398 'ALTER DATABASE %I OWNER TO regress_priv_group2', current_catalog); END$$;
2400 pg_has_role('regress_priv_user1', 'pg_database_owner', 'USAGE') as priv,
2401 pg_has_role('regress_priv_user1', 'pg_database_owner', 'MEMBER') as mem,
2402 pg_has_role('regress_priv_user1', 'pg_database_owner',
2403 'MEMBER WITH ADMIN OPTION') as admin;
2405 ------+-----+-------
2409 SET SESSION AUTHORIZATION regress_priv_user1;
2410 TABLE information_schema.enabled_roles ORDER BY role_name COLLATE "C";
2412 ---------------------
2418 TABLE information_schema.applicable_roles ORDER BY role_name COLLATE "C";
2419 grantee | role_name | is_grantable
2420 ---------------------+---------------------+--------------
2421 regress_priv_group2 | pg_database_owner | NO
2422 regress_priv_user1 | regress_priv_group2 | NO
2425 INSERT INTO datdba_only DEFAULT VALUES;
2426 SAVEPOINT q; DELETE FROM datdba_only; ROLLBACK TO q;
2427 ERROR: permission denied for table datdba_only
2428 SET SESSION AUTHORIZATION regress_priv_user2;
2429 TABLE information_schema.enabled_roles;
2431 --------------------
2435 INSERT INTO datdba_only DEFAULT VALUES;
2436 ERROR: permission denied for table datdba_only
2438 -- test default ACLs
2440 CREATE SCHEMA testns;
2441 GRANT ALL ON SCHEMA testns TO regress_priv_user1;
2442 CREATE TABLE testns.acltest1 (x int);
2443 SELECT has_table_privilege('regress_priv_user1', 'testns.acltest1', 'SELECT'); -- no
2445 ---------------------
2449 SELECT has_table_privilege('regress_priv_user1', 'testns.acltest1', 'INSERT'); -- no
2451 ---------------------
2455 -- placeholder for test with duplicated schema and role names
2456 ALTER DEFAULT PRIVILEGES IN SCHEMA testns,testns GRANT SELECT ON TABLES TO public,public;
2457 SELECT has_table_privilege('regress_priv_user1', 'testns.acltest1', 'SELECT'); -- no
2459 ---------------------
2463 SELECT has_table_privilege('regress_priv_user1', 'testns.acltest1', 'INSERT'); -- no
2465 ---------------------
2469 DROP TABLE testns.acltest1;
2470 CREATE TABLE testns.acltest1 (x int);
2471 SELECT has_table_privilege('regress_priv_user1', 'testns.acltest1', 'SELECT'); -- yes
2473 ---------------------
2477 SELECT has_table_privilege('regress_priv_user1', 'testns.acltest1', 'INSERT'); -- no
2479 ---------------------
2483 ALTER DEFAULT PRIVILEGES IN SCHEMA testns GRANT INSERT ON TABLES TO regress_priv_user1;
2484 DROP TABLE testns.acltest1;
2485 CREATE TABLE testns.acltest1 (x int);
2486 SELECT has_table_privilege('regress_priv_user1', 'testns.acltest1', 'SELECT'); -- yes
2488 ---------------------
2492 SELECT has_table_privilege('regress_priv_user1', 'testns.acltest1', 'INSERT'); -- yes
2494 ---------------------
2498 ALTER DEFAULT PRIVILEGES IN SCHEMA testns REVOKE INSERT ON TABLES FROM regress_priv_user1;
2499 DROP TABLE testns.acltest1;
2500 CREATE TABLE testns.acltest1 (x int);
2501 SELECT has_table_privilege('regress_priv_user1', 'testns.acltest1', 'SELECT'); -- yes
2503 ---------------------
2507 SELECT has_table_privilege('regress_priv_user1', 'testns.acltest1', 'INSERT'); -- no
2509 ---------------------
2513 ALTER DEFAULT PRIVILEGES FOR ROLE regress_priv_user1 REVOKE EXECUTE ON FUNCTIONS FROM public;
2514 ALTER DEFAULT PRIVILEGES IN SCHEMA testns GRANT USAGE ON SCHEMAS TO regress_priv_user2; -- error
2515 ERROR: cannot use IN SCHEMA clause when using GRANT/REVOKE ON SCHEMAS
2516 -- Test makeaclitem()
2517 SELECT makeaclitem('regress_priv_user1'::regrole, 'regress_priv_user2'::regrole,
2518 'SELECT', TRUE); -- single privilege
2520 ------------------------------------------
2521 regress_priv_user1=r*/regress_priv_user2
2524 SELECT makeaclitem('regress_priv_user1'::regrole, 'regress_priv_user2'::regrole,
2525 'SELECT, INSERT, UPDATE , DELETE ', FALSE); -- multiple privileges
2527 --------------------------------------------
2528 regress_priv_user1=arwd/regress_priv_user2
2531 SELECT makeaclitem('regress_priv_user1'::regrole, 'regress_priv_user2'::regrole,
2532 'SELECT, fake_privilege', FALSE); -- error
2533 ERROR: unrecognized privilege type: "fake_privilege"
2534 -- Test non-throwing aclitem I/O
2535 SELECT pg_input_is_valid('regress_priv_user1=r/regress_priv_user2', 'aclitem');
2541 SELECT pg_input_is_valid('regress_priv_user1=r/', 'aclitem');
2547 SELECT * FROM pg_input_error_info('regress_priv_user1=r/', 'aclitem');
2548 message | detail | hint | sql_error_code
2549 ---------------------------------+--------+------+----------------
2550 a name must follow the "/" sign | | | 22P02
2553 SELECT pg_input_is_valid('regress_priv_user1=r/regress_no_such_user', 'aclitem');
2559 SELECT * FROM pg_input_error_info('regress_priv_user1=r/regress_no_such_user', 'aclitem');
2560 message | detail | hint | sql_error_code
2561 --------------------------------------------+--------+------+----------------
2562 role "regress_no_such_user" does not exist | | | 42704
2565 SELECT pg_input_is_valid('regress_priv_user1=rY', 'aclitem');
2571 SELECT * FROM pg_input_error_info('regress_priv_user1=rY', 'aclitem');
2572 message | detail | hint | sql_error_code
2573 ----------------------------------------------------------+--------+------+----------------
2574 invalid mode character: must be one of "arwdDxtXUCTcsAm" | | | 22P02
2578 -- Testing blanket default grants is very hazardous since it might change
2579 -- the privileges attached to objects created by concurrent regression tests.
2580 -- To avoid that, be sure to revoke the privileges again before committing.
2583 ALTER DEFAULT PRIVILEGES GRANT USAGE ON SCHEMAS TO regress_priv_user2;
2584 CREATE SCHEMA testns2;
2585 SELECT has_schema_privilege('regress_priv_user2', 'testns2', 'USAGE'); -- yes
2586 has_schema_privilege
2587 ----------------------
2591 SELECT has_schema_privilege('regress_priv_user6', 'testns2', 'USAGE'); -- yes
2592 has_schema_privilege
2593 ----------------------
2597 SELECT has_schema_privilege('regress_priv_user2', 'testns2', 'CREATE'); -- no
2598 has_schema_privilege
2599 ----------------------
2603 ALTER DEFAULT PRIVILEGES REVOKE USAGE ON SCHEMAS FROM regress_priv_user2;
2604 CREATE SCHEMA testns3;
2605 SELECT has_schema_privilege('regress_priv_user2', 'testns3', 'USAGE'); -- no
2606 has_schema_privilege
2607 ----------------------
2611 SELECT has_schema_privilege('regress_priv_user2', 'testns3', 'CREATE'); -- no
2612 has_schema_privilege
2613 ----------------------
2617 ALTER DEFAULT PRIVILEGES GRANT ALL ON SCHEMAS TO regress_priv_user2;
2618 CREATE SCHEMA testns4;
2619 SELECT has_schema_privilege('regress_priv_user2', 'testns4', 'USAGE'); -- yes
2620 has_schema_privilege
2621 ----------------------
2625 SELECT has_schema_privilege('regress_priv_user2', 'testns4', 'CREATE'); -- yes
2626 has_schema_privilege
2627 ----------------------
2631 ALTER DEFAULT PRIVILEGES REVOKE ALL ON SCHEMAS FROM regress_priv_user2;
2633 -- Test for DROP OWNED BY with shared dependencies. This is done in a
2634 -- separate, rollbacked, transaction to avoid any trouble with other
2635 -- regression sessions.
2637 ALTER DEFAULT PRIVILEGES GRANT ALL ON FUNCTIONS TO regress_priv_user2;
2638 ALTER DEFAULT PRIVILEGES GRANT ALL ON SCHEMAS TO regress_priv_user2;
2639 ALTER DEFAULT PRIVILEGES GRANT ALL ON SEQUENCES TO regress_priv_user2;
2640 ALTER DEFAULT PRIVILEGES GRANT ALL ON TABLES TO regress_priv_user2;
2641 ALTER DEFAULT PRIVILEGES GRANT ALL ON TYPES TO regress_priv_user2;
2642 SELECT count(*) FROM pg_shdepend
2643 WHERE deptype = 'a' AND
2644 refobjid = 'regress_priv_user2'::regrole AND
2645 classid = 'pg_default_acl'::regclass;
2651 DROP OWNED BY regress_priv_user2, regress_priv_user2;
2652 SELECT count(*) FROM pg_shdepend
2653 WHERE deptype = 'a' AND
2654 refobjid = 'regress_priv_user2'::regrole AND
2655 classid = 'pg_default_acl'::regclass;
2662 CREATE SCHEMA testns5;
2663 SELECT has_schema_privilege('regress_priv_user2', 'testns5', 'USAGE'); -- no
2664 has_schema_privilege
2665 ----------------------
2669 SELECT has_schema_privilege('regress_priv_user2', 'testns5', 'CREATE'); -- no
2670 has_schema_privilege
2671 ----------------------
2675 SET ROLE regress_priv_user1;
2676 CREATE FUNCTION testns.foo() RETURNS int AS 'select 1' LANGUAGE sql;
2677 CREATE AGGREGATE testns.agg1(int) (sfunc = int4pl, stype = int4);
2678 CREATE PROCEDURE testns.bar() AS 'select 1' LANGUAGE sql;
2679 SELECT has_function_privilege('regress_priv_user2', 'testns.foo()', 'EXECUTE'); -- no
2680 has_function_privilege
2681 ------------------------
2685 SELECT has_function_privilege('regress_priv_user2', 'testns.agg1(int)', 'EXECUTE'); -- no
2686 has_function_privilege
2687 ------------------------
2691 SELECT has_function_privilege('regress_priv_user2', 'testns.bar()', 'EXECUTE'); -- no
2692 has_function_privilege
2693 ------------------------
2697 ALTER DEFAULT PRIVILEGES IN SCHEMA testns GRANT EXECUTE ON ROUTINES to public;
2698 DROP FUNCTION testns.foo();
2699 CREATE FUNCTION testns.foo() RETURNS int AS 'select 1' LANGUAGE sql;
2700 DROP AGGREGATE testns.agg1(int);
2701 CREATE AGGREGATE testns.agg1(int) (sfunc = int4pl, stype = int4);
2702 DROP PROCEDURE testns.bar();
2703 CREATE PROCEDURE testns.bar() AS 'select 1' LANGUAGE sql;
2704 SELECT has_function_privilege('regress_priv_user2', 'testns.foo()', 'EXECUTE'); -- yes
2705 has_function_privilege
2706 ------------------------
2710 SELECT has_function_privilege('regress_priv_user2', 'testns.agg1(int)', 'EXECUTE'); -- yes
2711 has_function_privilege
2712 ------------------------
2716 SELECT has_function_privilege('regress_priv_user2', 'testns.bar()', 'EXECUTE'); -- yes (counts as function here)
2717 has_function_privilege
2718 ------------------------
2722 DROP FUNCTION testns.foo();
2723 DROP AGGREGATE testns.agg1(int);
2724 DROP PROCEDURE testns.bar();
2725 ALTER DEFAULT PRIVILEGES FOR ROLE regress_priv_user1 REVOKE USAGE ON TYPES FROM public;
2726 CREATE DOMAIN testns.priv_testdomain1 AS int;
2727 SELECT has_type_privilege('regress_priv_user2', 'testns.priv_testdomain1', 'USAGE'); -- no
2729 --------------------
2733 ALTER DEFAULT PRIVILEGES IN SCHEMA testns GRANT USAGE ON TYPES to public;
2734 DROP DOMAIN testns.priv_testdomain1;
2735 CREATE DOMAIN testns.priv_testdomain1 AS int;
2736 SELECT has_type_privilege('regress_priv_user2', 'testns.priv_testdomain1', 'USAGE'); -- yes
2738 --------------------
2742 DROP DOMAIN testns.priv_testdomain1;
2745 FROM pg_default_acl d LEFT JOIN pg_namespace n ON defaclnamespace = n.oid
2746 WHERE nspname = 'testns';
2752 DROP SCHEMA testns CASCADE;
2753 NOTICE: drop cascades to table testns.acltest1
2754 DROP SCHEMA testns2 CASCADE;
2755 DROP SCHEMA testns3 CASCADE;
2756 DROP SCHEMA testns4 CASCADE;
2757 DROP SCHEMA testns5 CASCADE;
2758 SELECT d.* -- check that entries went away
2759 FROM pg_default_acl d LEFT JOIN pg_namespace n ON defaclnamespace = n.oid
2760 WHERE nspname IS NULL AND defaclnamespace != 0;
2761 oid | defaclrole | defaclnamespace | defaclobjtype | defaclacl
2762 -----+------------+-----------------+---------------+-----------
2765 -- Grant on all objects of given type in a schema
2767 CREATE SCHEMA testns;
2768 CREATE TABLE testns.t1 (f1 int);
2769 CREATE TABLE testns.t2 (f1 int);
2770 SELECT has_table_privilege('regress_priv_user1', 'testns.t1', 'SELECT'); -- false
2772 ---------------------
2776 GRANT ALL ON ALL TABLES IN SCHEMA testns TO regress_priv_user1;
2777 SELECT has_table_privilege('regress_priv_user1', 'testns.t1', 'SELECT'); -- true
2779 ---------------------
2783 SELECT has_table_privilege('regress_priv_user1', 'testns.t2', 'SELECT'); -- true
2785 ---------------------
2789 REVOKE ALL ON ALL TABLES IN SCHEMA testns FROM regress_priv_user1;
2790 SELECT has_table_privilege('regress_priv_user1', 'testns.t1', 'SELECT'); -- false
2792 ---------------------
2796 SELECT has_table_privilege('regress_priv_user1', 'testns.t2', 'SELECT'); -- false
2798 ---------------------
2802 CREATE FUNCTION testns.priv_testfunc(int) RETURNS int AS 'select 3 * $1;' LANGUAGE sql;
2803 CREATE AGGREGATE testns.priv_testagg(int) (sfunc = int4pl, stype = int4);
2804 CREATE PROCEDURE testns.priv_testproc(int) AS 'select 3' LANGUAGE sql;
2805 SELECT has_function_privilege('regress_priv_user1', 'testns.priv_testfunc(int)', 'EXECUTE'); -- true by default
2806 has_function_privilege
2807 ------------------------
2811 SELECT has_function_privilege('regress_priv_user1', 'testns.priv_testagg(int)', 'EXECUTE'); -- true by default
2812 has_function_privilege
2813 ------------------------
2817 SELECT has_function_privilege('regress_priv_user1', 'testns.priv_testproc(int)', 'EXECUTE'); -- true by default
2818 has_function_privilege
2819 ------------------------
2823 REVOKE ALL ON ALL FUNCTIONS IN SCHEMA testns FROM PUBLIC;
2824 SELECT has_function_privilege('regress_priv_user1', 'testns.priv_testfunc(int)', 'EXECUTE'); -- false
2825 has_function_privilege
2826 ------------------------
2830 SELECT has_function_privilege('regress_priv_user1', 'testns.priv_testagg(int)', 'EXECUTE'); -- false
2831 has_function_privilege
2832 ------------------------
2836 SELECT has_function_privilege('regress_priv_user1', 'testns.priv_testproc(int)', 'EXECUTE'); -- still true, not a function
2837 has_function_privilege
2838 ------------------------
2842 REVOKE ALL ON ALL PROCEDURES IN SCHEMA testns FROM PUBLIC;
2843 SELECT has_function_privilege('regress_priv_user1', 'testns.priv_testproc(int)', 'EXECUTE'); -- now false
2844 has_function_privilege
2845 ------------------------
2849 GRANT ALL ON ALL ROUTINES IN SCHEMA testns TO PUBLIC;
2850 SELECT has_function_privilege('regress_priv_user1', 'testns.priv_testfunc(int)', 'EXECUTE'); -- true
2851 has_function_privilege
2852 ------------------------
2856 SELECT has_function_privilege('regress_priv_user1', 'testns.priv_testagg(int)', 'EXECUTE'); -- true
2857 has_function_privilege
2858 ------------------------
2862 SELECT has_function_privilege('regress_priv_user1', 'testns.priv_testproc(int)', 'EXECUTE'); -- true
2863 has_function_privilege
2864 ------------------------
2868 DROP SCHEMA testns CASCADE;
2869 NOTICE: drop cascades to 5 other objects
2870 DETAIL: drop cascades to table testns.t1
2871 drop cascades to table testns.t2
2872 drop cascades to function testns.priv_testfunc(integer)
2873 drop cascades to function testns.priv_testagg(integer)
2874 drop cascades to function testns.priv_testproc(integer)
2875 -- Change owner of the schema & and rename of new schema owner
2877 CREATE ROLE regress_schemauser1 superuser login;
2878 CREATE ROLE regress_schemauser2 superuser login;
2879 SET SESSION ROLE regress_schemauser1;
2880 CREATE SCHEMA testns;
2881 SELECT nspname, rolname FROM pg_namespace, pg_roles WHERE pg_namespace.nspname = 'testns' AND pg_namespace.nspowner = pg_roles.oid;
2883 ---------+---------------------
2884 testns | regress_schemauser1
2887 ALTER SCHEMA testns OWNER TO regress_schemauser2;
2888 ALTER ROLE regress_schemauser2 RENAME TO regress_schemauser_renamed;
2889 SELECT nspname, rolname FROM pg_namespace, pg_roles WHERE pg_namespace.nspname = 'testns' AND pg_namespace.nspowner = pg_roles.oid;
2891 ---------+----------------------------
2892 testns | regress_schemauser_renamed
2895 set session role regress_schemauser_renamed;
2896 DROP SCHEMA testns CASCADE;
2899 DROP ROLE regress_schemauser1;
2900 DROP ROLE regress_schemauser_renamed;
2901 -- test that dependent privileges are revoked (or not) properly
2903 set session role regress_priv_user1;
2904 create table dep_priv_test (a int);
2905 grant select on dep_priv_test to regress_priv_user2 with grant option;
2906 grant select on dep_priv_test to regress_priv_user3 with grant option;
2907 set session role regress_priv_user2;
2908 grant select on dep_priv_test to regress_priv_user4 with grant option;
2909 set session role regress_priv_user3;
2910 grant select on dep_priv_test to regress_priv_user4 with grant option;
2911 set session role regress_priv_user4;
2912 grant select on dep_priv_test to regress_priv_user5;
2915 Schema | Name | Type | Access privileges | Column privileges | Policies
2916 --------+---------------+-------+------------------------------------------------+-------------------+----------
2917 public | dep_priv_test | table | regress_priv_user1=arwdDxtm/regress_priv_user1+| |
2918 | | | regress_priv_user2=r*/regress_priv_user1 +| |
2919 | | | regress_priv_user3=r*/regress_priv_user1 +| |
2920 | | | regress_priv_user4=r*/regress_priv_user2 +| |
2921 | | | regress_priv_user4=r*/regress_priv_user3 +| |
2922 | | | regress_priv_user5=r/regress_priv_user4 | |
2925 set session role regress_priv_user2;
2926 revoke select on dep_priv_test from regress_priv_user4 cascade;
2929 Schema | Name | Type | Access privileges | Column privileges | Policies
2930 --------+---------------+-------+------------------------------------------------+-------------------+----------
2931 public | dep_priv_test | table | regress_priv_user1=arwdDxtm/regress_priv_user1+| |
2932 | | | regress_priv_user2=r*/regress_priv_user1 +| |
2933 | | | regress_priv_user3=r*/regress_priv_user1 +| |
2934 | | | regress_priv_user4=r*/regress_priv_user3 +| |
2935 | | | regress_priv_user5=r/regress_priv_user4 | |
2938 set session role regress_priv_user3;
2939 revoke select on dep_priv_test from regress_priv_user4 cascade;
2942 Schema | Name | Type | Access privileges | Column privileges | Policies
2943 --------+---------------+-------+------------------------------------------------+-------------------+----------
2944 public | dep_priv_test | table | regress_priv_user1=arwdDxtm/regress_priv_user1+| |
2945 | | | regress_priv_user2=r*/regress_priv_user1 +| |
2946 | | | regress_priv_user3=r*/regress_priv_user1 | |
2949 set session role regress_priv_user1;
2950 drop table dep_priv_test;
2953 drop sequence x_seq;
2954 DROP AGGREGATE priv_testagg1(int);
2955 DROP FUNCTION priv_testfunc2(int);
2956 DROP FUNCTION priv_testfunc4(boolean);
2957 DROP PROCEDURE priv_testproc1(int);
2961 -- this should cascade to drop atestv4
2962 DROP VIEW atestv3 CASCADE;
2963 NOTICE: drop cascades to view atestv4
2964 -- this should complain "does not exist"
2966 ERROR: view "atestv4" does not exist
2976 SELECT lo_unlink(oid) FROM pg_largeobject_metadata WHERE oid >= 1000 AND oid < 3000 ORDER BY oid;
2986 DROP GROUP regress_priv_group1;
2987 DROP GROUP regress_priv_group2;
2988 -- these are needed to clean up permissions
2989 REVOKE USAGE ON LANGUAGE sql FROM regress_priv_user1;
2990 DROP OWNED BY regress_priv_user1;
2991 DROP USER regress_priv_user1;
2992 DROP USER regress_priv_user2;
2993 DROP USER regress_priv_user3;
2994 DROP USER regress_priv_user4;
2995 DROP USER regress_priv_user5;
2996 DROP USER regress_priv_user6;
2997 DROP USER regress_priv_user7;
2998 DROP USER regress_priv_user8; -- does not exist
2999 ERROR: role "regress_priv_user8" does not exist
3000 -- permissions with LOCK TABLE
3001 CREATE USER regress_locktable_user;
3002 CREATE TABLE lock_table (a int);
3003 -- LOCK TABLE and SELECT permission
3004 GRANT SELECT ON lock_table TO regress_locktable_user;
3005 SET SESSION AUTHORIZATION regress_locktable_user;
3007 LOCK TABLE lock_table IN ACCESS SHARE MODE; -- should pass
3010 LOCK TABLE lock_table IN ROW EXCLUSIVE MODE; -- should fail
3011 ERROR: permission denied for table lock_table
3014 LOCK TABLE lock_table IN ACCESS EXCLUSIVE MODE; -- should fail
3015 ERROR: permission denied for table lock_table
3018 REVOKE SELECT ON lock_table FROM regress_locktable_user;
3019 -- LOCK TABLE and INSERT permission
3020 GRANT INSERT ON lock_table TO regress_locktable_user;
3021 SET SESSION AUTHORIZATION regress_locktable_user;
3023 LOCK TABLE lock_table IN ACCESS SHARE MODE; -- should pass
3026 LOCK TABLE lock_table IN ROW EXCLUSIVE MODE; -- should pass
3029 LOCK TABLE lock_table IN ACCESS EXCLUSIVE MODE; -- should fail
3030 ERROR: permission denied for table lock_table
3033 REVOKE INSERT ON lock_table FROM regress_locktable_user;
3034 -- LOCK TABLE and UPDATE permission
3035 GRANT UPDATE ON lock_table TO regress_locktable_user;
3036 SET SESSION AUTHORIZATION regress_locktable_user;
3038 LOCK TABLE lock_table IN ACCESS SHARE MODE; -- should pass
3041 LOCK TABLE lock_table IN ROW EXCLUSIVE MODE; -- should pass
3044 LOCK TABLE lock_table IN ACCESS EXCLUSIVE MODE; -- should pass
3047 REVOKE UPDATE ON lock_table FROM regress_locktable_user;
3048 -- LOCK TABLE and DELETE permission
3049 GRANT DELETE ON lock_table TO regress_locktable_user;
3050 SET SESSION AUTHORIZATION regress_locktable_user;
3052 LOCK TABLE lock_table IN ACCESS SHARE MODE; -- should pass
3055 LOCK TABLE lock_table IN ROW EXCLUSIVE MODE; -- should pass
3058 LOCK TABLE lock_table IN ACCESS EXCLUSIVE MODE; -- should pass
3061 REVOKE DELETE ON lock_table FROM regress_locktable_user;
3062 -- LOCK TABLE and TRUNCATE permission
3063 GRANT TRUNCATE ON lock_table TO regress_locktable_user;
3064 SET SESSION AUTHORIZATION regress_locktable_user;
3066 LOCK TABLE lock_table IN ACCESS SHARE MODE; -- should pass
3069 LOCK TABLE lock_table IN ROW EXCLUSIVE MODE; -- should pass
3072 LOCK TABLE lock_table IN ACCESS EXCLUSIVE MODE; -- should pass
3075 REVOKE TRUNCATE ON lock_table FROM regress_locktable_user;
3076 -- LOCK TABLE and MAINTAIN permission
3077 GRANT MAINTAIN ON lock_table TO regress_locktable_user;
3078 SET SESSION AUTHORIZATION regress_locktable_user;
3080 LOCK TABLE lock_table IN ACCESS SHARE MODE; -- should pass
3083 LOCK TABLE lock_table IN ROW EXCLUSIVE MODE; -- should pass
3086 LOCK TABLE lock_table IN ACCESS EXCLUSIVE MODE; -- should pass
3089 REVOKE MAINTAIN ON lock_table FROM regress_locktable_user;
3091 DROP TABLE lock_table;
3092 DROP USER regress_locktable_user;
3093 -- test to check privileges of system views pg_shmem_allocations and
3094 -- pg_backend_memory_contexts.
3095 -- switch to superuser
3097 CREATE ROLE regress_readallstats;
3098 SELECT has_table_privilege('regress_readallstats','pg_backend_memory_contexts','SELECT'); -- no
3100 ---------------------
3104 SELECT has_table_privilege('regress_readallstats','pg_shmem_allocations','SELECT'); -- no
3106 ---------------------
3110 GRANT pg_read_all_stats TO regress_readallstats;
3111 SELECT has_table_privilege('regress_readallstats','pg_backend_memory_contexts','SELECT'); -- yes
3113 ---------------------
3117 SELECT has_table_privilege('regress_readallstats','pg_shmem_allocations','SELECT'); -- yes
3119 ---------------------
3123 -- run query to ensure that functions within views can be executed
3124 SET ROLE regress_readallstats;
3125 SELECT COUNT(*) >= 0 AS ok FROM pg_backend_memory_contexts;
3131 SELECT COUNT(*) >= 0 AS ok FROM pg_shmem_allocations;
3139 DROP ROLE regress_readallstats;
3140 -- test role grantor machinery
3141 CREATE ROLE regress_group;
3142 CREATE ROLE regress_group_direct_manager;
3143 CREATE ROLE regress_group_indirect_manager;
3144 CREATE ROLE regress_group_member;
3145 GRANT regress_group TO regress_group_direct_manager WITH INHERIT FALSE, ADMIN TRUE;
3146 GRANT regress_group_direct_manager TO regress_group_indirect_manager;
3147 SET SESSION AUTHORIZATION regress_group_direct_manager;
3148 GRANT regress_group TO regress_group_member;
3149 SELECT member::regrole::text, CASE WHEN grantor = 10 THEN 'BOOTSTRAP SUPERUSER' ELSE grantor::regrole::text END FROM pg_auth_members WHERE roleid = 'regress_group'::regrole ORDER BY 1, 2;
3151 ------------------------------+------------------------------
3152 regress_group_direct_manager | BOOTSTRAP SUPERUSER
3153 regress_group_member | regress_group_direct_manager
3156 REVOKE regress_group FROM regress_group_member;
3157 SET SESSION AUTHORIZATION regress_group_indirect_manager;
3158 GRANT regress_group TO regress_group_member;
3159 SELECT member::regrole::text, CASE WHEN grantor = 10 THEN 'BOOTSTRAP SUPERUSER' ELSE grantor::regrole::text END FROM pg_auth_members WHERE roleid = 'regress_group'::regrole ORDER BY 1, 2;
3161 ------------------------------+------------------------------
3162 regress_group_direct_manager | BOOTSTRAP SUPERUSER
3163 regress_group_member | regress_group_direct_manager
3166 REVOKE regress_group FROM regress_group_member;
3167 RESET SESSION AUTHORIZATION;
3168 DROP ROLE regress_group;
3169 DROP ROLE regress_group_direct_manager;
3170 DROP ROLE regress_group_indirect_manager;
3171 DROP ROLE regress_group_member;
3172 -- test SET and INHERIT options with object ownership changes
3173 CREATE ROLE regress_roleoption_protagonist;
3174 CREATE ROLE regress_roleoption_donor;
3175 CREATE ROLE regress_roleoption_recipient;
3176 CREATE SCHEMA regress_roleoption;
3177 GRANT CREATE, USAGE ON SCHEMA regress_roleoption TO PUBLIC;
3178 GRANT regress_roleoption_donor TO regress_roleoption_protagonist WITH INHERIT TRUE, SET FALSE;
3179 GRANT regress_roleoption_recipient TO regress_roleoption_protagonist WITH INHERIT FALSE, SET TRUE;
3180 SET SESSION AUTHORIZATION regress_roleoption_protagonist;
3181 CREATE TABLE regress_roleoption.t1 (a int);
3182 CREATE TABLE regress_roleoption.t2 (a int);
3183 SET SESSION AUTHORIZATION regress_roleoption_donor;
3184 CREATE TABLE regress_roleoption.t3 (a int);
3185 SET SESSION AUTHORIZATION regress_roleoption_recipient;
3186 CREATE TABLE regress_roleoption.t4 (a int);
3187 SET SESSION AUTHORIZATION regress_roleoption_protagonist;
3188 ALTER TABLE regress_roleoption.t1 OWNER TO regress_roleoption_donor; -- fails, can't be come donor
3189 ERROR: must be able to SET ROLE "regress_roleoption_donor"
3190 ALTER TABLE regress_roleoption.t2 OWNER TO regress_roleoption_recipient; -- works
3191 ALTER TABLE regress_roleoption.t3 OWNER TO regress_roleoption_protagonist; -- works
3192 ALTER TABLE regress_roleoption.t4 OWNER TO regress_roleoption_protagonist; -- fails, we don't inherit from recipient
3193 ERROR: must be owner of table t4
3194 RESET SESSION AUTHORIZATION;
3195 DROP TABLE regress_roleoption.t1;
3196 DROP TABLE regress_roleoption.t2;
3197 DROP TABLE regress_roleoption.t3;
3198 DROP TABLE regress_roleoption.t4;
3199 DROP SCHEMA regress_roleoption;
3200 DROP ROLE regress_roleoption_protagonist;
3201 DROP ROLE regress_roleoption_donor;
3202 DROP ROLE regress_roleoption_recipient;
3204 CREATE ROLE regress_no_maintain;
3205 CREATE ROLE regress_maintain;
3206 CREATE ROLE regress_maintain_all IN ROLE pg_maintain;
3207 CREATE TABLE maintain_test (a INT);
3208 CREATE INDEX ON maintain_test (a);
3209 GRANT MAINTAIN ON maintain_test TO regress_maintain;
3210 CREATE MATERIALIZED VIEW refresh_test AS SELECT 1;
3211 GRANT MAINTAIN ON refresh_test TO regress_maintain;
3212 CREATE SCHEMA reindex_test;
3213 -- negative tests; should fail
3214 SET ROLE regress_no_maintain;
3215 VACUUM maintain_test;
3216 WARNING: permission denied to vacuum "maintain_test", skipping it
3217 ANALYZE maintain_test;
3218 WARNING: permission denied to analyze "maintain_test", skipping it
3219 VACUUM (ANALYZE) maintain_test;
3220 WARNING: permission denied to vacuum "maintain_test", skipping it
3221 CLUSTER maintain_test USING maintain_test_a_idx;
3222 ERROR: permission denied for table maintain_test
3223 REFRESH MATERIALIZED VIEW refresh_test;
3224 ERROR: permission denied for materialized view refresh_test
3225 REINDEX TABLE maintain_test;
3226 ERROR: permission denied for table maintain_test
3227 REINDEX INDEX maintain_test_a_idx;
3228 ERROR: permission denied for index maintain_test_a_idx
3229 REINDEX SCHEMA reindex_test;
3230 ERROR: must be owner of schema reindex_test
3232 SET ROLE regress_maintain;
3233 VACUUM maintain_test;
3234 ANALYZE maintain_test;
3235 VACUUM (ANALYZE) maintain_test;
3236 CLUSTER maintain_test USING maintain_test_a_idx;
3237 REFRESH MATERIALIZED VIEW refresh_test;
3238 REINDEX TABLE maintain_test;
3239 REINDEX INDEX maintain_test_a_idx;
3240 REINDEX SCHEMA reindex_test;
3241 ERROR: must be owner of schema reindex_test
3243 SET ROLE regress_maintain_all;
3244 VACUUM maintain_test;
3245 ANALYZE maintain_test;
3246 VACUUM (ANALYZE) maintain_test;
3247 CLUSTER maintain_test USING maintain_test_a_idx;
3248 REFRESH MATERIALIZED VIEW refresh_test;
3249 REINDEX TABLE maintain_test;
3250 REINDEX INDEX maintain_test_a_idx;
3251 REINDEX SCHEMA reindex_test;
3253 DROP TABLE maintain_test;
3254 DROP MATERIALIZED VIEW refresh_test;
3255 DROP SCHEMA reindex_test;
3256 DROP ROLE regress_no_maintain;
3257 DROP ROLE regress_maintain;
3258 DROP ROLE regress_maintain_all;