Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4352c23

Browse files
committed
Revert "Accept relations of any kind in LOCK TABLE".
Revert 59ab4ac32, as well as the followup fix 33862cb9c, 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 fa3840c commit 4352c23

File tree

4 files changed

+25
-42
lines changed

4 files changed

+25
-42
lines changed

doc/src/sgml/ref/lock.sgml

Lines changed: 6 additions & 9 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
@@ -117,18 +115,17 @@ LOCK [ TABLE ] [ ONLY ] <replaceable class="parameter">name</replaceable> [ * ]
117115
<term><replaceable class="parameter">name</replaceable></term>
118116
<listitem>
119117
<para>
120-
The name (optionally schema-qualified) of an existing relation to
121-
lock. If <literal>ONLY</literal> is specified before a table name, only that
118+
The name (optionally schema-qualified) of an existing table to
119+
lock. If <literal>ONLY</literal> is specified before the table name, only that
122120
table is locked. If <literal>ONLY</literal> is not specified, the table and all
123121
its descendant tables (if any) are locked. Optionally, <literal>*</literal>
124122
can be specified after the table name to explicitly indicate that
125-
descendant tables are included. When locking a view, all relations appearing
126-
in the view definition are locked, regardless of <literal>ONLY</literal>.
123+
descendant tables are included.
127124
</para>
128125

129126
<para>
130127
The command <literal>LOCK TABLE a, b;</literal> is equivalent to
131-
<literal>LOCK TABLE a; LOCK TABLE b;</literal>. The relations are locked
128+
<literal>LOCK TABLE a; LOCK TABLE b;</literal>. The tables are locked
132129
one-by-one in the order specified in the <command>LOCK
133130
TABLE</command> command.
134131
</para>

src/backend/commands/lockcmds.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid,
8484
return; /* woops, concurrently dropped; no permissions
8585
* check */
8686

87+
/* Currently, we only allow plain tables or views to be locked */
88+
if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE &&
89+
relkind != RELKIND_VIEW)
90+
ereport(ERROR,
91+
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
92+
errmsg("\"%s\" is not a table or view",
93+
rv->relname)));
94+
8795
/*
8896
* Make note if a temporary relation has been accessed in this
8997
* transaction.
@@ -179,13 +187,11 @@ LockViewRecurse_walker(Node *node, LockViewRecurse_context *context)
179187
foreach(rtable, query->rtable)
180188
{
181189
RangeTblEntry *rte = lfirst(rtable);
182-
Oid relid;
183190
AclResult aclresult;
184191

185-
/* ignore all non-relation RTEs */
186-
if (rte->rtekind != RTE_RELATION)
187-
continue;
188-
relid = rte->relid;
192+
Oid relid = rte->relid;
193+
char relkind = rte->relkind;
194+
char *relname = get_rel_name(relid);
189195

190196
/*
191197
* The OLD and NEW placeholder entries in the view's rtable are
@@ -196,6 +202,11 @@ LockViewRecurse_walker(Node *node, LockViewRecurse_context *context)
196202
strcmp(rte->eref->aliasname, "new") == 0))
197203
continue;
198204

205+
/* Currently, we only allow plain tables or views to be locked. */
206+
if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE &&
207+
relkind != RELKIND_VIEW)
208+
continue;
209+
199210
/*
200211
* We might be dealing with a self-referential view. If so, we
201212
* can just stop recursing, since we already locked it.
@@ -206,8 +217,7 @@ LockViewRecurse_walker(Node *node, LockViewRecurse_context *context)
206217
/* Check permissions with the view owner's privilege. */
207218
aclresult = LockTableAclCheck(relid, context->lockmode, context->viewowner);
208219
if (aclresult != ACLCHECK_OK)
209-
aclcheck_error(aclresult, get_relkind_objtype(rte->relkind),
210-
get_rel_name(relid));
220+
aclcheck_error(aclresult, get_relkind_objtype(relkind), relname);
211221

212222
/* We have enough rights to lock the relation; do so. */
213223
if (!context->nowait)
@@ -216,9 +226,9 @@ LockViewRecurse_walker(Node *node, LockViewRecurse_context *context)
216226
ereport(ERROR,
217227
(errcode(ERRCODE_LOCK_NOT_AVAILABLE),
218228
errmsg("could not obtain lock on relation \"%s\"",
219-
get_rel_name(relid))));
229+
relname)));
220230

221-
if (rte->relkind == RELKIND_VIEW)
231+
if (relkind == RELKIND_VIEW)
222232
LockViewRecurse(relid, context->lockmode, context->nowait,
223233
context->ancestor_views);
224234
else if (rte->inh)

src/test/regress/expected/lock.out

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ CREATE VIEW lock_view3 AS SELECT * from lock_view2;
1212
CREATE VIEW lock_view4 AS SELECT (select a from lock_tbl1a limit 1) from lock_tbl1;
1313
CREATE VIEW lock_view5 AS SELECT * from lock_tbl1 where a in (select * from lock_tbl1a);
1414
CREATE VIEW lock_view6 AS SELECT * from (select * from lock_tbl1) sub;
15-
CREATE MATERIALIZED VIEW lock_mv1 AS SELECT * FROM lock_view6;
16-
CREATE INDEX lock_mvi1 ON lock_mv1 (a);
17-
CREATE SEQUENCE lock_seq;
1815
CREATE ROLE regress_rol_lock1;
1916
ALTER ROLE regress_rol_lock1 SET search_path = lock_schema1;
2017
GRANT USAGE ON SCHEMA lock_schema1 TO regress_rol_lock1;
@@ -155,16 +152,9 @@ BEGIN;
155152
LOCK TABLE ONLY lock_tbl1;
156153
ROLLBACK;
157154
RESET ROLE;
158-
-- Lock other relations
159-
BEGIN TRANSACTION;
160-
LOCK TABLE lock_mv1;
161-
LOCK TABLE lock_mvi1;
162-
LOCK TABLE lock_seq;
163-
ROLLBACK;
164155
--
165156
-- Clean up
166157
--
167-
DROP MATERIALIZED VIEW lock_mv1;
168158
DROP VIEW lock_view7;
169159
DROP VIEW lock_view6;
170160
DROP VIEW lock_view5;
@@ -176,7 +166,6 @@ DROP TABLE lock_tbl3;
176166
DROP TABLE lock_tbl2;
177167
DROP TABLE lock_tbl1;
178168
DROP TABLE lock_tbl1a;
179-
DROP SEQUENCE lock_seq;
180169
DROP SCHEMA lock_schema1 CASCADE;
181170
DROP ROLE regress_rol_lock1;
182171
-- atomic ops tests

src/test/regress/sql/lock.sql

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ CREATE VIEW lock_view3 AS SELECT * from lock_view2;
1313
CREATE VIEW lock_view4 AS SELECT (select a from lock_tbl1a limit 1) from lock_tbl1;
1414
CREATE VIEW lock_view5 AS SELECT * from lock_tbl1 where a in (select * from lock_tbl1a);
1515
CREATE VIEW lock_view6 AS SELECT * from (select * from lock_tbl1) sub;
16-
CREATE MATERIALIZED VIEW lock_mv1 AS SELECT * FROM lock_view6;
17-
CREATE INDEX lock_mvi1 ON lock_mv1 (a);
18-
CREATE SEQUENCE lock_seq;
1916
CREATE ROLE regress_rol_lock1;
2017
ALTER ROLE regress_rol_lock1 SET search_path = lock_schema1;
2118
GRANT USAGE ON SCHEMA lock_schema1 TO regress_rol_lock1;
@@ -120,18 +117,9 @@ LOCK TABLE ONLY lock_tbl1;
120117
ROLLBACK;
121118
RESET ROLE;
122119

123-
-- Lock other relations
124-
BEGIN TRANSACTION;
125-
LOCK TABLE lock_mv1;
126-
LOCK TABLE lock_mvi1;
127-
LOCK TABLE lock_seq;
128-
ROLLBACK;
129-
130-
131120
--
132121
-- Clean up
133122
--
134-
DROP MATERIALIZED VIEW lock_mv1;
135123
DROP VIEW lock_view7;
136124
DROP VIEW lock_view6;
137125
DROP VIEW lock_view5;
@@ -142,7 +130,6 @@ DROP TABLE lock_tbl3;
142130
DROP TABLE lock_tbl2;
143131
DROP TABLE lock_tbl1;
144132
DROP TABLE lock_tbl1a;
145-
DROP SEQUENCE lock_seq;
146133
DROP SCHEMA lock_schema1 CASCADE;
147134
DROP ROLE regress_rol_lock1;
148135

0 commit comments

Comments
 (0)