LOCK
- lock a table
+ lock a named relation (table, etc)
Description
- LOCK TABLE obtains a table-level lock, waiting
+ LOCK TABLE obtains a table-level lock on a
+ relation (table, partitioned table, foreign table, view,
+ materialized view, index, composite type, sequence), waiting
if necessary for any conflicting locks to be released. If
NOWAIT is specified, LOCK
TABLE does not wait to acquire the desired lock: if it
name
- The name (optionally schema-qualified) of an existing table to
- lock. If ONLY> is specified before the table name, only that
- table is locked. If ONLY> is not specified, the table and all
- its descendant tables (if any) are locked. Optionally, *>
+ The name (optionally schema-qualified) of an existing relation to
+ lock. If ONLYliteral> is specified before a table name, only that
+ table is locked. If ONLYliteral> is not specified, the table and all
+ its descendant tables (if any) are locked. Optionally, *literal>
can be specified after the table name to explicitly indicate that
- descendant tables are included.
+ descendant tables are included. When locking a view, all relations appearing
+ in the view definition are locked, regardless of ONLY.
- The command LOCK TABLE a, b;> is equivalent to
- LOCK TABLE a; LOCK TABLE b;>. The tables are locked
+ The command LOCK TABLE a, b;literal> is equivalent to
+ LOCK TABLE a; LOCK TABLE b;literal>. The relations are locked
one-by-one in the order specified in the LOCK
TABLE command.
return; /* woops, concurrently dropped; no permissions
* check */
- /* Currently, we only allow plain tables to be locked */
- if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE)
- ereport(ERROR,
- (errcode(ERRCODE_WRONG_OBJECT_TYPE),
- errmsg("\"%s\" is not a table",
- rv->relname)));
-
/*
* Make note if a temporary relation has been accessed in this
* transaction.
CREATE SCHEMA lock_schema1;
SET search_path = lock_schema1;
CREATE TABLE lock_tbl1 (a BIGINT);
-CREATE VIEW lock_view1 AS SELECT 1;
+CREATE VIEW lock_view1 AS SELECT 1 AS a;
+CREATE MATERIALIZED VIEW lock_mv1 AS SELECT * FROM lock_view1;
+CREATE INDEX lock_mvi1 ON lock_mv1 (a);
+CREATE SEQUENCE lock_seq;
CREATE ROLE regress_rol_lock1;
ALTER ROLE regress_rol_lock1 SET search_path = lock_schema1;
GRANT USAGE ON SCHEMA lock_schema1 TO regress_rol_lock1;
LOCK TABLE lock_tbl1 IN SHARE ROW EXCLUSIVE MODE NOWAIT;
LOCK TABLE lock_tbl1 IN EXCLUSIVE MODE NOWAIT;
LOCK TABLE lock_tbl1 IN ACCESS EXCLUSIVE MODE NOWAIT;
-LOCK TABLE lock_view1 IN EXCLUSIVE MODE; -- Will fail; can't lock a non-table
-ERROR: "lock_view1" is not a table
+LOCK TABLE lock_view1 IN EXCLUSIVE MODE;
ROLLBACK;
-- Verify that we can lock a table with inheritance children.
CREATE TABLE lock_tbl2 (b BIGINT) INHERITS (lock_tbl1);
LOCK TABLE ONLY lock_tbl1;
ROLLBACK;
RESET ROLE;
+-- Lock other relations
+BEGIN TRANSACTION;
+LOCK TABLE lock_mv1;
+LOCK TABLE lock_mvi1;
+LOCK TABLE lock_seq;
+ROLLBACK;
--
-- Clean up
--
+DROP MATERIALIZED VIEW lock_mv1;
DROP VIEW lock_view1;
DROP TABLE lock_tbl3;
DROP TABLE lock_tbl2;
DROP TABLE lock_tbl1;
+DROP SEQUENCE lock_seq;
DROP SCHEMA lock_schema1 CASCADE;
DROP ROLE regress_rol_lock1;
-- atomic ops tests
CREATE SCHEMA lock_schema1;
SET search_path = lock_schema1;
CREATE TABLE lock_tbl1 (a BIGINT);
-CREATE VIEW lock_view1 AS SELECT 1;
+CREATE VIEW lock_view1 AS SELECT 1 AS a;
+CREATE MATERIALIZED VIEW lock_mv1 AS SELECT * FROM lock_view1;
+CREATE INDEX lock_mvi1 ON lock_mv1 (a);
+CREATE SEQUENCE lock_seq;
CREATE ROLE regress_rol_lock1;
ALTER ROLE regress_rol_lock1 SET search_path = lock_schema1;
GRANT USAGE ON SCHEMA lock_schema1 TO regress_rol_lock1;
LOCK TABLE lock_tbl1 IN SHARE ROW EXCLUSIVE MODE NOWAIT;
LOCK TABLE lock_tbl1 IN EXCLUSIVE MODE NOWAIT;
LOCK TABLE lock_tbl1 IN ACCESS EXCLUSIVE MODE NOWAIT;
-LOCK TABLE lock_view1 IN EXCLUSIVE MODE; -- Will fail; can't lock a non-table
+LOCK TABLE lock_view1 IN EXCLUSIVE MODE;
ROLLBACK;
-- Verify that we can lock a table with inheritance children.
ROLLBACK;
RESET ROLE;
+-- Lock other relations
+BEGIN TRANSACTION;
+LOCK TABLE lock_mv1;
+LOCK TABLE lock_mvi1;
+LOCK TABLE lock_seq;
+ROLLBACK;
+
+
--
-- Clean up
--
+DROP MATERIALIZED VIEW lock_mv1;
DROP VIEW lock_view1;
DROP TABLE lock_tbl3;
DROP TABLE lock_tbl2;
DROP TABLE lock_tbl1;
+DROP SEQUENCE lock_seq;
DROP SCHEMA lock_schema1 CASCADE;
DROP ROLE regress_rol_lock1;