Skip to content

Commit 9e55518

Browse files
committed
Revert "Accept relations of any kind in LOCK TABLE".
Revert 59ab4ac, as well as the followup fix 33862cb, in all branches. We need to think a bit harder about what the behavior of LOCK TABLE on views should be, and there's no time for that before next week's releases. We'll take another crack at this later. Discussion: https://postgr.es/m/16703-e348f58aab3cf6cc@postgresql.org
1 parent 768ab4d commit 9e55518

File tree

4 files changed

+20
-44
lines changed

4 files changed

+20
-44
lines changed

doc/src/sgml/ref/lock.sgml

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ PostgreSQL documentation
1616

1717
<refnamediv>
1818
<refname>LOCK</refname>
19-
<refpurpose>lock a named relation (table, etc)</refpurpose>
19+
<refpurpose>lock a table</refpurpose>
2020
</refnamediv>
2121

2222
<refsynopsisdiv>
@@ -34,9 +34,7 @@ LOCK [ TABLE ] [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
3434
<title>Description</title>
3535

3636
<para>
37-
<command>LOCK TABLE</command> obtains a table-level lock on a
38-
relation (table, partitioned table, foreign table, view,
39-
materialized view, index, composite type, sequence), waiting
37+
<command>LOCK TABLE</command> obtains a table-level lock, waiting
4038
if necessary for any conflicting locks to be released. If
4139
<literal>NOWAIT</literal> is specified, <command>LOCK
4240
TABLE</command> does not wait to acquire the desired lock: if it
@@ -112,23 +110,17 @@ LOCK [ TABLE ] [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
112110
<term><replaceable class="PARAMETER">name</replaceable></term>
113111
<listitem>
114112
<para>
115-
The name (optionally schema-qualified) of an existing relation to
116-
lock. If <literal>ONLY</literal> is specified before a table name, only that
117-
table is locked. If <literal>ONLY</literal> is not specified, the table and all
118-
its descendant tables (if any) are locked. Optionally, <literal>*</literal>
113+
The name (optionally schema-qualified) of an existing table to
114+
lock. If <literal>ONLY</> is specified before the table name, only that
115+
table is locked. If <literal>ONLY</> is not specified, the table and all
116+
its descendant tables (if any) are locked. Optionally, <literal>*</>
119117
can be specified after the table name to explicitly indicate that
120118
descendant tables are included.
121119
</para>
122120

123121
<para>
124-
Locking a view locks only the view object itself, not any referenced
125-
relations. (Note that this behavior is different
126-
in <productname>PostgreSQL</productname> v11 and later.)
127-
</para>
128-
129-
<para>
130-
The command <literal>LOCK TABLE a, b;</literal> is equivalent to
131-
<literal>LOCK TABLE a; LOCK TABLE b;</literal>. The relations are locked
122+
The command <literal>LOCK TABLE a, b;</> is equivalent to
123+
<literal>LOCK TABLE a; LOCK TABLE b;</>. The tables are locked
132124
one-by-one in the order specified in the <command>LOCK
133125
TABLE</command> command.
134126
</para>

src/backend/commands/lockcmds.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid,
8787
return; /* woops, concurrently dropped; no permissions
8888
* check */
8989

90+
/* Currently, we only allow plain tables to be locked */
91+
if (relkind != RELKIND_RELATION)
92+
ereport(ERROR,
93+
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
94+
errmsg("\"%s\" is not a table",
95+
rv->relname)));
96+
9097
/* Check permissions. */
9198
aclresult = LockTableAclCheck(relid, lockmode);
9299
if (aclresult != ACLCHECK_OK)

src/test/regress/expected/lock.out

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
CREATE SCHEMA lock_schema1;
66
SET search_path = lock_schema1;
77
CREATE TABLE lock_tbl1 (a BIGINT);
8-
CREATE VIEW lock_view1 AS SELECT 1 AS a;
9-
CREATE MATERIALIZED VIEW lock_mv1 AS SELECT * FROM lock_view1;
10-
CREATE INDEX lock_mvi1 ON lock_mv1 (a);
11-
CREATE SEQUENCE lock_seq;
8+
CREATE VIEW lock_view1 AS SELECT 1;
129
CREATE ROLE regress_rol_lock1;
1310
ALTER ROLE regress_rol_lock1 SET search_path = lock_schema1;
1411
GRANT USAGE ON SCHEMA lock_schema1 TO regress_rol_lock1;
@@ -33,7 +30,8 @@ LOCK TABLE lock_tbl1 IN SHARE MODE NOWAIT;
3330
LOCK TABLE lock_tbl1 IN SHARE ROW EXCLUSIVE MODE NOWAIT;
3431
LOCK TABLE lock_tbl1 IN EXCLUSIVE MODE NOWAIT;
3532
LOCK TABLE lock_tbl1 IN ACCESS EXCLUSIVE MODE NOWAIT;
36-
LOCK TABLE lock_view1 IN EXCLUSIVE MODE;
33+
LOCK TABLE lock_view1 IN EXCLUSIVE MODE; -- Will fail; can't lock a non-table
34+
ERROR: "lock_view1" is not a table
3735
ROLLBACK;
3836
-- Verify that we can lock a table with inheritance children.
3937
CREATE TABLE lock_tbl2 (b BIGINT) INHERITS (lock_tbl1);
@@ -53,21 +51,13 @@ BEGIN;
5351
LOCK TABLE ONLY lock_tbl1;
5452
ROLLBACK;
5553
RESET ROLE;
56-
-- Lock other relations
57-
BEGIN TRANSACTION;
58-
LOCK TABLE lock_mv1;
59-
LOCK TABLE lock_mvi1;
60-
LOCK TABLE lock_seq;
61-
ROLLBACK;
6254
--
6355
-- Clean up
6456
--
65-
DROP MATERIALIZED VIEW lock_mv1;
6657
DROP VIEW lock_view1;
6758
DROP TABLE lock_tbl3;
6859
DROP TABLE lock_tbl2;
6960
DROP TABLE lock_tbl1;
70-
DROP SEQUENCE lock_seq;
7161
DROP SCHEMA lock_schema1 CASCADE;
7262
DROP ROLE regress_rol_lock1;
7363
-- atomic ops tests

src/test/regress/sql/lock.sql

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
CREATE SCHEMA lock_schema1;
77
SET search_path = lock_schema1;
88
CREATE TABLE lock_tbl1 (a BIGINT);
9-
CREATE VIEW lock_view1 AS SELECT 1 AS a;
10-
CREATE MATERIALIZED VIEW lock_mv1 AS SELECT * FROM lock_view1;
11-
CREATE INDEX lock_mvi1 ON lock_mv1 (a);
12-
CREATE SEQUENCE lock_seq;
9+
CREATE VIEW lock_view1 AS SELECT 1;
1310
CREATE ROLE regress_rol_lock1;
1411
ALTER ROLE regress_rol_lock1 SET search_path = lock_schema1;
1512
GRANT USAGE ON SCHEMA lock_schema1 TO regress_rol_lock1;
@@ -36,7 +33,7 @@ LOCK TABLE lock_tbl1 IN SHARE MODE NOWAIT;
3633
LOCK TABLE lock_tbl1 IN SHARE ROW EXCLUSIVE MODE NOWAIT;
3734
LOCK TABLE lock_tbl1 IN EXCLUSIVE MODE NOWAIT;
3835
LOCK TABLE lock_tbl1 IN ACCESS EXCLUSIVE MODE NOWAIT;
39-
LOCK TABLE lock_view1 IN EXCLUSIVE MODE;
36+
LOCK TABLE lock_view1 IN EXCLUSIVE MODE; -- Will fail; can't lock a non-table
4037
ROLLBACK;
4138

4239
-- Verify that we can lock a table with inheritance children.
@@ -58,23 +55,13 @@ LOCK TABLE ONLY lock_tbl1;
5855
ROLLBACK;
5956
RESET ROLE;
6057

61-
-- Lock other relations
62-
BEGIN TRANSACTION;
63-
LOCK TABLE lock_mv1;
64-
LOCK TABLE lock_mvi1;
65-
LOCK TABLE lock_seq;
66-
ROLLBACK;
67-
68-
6958
--
7059
-- Clean up
7160
--
72-
DROP MATERIALIZED VIEW lock_mv1;
7361
DROP VIEW lock_view1;
7462
DROP TABLE lock_tbl3;
7563
DROP TABLE lock_tbl2;
7664
DROP TABLE lock_tbl1;
77-
DROP SEQUENCE lock_seq;
7865
DROP SCHEMA lock_schema1 CASCADE;
7966
DROP ROLE regress_rol_lock1;
8067

0 commit comments

Comments
 (0)