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

Commit 7ffead2

Browse files
committed
Accept relations of any kind in LOCK TABLE
The restriction that only tables and views can be locked by LOCK TABLE is quite arbitrary, since the underlying mechanism can lock any relation type. Drop the restriction so that programs such as pg_dump can lock all relations they're interested in, preventing schema changes that could cause a dump to fail after expending much effort. Backpatch to 9.5. Author: Álvaro Herrera <alvherre@alvh.no-ip.org> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Reported-by: Wells Oliver <wells.oliver@gmail.com> Discussion: https://postgr.es/m/20201021200659.GA32358@alvherre.pgsql
1 parent 340bbe1 commit 7ffead2

File tree

4 files changed

+42
-25
lines changed

4 files changed

+42
-25
lines changed

doc/src/sgml/ref/lock.sgml

Lines changed: 9 additions & 6 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 table</refpurpose>
19+
<refpurpose>lock a named relation (table, etc)</refpurpose>
2020
</refnamediv>
2121

2222
<refsynopsisdiv>
@@ -34,7 +34,9 @@ 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, waiting
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
3840
if necessary for any conflicting locks to be released. If
3941
<literal>NOWAIT</literal> is specified, <command>LOCK
4042
TABLE</command> does not wait to acquire the desired lock: if it
@@ -115,17 +117,18 @@ LOCK [ TABLE ] [ ONLY ] <replaceable class="parameter">name</replaceable> [ * ]
115117
<term><replaceable class="parameter">name</replaceable></term>
116118
<listitem>
117119
<para>
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
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
120122
table is locked. If <literal>ONLY</literal> is not specified, the table and all
121123
its descendant tables (if any) are locked. Optionally, <literal>*</literal>
122124
can be specified after the table name to explicitly indicate that
123-
descendant tables are included.
125+
descendant tables are included. When locking a view, all relations appearing
126+
in the view definition are locked, regardless of <literal>ONLY</literal>.
124127
</para>
125128

126129
<para>
127130
The command <literal>LOCK TABLE a, b;</literal> is equivalent to
128-
<literal>LOCK TABLE a; LOCK TABLE b;</literal>. The tables are locked
131+
<literal>LOCK TABLE a; LOCK TABLE b;</literal>. The relations are locked
129132
one-by-one in the order specified in the <command>LOCK
130133
TABLE</command> command.
131134
</para>

src/backend/commands/lockcmds.c

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

97-
/* Currently, we only allow plain tables or views to be locked */
98-
if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE &&
99-
relkind != RELKIND_VIEW)
100-
ereport(ERROR,
101-
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
102-
errmsg("\"%s\" is not a table or view",
103-
rv->relname)));
104-
10597
/*
10698
* Make note if a temporary relation has been accessed in this
10799
* transaction.
@@ -208,11 +200,13 @@ LockViewRecurse_walker(Node *node, LockViewRecurse_context *context)
208200
foreach(rtable, query->rtable)
209201
{
210202
RangeTblEntry *rte = lfirst(rtable);
203+
Oid relid;
211204
AclResult aclresult;
212205

213-
Oid relid = rte->relid;
214-
char relkind = rte->relkind;
215-
char *relname = get_rel_name(relid);
206+
/* ignore all non-relation RTEs */
207+
if (rte->rtekind != RTE_RELATION)
208+
continue;
209+
relid = rte->relid;
216210

217211
/*
218212
* The OLD and NEW placeholder entries in the view's rtable are
@@ -223,11 +217,6 @@ LockViewRecurse_walker(Node *node, LockViewRecurse_context *context)
223217
strcmp(rte->eref->aliasname, "new") == 0))
224218
continue;
225219

226-
/* Currently, we only allow plain tables or views to be locked. */
227-
if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE &&
228-
relkind != RELKIND_VIEW)
229-
continue;
230-
231220
/* Check infinite recursion in the view definition. */
232221
if (list_member_oid(context->ancestor_views, relid))
233222
ereport(ERROR,
@@ -238,7 +227,8 @@ LockViewRecurse_walker(Node *node, LockViewRecurse_context *context)
238227
/* Check permissions with the view owner's privilege. */
239228
aclresult = LockTableAclCheck(relid, context->lockmode, context->viewowner);
240229
if (aclresult != ACLCHECK_OK)
241-
aclcheck_error(aclresult, get_relkind_objtype(relkind), relname);
230+
aclcheck_error(aclresult, get_relkind_objtype(rte->relkind),
231+
get_rel_name(relid));
242232

243233
/* We have enough rights to lock the relation; do so. */
244234
if (!context->nowait)
@@ -247,9 +237,9 @@ LockViewRecurse_walker(Node *node, LockViewRecurse_context *context)
247237
ereport(ERROR,
248238
(errcode(ERRCODE_LOCK_NOT_AVAILABLE),
249239
errmsg("could not obtain lock on relation \"%s\"",
250-
relname)));
240+
get_rel_name(relid))));
251241

252-
if (relkind == RELKIND_VIEW)
242+
if (rte->relkind == RELKIND_VIEW)
253243
LockViewRecurse(relid, context->lockmode, context->nowait, context->ancestor_views);
254244
else if (rte->inh)
255245
LockTableRecurse(relid, context->lockmode, context->nowait, context->viewowner);

src/test/regress/expected/lock.out

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ 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;
1518
CREATE ROLE regress_rol_lock1;
1619
ALTER ROLE regress_rol_lock1 SET search_path = lock_schema1;
1720
GRANT USAGE ON SCHEMA lock_schema1 TO regress_rol_lock1;
@@ -150,9 +153,16 @@ BEGIN;
150153
LOCK TABLE ONLY lock_tbl1;
151154
ROLLBACK;
152155
RESET ROLE;
156+
-- Lock other relations
157+
BEGIN TRANSACTION;
158+
LOCK TABLE lock_mv1;
159+
LOCK TABLE lock_mvi1;
160+
LOCK TABLE lock_seq;
161+
ROLLBACK;
153162
--
154163
-- Clean up
155164
--
165+
DROP MATERIALIZED VIEW lock_mv1;
156166
DROP VIEW lock_view7;
157167
DROP VIEW lock_view6;
158168
DROP VIEW lock_view5;
@@ -164,6 +174,7 @@ DROP TABLE lock_tbl3;
164174
DROP TABLE lock_tbl2;
165175
DROP TABLE lock_tbl1;
166176
DROP TABLE lock_tbl1a;
177+
DROP SEQUENCE lock_seq;
167178
DROP SCHEMA lock_schema1 CASCADE;
168179
DROP ROLE regress_rol_lock1;
169180
-- atomic ops tests

src/test/regress/sql/lock.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ 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;
1619
CREATE ROLE regress_rol_lock1;
1720
ALTER ROLE regress_rol_lock1 SET search_path = lock_schema1;
1821
GRANT USAGE ON SCHEMA lock_schema1 TO regress_rol_lock1;
@@ -113,9 +116,18 @@ LOCK TABLE ONLY lock_tbl1;
113116
ROLLBACK;
114117
RESET ROLE;
115118

119+
-- Lock other relations
120+
BEGIN TRANSACTION;
121+
LOCK TABLE lock_mv1;
122+
LOCK TABLE lock_mvi1;
123+
LOCK TABLE lock_seq;
124+
ROLLBACK;
125+
126+
116127
--
117128
-- Clean up
118129
--
130+
DROP MATERIALIZED VIEW lock_mv1;
119131
DROP VIEW lock_view7;
120132
DROP VIEW lock_view6;
121133
DROP VIEW lock_view5;
@@ -126,6 +138,7 @@ DROP TABLE lock_tbl3;
126138
DROP TABLE lock_tbl2;
127139
DROP TABLE lock_tbl1;
128140
DROP TABLE lock_tbl1a;
141+
DROP SEQUENCE lock_seq;
129142
DROP SCHEMA lock_schema1 CASCADE;
130143
DROP ROLE regress_rol_lock1;
131144

0 commit comments

Comments
 (0)