1
0
mirror of https://github.com/postgres/postgres.git synced 2025-05-21 15:54:08 +03:00

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
This commit is contained in:
Tom Lane 2020-11-06 16:17:57 -05:00
parent 768ab4d676
commit 9e555180f2
4 changed files with 20 additions and 44 deletions

View File

@ -16,7 +16,7 @@ PostgreSQL documentation
<refnamediv> <refnamediv>
<refname>LOCK</refname> <refname>LOCK</refname>
<refpurpose>lock a named relation (table, etc)</refpurpose> <refpurpose>lock a table</refpurpose>
</refnamediv> </refnamediv>
<refsynopsisdiv> <refsynopsisdiv>
@ -34,9 +34,7 @@ LOCK [ TABLE ] [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
<title>Description</title> <title>Description</title>
<para> <para>
<command>LOCK TABLE</command> obtains a table-level lock on a <command>LOCK TABLE</command> obtains a table-level lock, waiting
relation (table, partitioned table, foreign table, view,
materialized view, index, composite type, sequence), waiting
if necessary for any conflicting locks to be released. If if necessary for any conflicting locks to be released. If
<literal>NOWAIT</literal> is specified, <command>LOCK <literal>NOWAIT</literal> is specified, <command>LOCK
TABLE</command> does not wait to acquire the desired lock: if it TABLE</command> does not wait to acquire the desired lock: if it
@ -112,23 +110,17 @@ LOCK [ TABLE ] [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
<term><replaceable class="PARAMETER">name</replaceable></term> <term><replaceable class="PARAMETER">name</replaceable></term>
<listitem> <listitem>
<para> <para>
The name (optionally schema-qualified) of an existing relation to The name (optionally schema-qualified) of an existing table to
lock. If <literal>ONLY</literal> is specified before a table name, only that lock. If <literal>ONLY</> is specified before the table name, only that
table is locked. If <literal>ONLY</literal> is not specified, the table and all table is locked. If <literal>ONLY</> is not specified, the table and all
its descendant tables (if any) are locked. Optionally, <literal>*</literal> its descendant tables (if any) are locked. Optionally, <literal>*</>
can be specified after the table name to explicitly indicate that can be specified after the table name to explicitly indicate that
descendant tables are included. descendant tables are included.
</para> </para>
<para> <para>
Locking a view locks only the view object itself, not any referenced The command <literal>LOCK TABLE a, b;</> is equivalent to
relations. (Note that this behavior is different <literal>LOCK TABLE a; LOCK TABLE b;</>. The tables are locked
in <productname>PostgreSQL</productname> v11 and later.)
</para>
<para>
The command <literal>LOCK TABLE a, b;</literal> is equivalent to
<literal>LOCK TABLE a; LOCK TABLE b;</literal>. The relations are locked
one-by-one in the order specified in the <command>LOCK one-by-one in the order specified in the <command>LOCK
TABLE</command> command. TABLE</command> command.
</para> </para>

View File

@ -87,6 +87,13 @@ RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid,
return; /* woops, concurrently dropped; no permissions return; /* woops, concurrently dropped; no permissions
* check */ * check */
/* Currently, we only allow plain tables to be locked */
if (relkind != RELKIND_RELATION)
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("\"%s\" is not a table",
rv->relname)));
/* Check permissions. */ /* Check permissions. */
aclresult = LockTableAclCheck(relid, lockmode); aclresult = LockTableAclCheck(relid, lockmode);
if (aclresult != ACLCHECK_OK) if (aclresult != ACLCHECK_OK)

View File

@ -5,10 +5,7 @@
CREATE SCHEMA lock_schema1; CREATE SCHEMA lock_schema1;
SET search_path = lock_schema1; SET search_path = lock_schema1;
CREATE TABLE lock_tbl1 (a BIGINT); CREATE TABLE lock_tbl1 (a BIGINT);
CREATE VIEW lock_view1 AS SELECT 1 AS a; CREATE VIEW lock_view1 AS SELECT 1;
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; CREATE ROLE regress_rol_lock1;
ALTER ROLE regress_rol_lock1 SET search_path = lock_schema1; ALTER ROLE regress_rol_lock1 SET search_path = lock_schema1;
GRANT USAGE ON SCHEMA lock_schema1 TO regress_rol_lock1; GRANT USAGE ON SCHEMA lock_schema1 TO regress_rol_lock1;
@ -33,7 +30,8 @@ LOCK TABLE lock_tbl1 IN SHARE MODE NOWAIT;
LOCK TABLE lock_tbl1 IN SHARE ROW EXCLUSIVE MODE NOWAIT; LOCK TABLE lock_tbl1 IN SHARE ROW EXCLUSIVE MODE NOWAIT;
LOCK TABLE lock_tbl1 IN EXCLUSIVE MODE NOWAIT; LOCK TABLE lock_tbl1 IN EXCLUSIVE MODE NOWAIT;
LOCK TABLE lock_tbl1 IN ACCESS EXCLUSIVE MODE NOWAIT; LOCK TABLE lock_tbl1 IN ACCESS EXCLUSIVE MODE NOWAIT;
LOCK TABLE lock_view1 IN EXCLUSIVE MODE; LOCK TABLE lock_view1 IN EXCLUSIVE MODE; -- Will fail; can't lock a non-table
ERROR: "lock_view1" is not a table
ROLLBACK; ROLLBACK;
-- Verify that we can lock a table with inheritance children. -- Verify that we can lock a table with inheritance children.
CREATE TABLE lock_tbl2 (b BIGINT) INHERITS (lock_tbl1); CREATE TABLE lock_tbl2 (b BIGINT) INHERITS (lock_tbl1);
@ -53,21 +51,13 @@ BEGIN;
LOCK TABLE ONLY lock_tbl1; LOCK TABLE ONLY lock_tbl1;
ROLLBACK; ROLLBACK;
RESET ROLE; RESET ROLE;
-- Lock other relations
BEGIN TRANSACTION;
LOCK TABLE lock_mv1;
LOCK TABLE lock_mvi1;
LOCK TABLE lock_seq;
ROLLBACK;
-- --
-- Clean up -- Clean up
-- --
DROP MATERIALIZED VIEW lock_mv1;
DROP VIEW lock_view1; DROP VIEW lock_view1;
DROP TABLE lock_tbl3; DROP TABLE lock_tbl3;
DROP TABLE lock_tbl2; DROP TABLE lock_tbl2;
DROP TABLE lock_tbl1; DROP TABLE lock_tbl1;
DROP SEQUENCE lock_seq;
DROP SCHEMA lock_schema1 CASCADE; DROP SCHEMA lock_schema1 CASCADE;
DROP ROLE regress_rol_lock1; DROP ROLE regress_rol_lock1;
-- atomic ops tests -- atomic ops tests

View File

@ -6,10 +6,7 @@
CREATE SCHEMA lock_schema1; CREATE SCHEMA lock_schema1;
SET search_path = lock_schema1; SET search_path = lock_schema1;
CREATE TABLE lock_tbl1 (a BIGINT); CREATE TABLE lock_tbl1 (a BIGINT);
CREATE VIEW lock_view1 AS SELECT 1 AS a; CREATE VIEW lock_view1 AS SELECT 1;
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; CREATE ROLE regress_rol_lock1;
ALTER ROLE regress_rol_lock1 SET search_path = lock_schema1; ALTER ROLE regress_rol_lock1 SET search_path = lock_schema1;
GRANT USAGE ON SCHEMA lock_schema1 TO regress_rol_lock1; GRANT USAGE ON SCHEMA lock_schema1 TO regress_rol_lock1;
@ -36,7 +33,7 @@ LOCK TABLE lock_tbl1 IN SHARE MODE NOWAIT;
LOCK TABLE lock_tbl1 IN SHARE ROW EXCLUSIVE MODE NOWAIT; LOCK TABLE lock_tbl1 IN SHARE ROW EXCLUSIVE MODE NOWAIT;
LOCK TABLE lock_tbl1 IN EXCLUSIVE MODE NOWAIT; LOCK TABLE lock_tbl1 IN EXCLUSIVE MODE NOWAIT;
LOCK TABLE lock_tbl1 IN ACCESS EXCLUSIVE MODE NOWAIT; LOCK TABLE lock_tbl1 IN ACCESS EXCLUSIVE MODE NOWAIT;
LOCK TABLE lock_view1 IN EXCLUSIVE MODE; LOCK TABLE lock_view1 IN EXCLUSIVE MODE; -- Will fail; can't lock a non-table
ROLLBACK; ROLLBACK;
-- Verify that we can lock a table with inheritance children. -- Verify that we can lock a table with inheritance children.
@ -58,23 +55,13 @@ LOCK TABLE ONLY lock_tbl1;
ROLLBACK; ROLLBACK;
RESET ROLE; RESET ROLE;
-- Lock other relations
BEGIN TRANSACTION;
LOCK TABLE lock_mv1;
LOCK TABLE lock_mvi1;
LOCK TABLE lock_seq;
ROLLBACK;
-- --
-- Clean up -- Clean up
-- --
DROP MATERIALIZED VIEW lock_mv1;
DROP VIEW lock_view1; DROP VIEW lock_view1;
DROP TABLE lock_tbl3; DROP TABLE lock_tbl3;
DROP TABLE lock_tbl2; DROP TABLE lock_tbl2;
DROP TABLE lock_tbl1; DROP TABLE lock_tbl1;
DROP SEQUENCE lock_seq;
DROP SCHEMA lock_schema1 CASCADE; DROP SCHEMA lock_schema1 CASCADE;
DROP ROLE regress_rol_lock1; DROP ROLE regress_rol_lock1;