2 -- Test the LOCK statement
4 -- directory paths and dlsuffix are passed to us in environment variables
5 \getenv libdir PG_LIBDIR
6 \getenv dlsuffix PG_DLSUFFIX
7 \set regresslib :libdir '/regress' :dlsuffix
9 CREATE SCHEMA lock_schema1;
10 SET search_path = lock_schema1;
11 CREATE TABLE lock_tbl1 (a BIGINT);
12 CREATE TABLE lock_tbl1a (a BIGINT);
13 CREATE VIEW lock_view1 AS SELECT * FROM lock_tbl1;
14 CREATE VIEW lock_view2(a,b) AS SELECT * FROM lock_tbl1, lock_tbl1a;
15 CREATE VIEW lock_view3 AS SELECT * from lock_view2;
16 CREATE VIEW lock_view4 AS SELECT (select a from lock_tbl1a limit 1) from lock_tbl1;
17 CREATE VIEW lock_view5 AS SELECT * from lock_tbl1 where a in (select * from lock_tbl1a);
18 CREATE VIEW lock_view6 AS SELECT * from (select * from lock_tbl1) sub;
19 CREATE ROLE regress_rol_lock1;
20 ALTER ROLE regress_rol_lock1 SET search_path = lock_schema1;
21 GRANT USAGE ON SCHEMA lock_schema1 TO regress_rol_lock1;
22 -- Try all valid lock options; also try omitting the optional TABLE keyword.
24 LOCK TABLE lock_tbl1 IN ACCESS SHARE MODE;
25 LOCK lock_tbl1 IN ROW SHARE MODE;
26 LOCK TABLE lock_tbl1 IN ROW EXCLUSIVE MODE;
27 LOCK TABLE lock_tbl1 IN SHARE UPDATE EXCLUSIVE MODE;
28 LOCK TABLE lock_tbl1 IN SHARE MODE;
29 LOCK lock_tbl1 IN SHARE ROW EXCLUSIVE MODE;
30 LOCK TABLE lock_tbl1 IN EXCLUSIVE MODE;
31 LOCK TABLE lock_tbl1 IN ACCESS EXCLUSIVE MODE;
33 -- Try using NOWAIT along with valid options.
35 LOCK TABLE lock_tbl1 IN ACCESS SHARE MODE NOWAIT;
36 LOCK TABLE lock_tbl1 IN ROW SHARE MODE NOWAIT;
37 LOCK TABLE lock_tbl1 IN ROW EXCLUSIVE MODE NOWAIT;
38 LOCK TABLE lock_tbl1 IN SHARE UPDATE EXCLUSIVE MODE NOWAIT;
39 LOCK TABLE lock_tbl1 IN SHARE MODE NOWAIT;
40 LOCK TABLE lock_tbl1 IN SHARE ROW EXCLUSIVE MODE NOWAIT;
41 LOCK TABLE lock_tbl1 IN EXCLUSIVE MODE NOWAIT;
42 LOCK TABLE lock_tbl1 IN ACCESS EXCLUSIVE MODE NOWAIT;
44 -- Verify that we can lock views.
46 LOCK TABLE lock_view1 IN EXCLUSIVE MODE;
47 -- lock_view1 and lock_tbl1 are locked.
48 select relname from pg_locks l, pg_class c
49 where l.relation = c.oid and relname like '%lock_%' and mode = 'ExclusiveLock'
59 LOCK TABLE lock_view2 IN EXCLUSIVE MODE;
60 -- lock_view1, lock_tbl1, and lock_tbl1a are locked.
61 select relname from pg_locks l, pg_class c
62 where l.relation = c.oid and relname like '%lock_%' and mode = 'ExclusiveLock'
73 LOCK TABLE lock_view3 IN EXCLUSIVE MODE;
74 -- lock_view3, lock_view2, lock_tbl1, and lock_tbl1a are locked recursively.
75 select relname from pg_locks l, pg_class c
76 where l.relation = c.oid and relname like '%lock_%' and mode = 'ExclusiveLock'
88 LOCK TABLE lock_view4 IN EXCLUSIVE MODE;
89 -- lock_view4, lock_tbl1, and lock_tbl1a are locked.
90 select relname from pg_locks l, pg_class c
91 where l.relation = c.oid and relname like '%lock_%' and mode = 'ExclusiveLock'
102 LOCK TABLE lock_view5 IN EXCLUSIVE MODE;
103 -- lock_view5, lock_tbl1, and lock_tbl1a are locked.
104 select relname from pg_locks l, pg_class c
105 where l.relation = c.oid and relname like '%lock_%' and mode = 'ExclusiveLock'
116 LOCK TABLE lock_view6 IN EXCLUSIVE MODE;
117 -- lock_view6 an lock_tbl1 are locked.
118 select relname from pg_locks l, pg_class c
119 where l.relation = c.oid and relname like '%lock_%' and mode = 'ExclusiveLock'
128 -- Verify that we cope with infinite recursion in view definitions.
129 CREATE OR REPLACE VIEW lock_view2 AS SELECT * from lock_view3;
131 LOCK TABLE lock_view2 IN EXCLUSIVE MODE;
133 CREATE VIEW lock_view7 AS SELECT * from lock_view2;
135 LOCK TABLE lock_view7 IN EXCLUSIVE MODE;
137 -- Verify that we can lock a table with inheritance children.
138 CREATE TABLE lock_tbl2 (b BIGINT) INHERITS (lock_tbl1);
139 CREATE TABLE lock_tbl3 () INHERITS (lock_tbl2);
141 LOCK TABLE lock_tbl1 * IN ACCESS EXCLUSIVE MODE;
143 -- Child tables are locked without granting explicit permission to do so as
144 -- long as we have permission to lock the parent.
145 GRANT UPDATE ON TABLE lock_tbl1 TO regress_rol_lock1;
146 SET ROLE regress_rol_lock1;
147 -- fail when child locked directly
149 LOCK TABLE lock_tbl2;
150 ERROR: permission denied for table lock_tbl2
153 LOCK TABLE lock_tbl1 * IN ACCESS EXCLUSIVE MODE;
156 LOCK TABLE ONLY lock_tbl1;
159 REVOKE UPDATE ON TABLE lock_tbl1 FROM regress_rol_lock1;
160 -- Tables referred to by views are locked without explicit permission to do so
161 -- as long as we have permission to lock the view itself.
162 SET ROLE regress_rol_lock1;
163 -- fail without permissions on the view
165 LOCK TABLE lock_view1;
166 ERROR: permission denied for view lock_view1
169 GRANT UPDATE ON TABLE lock_view1 TO regress_rol_lock1;
170 SET ROLE regress_rol_lock1;
172 LOCK TABLE lock_view1 IN ACCESS EXCLUSIVE MODE;
173 -- lock_view1 and lock_tbl1 (plus children lock_tbl2 and lock_tbl3) are locked.
174 select relname from pg_locks l, pg_class c
175 where l.relation = c.oid and relname like '%lock_%' and mode = 'AccessExclusiveLock'
187 REVOKE UPDATE ON TABLE lock_view1 FROM regress_rol_lock1;
188 -- Tables referred to by security invoker views require explicit permission to
190 CREATE VIEW lock_view8 WITH (security_invoker) AS SELECT * FROM lock_tbl1;
191 SET ROLE regress_rol_lock1;
192 -- fail without permissions on the view
194 LOCK TABLE lock_view8;
195 ERROR: permission denied for view lock_view8
198 GRANT UPDATE ON TABLE lock_view8 TO regress_rol_lock1;
199 SET ROLE regress_rol_lock1;
200 -- fail without permissions on the table referenced by the view
202 LOCK TABLE lock_view8;
203 ERROR: permission denied for table lock_tbl1
206 GRANT UPDATE ON TABLE lock_tbl1 TO regress_rol_lock1;
208 LOCK TABLE lock_view8 IN ACCESS EXCLUSIVE MODE;
209 -- lock_view8 and lock_tbl1 (plus children lock_tbl2 and lock_tbl3) are locked.
210 select relname from pg_locks l, pg_class c
211 where l.relation = c.oid and relname like '%lock_%' and mode = 'AccessExclusiveLock'
223 REVOKE UPDATE ON TABLE lock_view8 FROM regress_rol_lock1;
227 DROP VIEW lock_view8;
228 DROP VIEW lock_view7;
229 DROP VIEW lock_view6;
230 DROP VIEW lock_view5;
231 DROP VIEW lock_view4;
232 DROP VIEW lock_view3 CASCADE;
233 NOTICE: drop cascades to view lock_view2
234 DROP VIEW lock_view1;
235 DROP TABLE lock_tbl3;
236 DROP TABLE lock_tbl2;
237 DROP TABLE lock_tbl1;
238 DROP TABLE lock_tbl1a;
239 DROP SCHEMA lock_schema1 CASCADE;
240 DROP ROLE regress_rol_lock1;
243 CREATE FUNCTION test_atomic_ops()
247 SELECT test_atomic_ops();