2 -- Test the LOCK statement
5 CREATE SCHEMA lock_schema1;
6 SET search_path = lock_schema1;
7 CREATE TABLE lock_tbl1 (a BIGINT);
8 CREATE TABLE lock_tbl1a (a BIGINT);
9 CREATE VIEW lock_view1 AS SELECT * FROM lock_tbl1;
10 CREATE VIEW lock_view2(a,b) AS SELECT * FROM lock_tbl1, lock_tbl1a;
11 CREATE VIEW lock_view3 AS SELECT * from lock_view2;
12 CREATE VIEW lock_view4 AS SELECT (select a from lock_tbl1a limit 1) from lock_tbl1;
13 CREATE VIEW lock_view5 AS SELECT * from lock_tbl1 where a in (select * from lock_tbl1a);
14 CREATE VIEW lock_view6 AS SELECT * from (select * from lock_tbl1) sub;
15 CREATE ROLE regress_rol_lock1;
16 ALTER ROLE regress_rol_lock1 SET search_path = lock_schema1;
17 GRANT USAGE ON SCHEMA lock_schema1 TO regress_rol_lock1;
18 -- Try all valid lock options; also try omitting the optional TABLE keyword.
20 LOCK TABLE lock_tbl1 IN ACCESS SHARE MODE;
21 LOCK lock_tbl1 IN ROW SHARE MODE;
22 LOCK TABLE lock_tbl1 IN ROW EXCLUSIVE MODE;
23 LOCK TABLE lock_tbl1 IN SHARE UPDATE EXCLUSIVE MODE;
24 LOCK TABLE lock_tbl1 IN SHARE MODE;
25 LOCK lock_tbl1 IN SHARE ROW EXCLUSIVE MODE;
26 LOCK TABLE lock_tbl1 IN EXCLUSIVE MODE;
27 LOCK TABLE lock_tbl1 IN ACCESS EXCLUSIVE MODE;
29 -- Try using NOWAIT along with valid options.
31 LOCK TABLE lock_tbl1 IN ACCESS SHARE MODE NOWAIT;
32 LOCK TABLE lock_tbl1 IN ROW SHARE MODE NOWAIT;
33 LOCK TABLE lock_tbl1 IN ROW EXCLUSIVE MODE NOWAIT;
34 LOCK TABLE lock_tbl1 IN SHARE UPDATE EXCLUSIVE MODE NOWAIT;
35 LOCK TABLE lock_tbl1 IN SHARE MODE NOWAIT;
36 LOCK TABLE lock_tbl1 IN SHARE ROW EXCLUSIVE MODE NOWAIT;
37 LOCK TABLE lock_tbl1 IN EXCLUSIVE MODE NOWAIT;
38 LOCK TABLE lock_tbl1 IN ACCESS EXCLUSIVE MODE NOWAIT;
40 -- Verify that we can lock views.
42 LOCK TABLE lock_view1 IN EXCLUSIVE MODE;
43 -- lock_view1 and lock_tbl1 are locked.
44 select relname from pg_locks l, pg_class c
45 where l.relation = c.oid and relname like '%lock_%' and mode = 'ExclusiveLock'
55 LOCK TABLE lock_view2 IN EXCLUSIVE MODE;
56 -- lock_view1, lock_tbl1, and lock_tbl1a are locked.
57 select relname from pg_locks l, pg_class c
58 where l.relation = c.oid and relname like '%lock_%' and mode = 'ExclusiveLock'
69 LOCK TABLE lock_view3 IN EXCLUSIVE MODE;
70 -- lock_view3, lock_view2, lock_tbl1, and lock_tbl1a are locked recursively.
71 select relname from pg_locks l, pg_class c
72 where l.relation = c.oid and relname like '%lock_%' and mode = 'ExclusiveLock'
84 LOCK TABLE lock_view4 IN EXCLUSIVE MODE;
85 -- lock_view4, lock_tbl1, and lock_tbl1a are locked.
86 select relname from pg_locks l, pg_class c
87 where l.relation = c.oid and relname like '%lock_%' and mode = 'ExclusiveLock'
98 LOCK TABLE lock_view5 IN EXCLUSIVE MODE;
99 -- lock_view5, lock_tbl1, and lock_tbl1a are locked.
100 select relname from pg_locks l, pg_class c
101 where l.relation = c.oid and relname like '%lock_%' and mode = 'ExclusiveLock'
112 LOCK TABLE lock_view6 IN EXCLUSIVE MODE;
113 -- lock_view6 an lock_tbl1 are locked.
114 select relname from pg_locks l, pg_class c
115 where l.relation = c.oid and relname like '%lock_%' and mode = 'ExclusiveLock'
124 -- Verify that we cope with infinite recursion in view definitions.
125 CREATE OR REPLACE VIEW lock_view2 AS SELECT * from lock_view3;
127 LOCK TABLE lock_view2 IN EXCLUSIVE MODE;
129 CREATE VIEW lock_view7 AS SELECT * from lock_view2;
131 LOCK TABLE lock_view7 IN EXCLUSIVE MODE;
133 -- Verify that we can lock a table with inheritance children.
134 CREATE TABLE lock_tbl2 (b BIGINT) INHERITS (lock_tbl1);
135 CREATE TABLE lock_tbl3 () INHERITS (lock_tbl2);
137 LOCK TABLE lock_tbl1 * IN ACCESS EXCLUSIVE MODE;
139 -- Child tables are locked without granting explicit permission to do so as
140 -- long as we have permission to lock the parent.
141 GRANT UPDATE ON TABLE lock_tbl1 TO regress_rol_lock1;
142 SET ROLE regress_rol_lock1;
143 -- fail when child locked directly
145 LOCK TABLE lock_tbl2;
146 ERROR: permission denied for table lock_tbl2
149 LOCK TABLE lock_tbl1 * IN ACCESS EXCLUSIVE MODE;
152 LOCK TABLE ONLY lock_tbl1;
158 DROP VIEW lock_view7;
159 DROP VIEW lock_view6;
160 DROP VIEW lock_view5;
161 DROP VIEW lock_view4;
162 DROP VIEW lock_view3 CASCADE;
163 NOTICE: drop cascades to view lock_view2
164 DROP VIEW lock_view1;
165 DROP TABLE lock_tbl3;
166 DROP TABLE lock_tbl2;
167 DROP TABLE lock_tbl1;
168 DROP TABLE lock_tbl1a;
169 DROP SCHEMA lock_schema1 CASCADE;
170 DROP ROLE regress_rol_lock1;
173 SELECT test_atomic_ops();