Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Accept relations of any kind in LOCK TABLE
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Tue, 27 Oct 2020 16:49:19 +0000 (13:49 -0300)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Tue, 27 Oct 2020 16:49:19 +0000 (13:49 -0300)
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

doc/src/sgml/ref/lock.sgml
src/backend/commands/lockcmds.c
src/test/regress/expected/lock.out
src/test/regress/sql/lock.sql

index b946eab3039e951d0eb5508f1905ed59e80bdd44..7f40177129c0d8d5aa06b28ce5b16fb2044b4dcb 100644 (file)
@@ -16,7 +16,7 @@ PostgreSQL documentation
 
  <refnamediv>
   <refname>LOCK</refname>
-  <refpurpose>lock a table</refpurpose>
+  <refpurpose>lock a named relation (table, etc)</refpurpose>
  </refnamediv>
 
  <refsynopsisdiv>
@@ -34,7 +34,9 @@ LOCK [ TABLE ] [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
   <title>Description</title>
 
   <para>
-   <command>LOCK TABLE</command> obtains a table-level lock, waiting
+   <command>LOCK TABLE</command> 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
    <literal>NOWAIT</literal> is specified, <command>LOCK
    TABLE</command> does not wait to acquire the desired lock: if it
@@ -110,17 +112,18 @@ LOCK [ TABLE ] [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
     <term><replaceable class="PARAMETER">name</replaceable></term>
     <listitem>
      <para>
-      The name (optionally schema-qualified) of an existing table to
-      lock. If <literal>ONLY</> is specified before the table name, only that
-      table is locked. If <literal>ONLY</> is not specified, the table and all
-      its descendant tables (if any) are locked.  Optionally, <literal>*</>
+      The name (optionally schema-qualified) of an existing relation to
+      lock. If <literal>ONLY</literal> is specified before a table name, only that
+      table is locked. If <literal>ONLY</literal> is not specified, the table and all
+      its descendant tables (if any) are locked.  Optionally, <literal>*</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 <literal>ONLY</literal>.
      </para>
 
      <para>
-      The command <literal>LOCK TABLE a, b;</> is equivalent to
-      <literal>LOCK TABLE a; LOCK TABLE b;</>. The tables are locked
+      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
       TABLE</command> command.
      </para>
index bb7d9e8239e350291519b0f07fb35cdd146463a7..d48a15984f399d98185a4cd4854793ef7c4f17fe 100644 (file)
@@ -88,13 +88,6 @@ RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid,
        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.
index fd273445030482573643a7d5c1b1408408a74d4e..fec1c3798334ef84e03b011bfc56661ad13a7466 100644 (file)
@@ -5,7 +5,10 @@
 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;
@@ -30,8 +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 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);
@@ -51,13 +53,21 @@ BEGIN;
 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
index 567e8bccf1b58b38b5174d0623dff9e2a41d9423..81af5d08fc8b960a1fc2f980724d3dbbdb69522e 100644 (file)
@@ -6,7 +6,10 @@
 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;
@@ -33,7 +36,7 @@ LOCK TABLE lock_tbl1 IN SHARE 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 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.
@@ -55,13 +58,23 @@ 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;