Home Explore Blog CI



postgresql

2nd chunk of `doc/src/sgml/ref/lock.sgml`
da1aa5a2c2419cc0d6fa2071adcaa3fee8574cd64acfe0290000000100000f5a
 the <command>LOCK TABLE</command> statement
   before executing any <command>SELECT</command> or data modification statement.
   A <literal>REPEATABLE READ</literal> or <literal>SERIALIZABLE</literal> transaction's
   view of data will be frozen when its first
   <command>SELECT</command> or data modification statement begins.  A <command>LOCK
   TABLE</command> later in the transaction will still prevent concurrent writes
   &mdash; but it won't ensure that what the transaction reads corresponds to
   the latest committed values.
  </para>

  <para>
   If a transaction of this sort is going to change the data in the
   table, then it should use <literal>SHARE ROW EXCLUSIVE</literal> lock mode
   instead of <literal>SHARE</literal> mode.  This ensures that only one
   transaction of this type runs at a time.  Without this, a deadlock
   is possible: two transactions might both acquire <literal>SHARE</literal>
   mode, and then be unable to also acquire <literal>ROW EXCLUSIVE</literal>
   mode to actually perform their updates.  (Note that a transaction's
   own locks never conflict, so a transaction can acquire <literal>ROW
   EXCLUSIVE</literal> mode when it holds <literal>SHARE</literal> mode &mdash; but not
   if anyone else holds <literal>SHARE</literal> mode.)  To avoid deadlocks,
   make sure all transactions acquire locks on the same objects in the
   same order, and if multiple lock modes are involved for a single
   object, then transactions should always acquire the most
   restrictive mode first.
  </para>

  <para>
   More information about the lock modes and locking strategies can be
   found in <xref linkend="explicit-locking"/>.
  </para>
 </refsect1>

 <refsect1>
  <title>Parameters</title>

  <variablelist>
   <varlistentry>
    <term><replaceable class="parameter">name</replaceable></term>
    <listitem>
     <para>
      The name (optionally schema-qualified) of an existing table to
      lock. If <literal>ONLY</literal> is specified before the 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.
     </para>

     <para>
      The command <literal>LOCK TABLE a, b;</literal> is equivalent to
      <literal>LOCK TABLE a; LOCK TABLE b;</literal>. The tables are locked
      one-by-one in the order specified in the <command>LOCK
      TABLE</command> command.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><replaceable class="parameter">lockmode</replaceable></term>
    <listitem>
     <para>
      The lock mode specifies which locks this lock conflicts with.
      Lock modes are described in <xref linkend="explicit-locking"/>.
     </para>

     <para>
      If no lock mode is specified, then <literal>ACCESS
      EXCLUSIVE</literal>, the most restrictive mode, is used.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><literal>NOWAIT</literal></term>
    <listitem>
     <para>
      Specifies that <command>LOCK TABLE</command> should not wait for
      any conflicting locks to be released: if the specified lock(s)
      cannot be acquired immediately without waiting, the transaction
      is aborted.
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
 </refsect1>

 <refsect1>
  <title>Notes</title>

   <para>
    To lock a table, the user must have the right privilege for the specified
    <replaceable class="parameter">lockmode</replaceable>.
    If the user has <literal>MAINTAIN</literal>,
    <literal>UPDATE</literal>, <literal>DELETE</literal>, or
    <literal>TRUNCATE</literal> privileges on the table, any <replaceable
    class="parameter">lockmode</replaceable> is permitted. If the user has
    <literal>INSERT</literal>

Title: LOCK TABLE: Avoiding Deadlocks and Parameter Details
Summary
The text emphasizes the importance of using SHARE ROW EXCLUSIVE lock mode when a transaction intends to modify data to prevent deadlocks, and stresses that all transactions should acquire locks in the same order. It then details the parameters of the LOCK TABLE command, including the table name, lock modes (with ACCESS EXCLUSIVE being the default), and the NOWAIT option to avoid waiting for conflicting locks. It also notes that more information about lock modes and locking strategies can be found in explicit locking documentation. Finally, it is important to execute the LOCK TABLE statement before any SELECT or data modification statement with REPEATABLE READ or SERIALIZABLE.