Sophie

Sophie

distrib > Mageia > 6 > armv7hl > media > core-updates > by-pkgid > a8985c53237f42e0cfdfd17da0a53df3 > files > 247

postgresql9.4-docs-9.4.15-1.mga6.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Explicit Locking</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.4.15 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Concurrency Control"
HREF="mvcc.html"><LINK
REL="PREVIOUS"
TITLE="Transaction Isolation"
HREF="transaction-iso.html"><LINK
REL="NEXT"
TITLE="Data Consistency Checks at the Application Level"
HREF="applevel-consistency.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2017-11-10T00:43:05"></HEAD
><BODY
CLASS="SECT1"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="4"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.4.15 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="Transaction Isolation"
HREF="transaction-iso.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="mvcc.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
>Chapter 13. Concurrency Control</TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="Data Consistency Checks at the Application Level"
HREF="applevel-consistency.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="EXPLICIT-LOCKING"
>13.3. Explicit Locking</A
></H1
><P
>    <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> provides various lock modes
    to control concurrent access to data in tables.  These modes can
    be used for application-controlled locking in situations where
    <ACRONYM
CLASS="ACRONYM"
>MVCC</ACRONYM
> does not give the desired behavior.  Also,
    most <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> commands automatically
    acquire locks of appropriate modes to ensure that referenced
    tables are not dropped or modified in incompatible ways while the
    command executes.  (For example, <TT
CLASS="COMMAND"
>TRUNCATE</TT
> cannot safely be
    executed concurrently with other operations on the same table, so it
    obtains an exclusive lock on the table to enforce that.)
   </P
><P
>    To examine a list of the currently outstanding locks in a database
    server, use the
    <A
HREF="view-pg-locks.html"
><TT
CLASS="STRUCTNAME"
>pg_locks</TT
></A
>
    system view. For more information on monitoring the status of the lock
    manager subsystem, refer to <A
HREF="monitoring.html"
>Chapter 27</A
>.
   </P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="LOCKING-TABLES"
>13.3.1. Table-level Locks</A
></H2
><P
>    The list below shows the available lock modes and the contexts in
    which they are used automatically by
    <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>.  You can also acquire any
    of these locks explicitly with the command <A
HREF="sql-lock.html"
>LOCK</A
>.
    Remember that all of these lock modes are table-level locks,
    even if the name contains the word
    <SPAN
CLASS="QUOTE"
>"row"</SPAN
>; the names of the lock modes are historical.
    To some extent the names reflect the typical usage of each lock
    mode &mdash; but the semantics are all the same.  The only real difference
    between one lock mode and another is the set of lock modes with
    which each conflicts (see <A
HREF="explicit-locking.html#TABLE-LOCK-COMPATIBILITY"
>Table 13-2</A
>).
    Two transactions cannot hold locks of conflicting
    modes on the same table at the same time.  (However, a transaction
    never conflicts with itself.  For example, it might acquire
    <TT
CLASS="LITERAL"
>ACCESS EXCLUSIVE</TT
> lock and later acquire
    <TT
CLASS="LITERAL"
>ACCESS SHARE</TT
> lock on the same table.)  Non-conflicting
    lock modes can be held concurrently by many transactions.  Notice in
    particular that some lock modes are self-conflicting (for example,
    an <TT
CLASS="LITERAL"
>ACCESS EXCLUSIVE</TT
> lock cannot be held by more than one
    transaction at a time) while others are not self-conflicting (for example,
    an <TT
CLASS="LITERAL"
>ACCESS SHARE</TT
> lock can be held by multiple transactions).
   </P
><P
></P
><DIV
CLASS="VARIABLELIST"
><P
><B
>Table-level Lock Modes</B
></P
><DL
><DT
><TT
CLASS="LITERAL"
>ACCESS SHARE</TT
></DT
><DD
><P
>         Conflicts with the <TT
CLASS="LITERAL"
>ACCESS EXCLUSIVE</TT
> lock
         mode only.
        </P
><P
>         The <TT
CLASS="COMMAND"
>SELECT</TT
> command acquires a lock of this mode on
         referenced tables.  In general, any query that only <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>reads</I
></SPAN
> a table
         and does not modify it will acquire this lock mode.
        </P
></DD
><DT
><TT
CLASS="LITERAL"
>ROW SHARE</TT
></DT
><DD
><P
>         Conflicts with the <TT
CLASS="LITERAL"
>EXCLUSIVE</TT
> and
         <TT
CLASS="LITERAL"
>ACCESS EXCLUSIVE</TT
> lock modes.
        </P
><P
>         The <TT
CLASS="COMMAND"
>SELECT FOR UPDATE</TT
> and
         <TT
CLASS="COMMAND"
>SELECT FOR SHARE</TT
> commands acquire a
         lock of this mode on the target table(s) (in addition to
         <TT
CLASS="LITERAL"
>ACCESS SHARE</TT
> locks on any other tables
         that are referenced but not selected
         <TT
CLASS="OPTION"
>FOR UPDATE/FOR SHARE</TT
>).
        </P
></DD
><DT
><TT
CLASS="LITERAL"
>ROW EXCLUSIVE</TT
></DT
><DD
><P
>         Conflicts with the <TT
CLASS="LITERAL"
>SHARE</TT
>, <TT
CLASS="LITERAL"
>SHARE ROW
         EXCLUSIVE</TT
>, <TT
CLASS="LITERAL"
>EXCLUSIVE</TT
>, and
         <TT
CLASS="LITERAL"
>ACCESS EXCLUSIVE</TT
> lock modes.
        </P
><P
>         The commands <TT
CLASS="COMMAND"
>UPDATE</TT
>,
         <TT
CLASS="COMMAND"
>DELETE</TT
>, and <TT
CLASS="COMMAND"
>INSERT</TT
>
         acquire this lock mode on the target table (in addition to
         <TT
CLASS="LITERAL"
>ACCESS SHARE</TT
> locks on any other referenced
         tables).  In general, this lock mode will be acquired by any
         command that <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>modifies data</I
></SPAN
> in a table.
        </P
></DD
><DT
><TT
CLASS="LITERAL"
>SHARE UPDATE EXCLUSIVE</TT
></DT
><DD
><P
>         Conflicts with the <TT
CLASS="LITERAL"
>SHARE UPDATE EXCLUSIVE</TT
>,
         <TT
CLASS="LITERAL"
>SHARE</TT
>, <TT
CLASS="LITERAL"
>SHARE ROW
         EXCLUSIVE</TT
>, <TT
CLASS="LITERAL"
>EXCLUSIVE</TT
>, and
         <TT
CLASS="LITERAL"
>ACCESS EXCLUSIVE</TT
> lock modes.
         This mode protects a table against
         concurrent schema changes and <TT
CLASS="COMMAND"
>VACUUM</TT
> runs.
        </P
><P
>         Acquired by <TT
CLASS="COMMAND"
>VACUUM</TT
> (without <TT
CLASS="OPTION"
>FULL</TT
>),
         <TT
CLASS="COMMAND"
>ANALYZE</TT
>, <TT
CLASS="COMMAND"
>CREATE INDEX CONCURRENTLY</TT
>, and
         <TT
CLASS="COMMAND"
>ALTER TABLE VALIDATE</TT
> and other
         <TT
CLASS="COMMAND"
>ALTER TABLE</TT
> variants (for full details see
         <A
HREF="sql-altertable.html"
>ALTER TABLE</A
>).
        </P
></DD
><DT
><TT
CLASS="LITERAL"
>SHARE</TT
></DT
><DD
><P
>         Conflicts with the <TT
CLASS="LITERAL"
>ROW EXCLUSIVE</TT
>,
         <TT
CLASS="LITERAL"
>SHARE UPDATE EXCLUSIVE</TT
>, <TT
CLASS="LITERAL"
>SHARE ROW
         EXCLUSIVE</TT
>, <TT
CLASS="LITERAL"
>EXCLUSIVE</TT
>, and
         <TT
CLASS="LITERAL"
>ACCESS EXCLUSIVE</TT
> lock modes.
         This mode protects a table against concurrent data changes.
        </P
><P
>         Acquired by <TT
CLASS="COMMAND"
>CREATE INDEX</TT
>
         (without <TT
CLASS="OPTION"
>CONCURRENTLY</TT
>).
        </P
></DD
><DT
><TT
CLASS="LITERAL"
>SHARE ROW EXCLUSIVE</TT
></DT
><DD
><P
>         Conflicts with the <TT
CLASS="LITERAL"
>ROW EXCLUSIVE</TT
>,
         <TT
CLASS="LITERAL"
>SHARE UPDATE EXCLUSIVE</TT
>,
         <TT
CLASS="LITERAL"
>SHARE</TT
>, <TT
CLASS="LITERAL"
>SHARE ROW
         EXCLUSIVE</TT
>, <TT
CLASS="LITERAL"
>EXCLUSIVE</TT
>, and
         <TT
CLASS="LITERAL"
>ACCESS EXCLUSIVE</TT
> lock modes.
         This mode protects a table against concurrent data changes, and
         is self-exclusive so that only one session can hold it at a time.
        </P
><P
>         This lock mode is not automatically acquired by any
         <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> command.
        </P
></DD
><DT
><TT
CLASS="LITERAL"
>EXCLUSIVE</TT
></DT
><DD
><P
>         Conflicts with the <TT
CLASS="LITERAL"
>ROW SHARE</TT
>, <TT
CLASS="LITERAL"
>ROW
         EXCLUSIVE</TT
>, <TT
CLASS="LITERAL"
>SHARE UPDATE
         EXCLUSIVE</TT
>, <TT
CLASS="LITERAL"
>SHARE</TT
>, <TT
CLASS="LITERAL"
>SHARE
         ROW EXCLUSIVE</TT
>, <TT
CLASS="LITERAL"
>EXCLUSIVE</TT
>, and
         <TT
CLASS="LITERAL"
>ACCESS EXCLUSIVE</TT
> lock modes.
         This mode allows only concurrent <TT
CLASS="LITERAL"
>ACCESS SHARE</TT
> locks,
         i.e., only reads from the table can proceed in parallel with a
         transaction holding this lock mode.
        </P
><P
>         Acquired by <TT
CLASS="COMMAND"
>REFRESH MATERIALIZED VIEW CONCURRENTLY</TT
>.
        </P
></DD
><DT
><TT
CLASS="LITERAL"
>ACCESS EXCLUSIVE</TT
></DT
><DD
><P
>         Conflicts with locks of all modes (<TT
CLASS="LITERAL"
>ACCESS
         SHARE</TT
>, <TT
CLASS="LITERAL"
>ROW SHARE</TT
>, <TT
CLASS="LITERAL"
>ROW
         EXCLUSIVE</TT
>, <TT
CLASS="LITERAL"
>SHARE UPDATE
         EXCLUSIVE</TT
>, <TT
CLASS="LITERAL"
>SHARE</TT
>, <TT
CLASS="LITERAL"
>SHARE
         ROW EXCLUSIVE</TT
>, <TT
CLASS="LITERAL"
>EXCLUSIVE</TT
>, and
         <TT
CLASS="LITERAL"
>ACCESS EXCLUSIVE</TT
>).
         This mode guarantees that the
         holder is the only transaction accessing the table in any way.
        </P
><P
>         Acquired by the <TT
CLASS="COMMAND"
>DROP TABLE</TT
>,
         <TT
CLASS="COMMAND"
>TRUNCATE</TT
>, <TT
CLASS="COMMAND"
>REINDEX</TT
>,
         <TT
CLASS="COMMAND"
>CLUSTER</TT
>, <TT
CLASS="COMMAND"
>VACUUM FULL</TT
>,
         and <TT
CLASS="COMMAND"
>REFRESH MATERIALIZED VIEW</TT
> (without
         <TT
CLASS="OPTION"
>CONCURRENTLY</TT
>)
         commands. Many forms of <TT
CLASS="COMMAND"
>ALTER TABLE</TT
> also acquire
         a lock at this level (see <A
HREF="sql-altertable.html"
>ALTER TABLE</A
>).
         This is also the default lock mode for <TT
CLASS="COMMAND"
>LOCK TABLE</TT
>
         statements that do not specify a mode explicitly.
        </P
></DD
></DL
></DIV
><DIV
CLASS="TIP"
><BLOCKQUOTE
CLASS="TIP"
><P
><B
>Tip: </B
>       Only an <TT
CLASS="LITERAL"
>ACCESS EXCLUSIVE</TT
> lock blocks a
       <TT
CLASS="COMMAND"
>SELECT</TT
> (without <TT
CLASS="OPTION"
>FOR UPDATE/SHARE</TT
>)
       statement.
      </P
></BLOCKQUOTE
></DIV
><P
>    Once acquired, a lock is normally held till end of transaction.  But if a
    lock is acquired after establishing a savepoint, the lock is released
    immediately if the savepoint is rolled back to.  This is consistent with
    the principle that <TT
CLASS="COMMAND"
>ROLLBACK</TT
> cancels all effects of the
    commands since the savepoint.  The same holds for locks acquired within a
    <SPAN
CLASS="APPLICATION"
>PL/pgSQL</SPAN
> exception block: an error escape from the block
    releases locks acquired within it.
   </P
><DIV
CLASS="TABLE"
><A
NAME="TABLE-LOCK-COMPATIBILITY"
></A
><P
><B
>Table 13-2.  Conflicting Lock Modes</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL
WIDTH="1*"
TITLE="lockst"><COL><COL><COL><COL><COL><COL><COL
WIDTH="1*"
TITLE="lockend"><THEAD
><TR
><TH
ROWSPAN="2"
>Requested Lock Mode</TH
><TH
COLSPAN="8"
>Current Lock Mode</TH
></TR
><TR
><TH
>ACCESS SHARE</TH
><TH
>ROW SHARE</TH
><TH
>ROW EXCLUSIVE</TH
><TH
>SHARE UPDATE EXCLUSIVE</TH
><TH
>SHARE</TH
><TH
>SHARE ROW EXCLUSIVE</TH
><TH
>EXCLUSIVE</TH
><TH
>ACCESS EXCLUSIVE</TH
></TR
></THEAD
><TBODY
><TR
><TD
>ACCESS SHARE</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>X</TD
></TR
><TR
><TD
>ROW SHARE</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
></TR
><TR
><TD
>ROW EXCLUSIVE</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
></TR
><TR
><TD
>SHARE UPDATE EXCLUSIVE</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
></TR
><TR
><TD
>SHARE</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
></TR
><TR
><TD
>SHARE ROW EXCLUSIVE</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
></TR
><TR
><TD
>EXCLUSIVE</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
></TR
><TR
><TD
>ACCESS EXCLUSIVE</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
></TR
></TBODY
></TABLE
></DIV
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="LOCKING-ROWS"
>13.3.2. Row-level Locks</A
></H2
><P
>     In addition to table-level locks, there are row-level locks, which
     are listed as below with the contexts in which they are used
     automatically by <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>.  See
     <A
HREF="explicit-locking.html#ROW-LOCK-COMPATIBILITY"
>Table 13-3</A
> for a complete table of
     row-level lock conflicts.  Note that a transaction can hold
     conflicting locks on the same row, even in different subtransactions;
     but other than that, two transactions can never hold conflicting locks
     on the same row.  Row-level locks do not affect data querying; they
     block only <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>writers and lockers</I
></SPAN
> to the same row.
    </P
><P
></P
><DIV
CLASS="VARIABLELIST"
><P
><B
>Row-level Lock Modes</B
></P
><DL
><DT
><TT
CLASS="LITERAL"
>FOR UPDATE</TT
></DT
><DD
><P
>         <TT
CLASS="LITERAL"
>FOR UPDATE</TT
> causes the rows retrieved by the
         <TT
CLASS="COMMAND"
>SELECT</TT
> statement to be locked as though for
         update.  This prevents them from being locked, modified or deleted by
         other transactions until the current transaction ends.  That is,
         other transactions that attempt <TT
CLASS="COMMAND"
>UPDATE</TT
>,
         <TT
CLASS="COMMAND"
>DELETE</TT
>,
         <TT
CLASS="COMMAND"
>SELECT FOR UPDATE</TT
>,
         <TT
CLASS="COMMAND"
>SELECT FOR NO KEY UPDATE</TT
>,
         <TT
CLASS="COMMAND"
>SELECT FOR SHARE</TT
> or
         <TT
CLASS="COMMAND"
>SELECT FOR KEY SHARE</TT
>
         of these rows will be blocked until the current transaction ends;
         conversely, <TT
CLASS="COMMAND"
>SELECT FOR UPDATE</TT
> will wait for a
         concurrent transaction that has run any of those commands on the
         same row,
         and will then lock and return the updated row (or no row, if the
         row was deleted).  Within a <TT
CLASS="LITERAL"
>REPEATABLE READ</TT
> or
         <TT
CLASS="LITERAL"
>SERIALIZABLE</TT
> transaction,
         however, an error will be thrown if a row to be locked has changed
         since the transaction started.  For further discussion see
         <A
HREF="applevel-consistency.html"
>Section 13.4</A
>.
        </P
><P
>         The <TT
CLASS="LITERAL"
>FOR UPDATE</TT
> lock mode
         is also acquired by any <TT
CLASS="COMMAND"
>DELETE</TT
> on a row, and also by an
         <TT
CLASS="COMMAND"
>UPDATE</TT
> that modifies the values on certain columns.  Currently,
         the set of columns considered for the <TT
CLASS="COMMAND"
>UPDATE</TT
> case are those that
         have a unique index on them that can be used in a foreign key (so partial
         indexes and expressional indexes are not considered), but this may change
         in the future.
        </P
></DD
><DT
><TT
CLASS="LITERAL"
>FOR NO KEY UPDATE</TT
></DT
><DD
><P
>         Behaves similarly to <TT
CLASS="LITERAL"
>FOR UPDATE</TT
>, except that the lock
         acquired is weaker: this lock will not block
         <TT
CLASS="LITERAL"
>SELECT FOR KEY SHARE</TT
> commands that attempt to acquire
         a lock on the same rows. This lock mode is also acquired by any
         <TT
CLASS="COMMAND"
>UPDATE</TT
> that does not acquire a <TT
CLASS="LITERAL"
>FOR UPDATE</TT
> lock.
        </P
></DD
><DT
><TT
CLASS="LITERAL"
>FOR SHARE</TT
></DT
><DD
><P
>         Behaves similarly to <TT
CLASS="LITERAL"
>FOR NO KEY UPDATE</TT
>, except that it
         acquires a shared lock rather than exclusive lock on each retrieved
         row.  A shared lock blocks other transactions from performing
         <TT
CLASS="COMMAND"
>UPDATE</TT
>, <TT
CLASS="COMMAND"
>DELETE</TT
>,
         <TT
CLASS="COMMAND"
>SELECT FOR UPDATE</TT
> or
         <TT
CLASS="COMMAND"
>SELECT FOR NO KEY UPDATE</TT
> on these rows, but it does not
         prevent them from performing <TT
CLASS="COMMAND"
>SELECT FOR SHARE</TT
> or
         <TT
CLASS="COMMAND"
>SELECT FOR KEY SHARE</TT
>.
        </P
></DD
><DT
><TT
CLASS="LITERAL"
>FOR KEY SHARE</TT
></DT
><DD
><P
>         Behaves similarly to <TT
CLASS="LITERAL"
>FOR SHARE</TT
>, except that the
         lock is weaker: <TT
CLASS="LITERAL"
>SELECT FOR UPDATE</TT
> is blocked, but not
         <TT
CLASS="LITERAL"
>SELECT FOR NO KEY UPDATE</TT
>.  A key-shared lock blocks
         other transactions from performing <TT
CLASS="COMMAND"
>DELETE</TT
> or
         any <TT
CLASS="COMMAND"
>UPDATE</TT
> that changes the key values, but not
         other <TT
CLASS="COMMAND"
>UPDATE</TT
>, and neither does it prevent
         <TT
CLASS="COMMAND"
>SELECT FOR NO KEY UPDATE</TT
>, <TT
CLASS="COMMAND"
>SELECT FOR SHARE</TT
>,
         or <TT
CLASS="COMMAND"
>SELECT FOR KEY SHARE</TT
>.
        </P
></DD
></DL
></DIV
><P
>     <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> doesn't remember any
     information about modified rows in memory, so there is no limit on
     the number of rows locked at one time.  However, locking a row
     might cause a disk write, e.g., <TT
CLASS="COMMAND"
>SELECT FOR
     UPDATE</TT
> modifies selected rows to mark them locked, and so
     will result in disk writes.
    </P
><DIV
CLASS="TABLE"
><A
NAME="ROW-LOCK-COMPATIBILITY"
></A
><P
><B
>Table 13-3. Conflicting Row-level Locks</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL
WIDTH="1*"
TITLE="lockst"><COL><COL><COL
WIDTH="1*"
TITLE="lockend"><THEAD
><TR
><TH
ROWSPAN="2"
>Requested Lock Mode</TH
><TH
COLSPAN="4"
>Current Lock Mode</TH
></TR
><TR
><TH
>FOR KEY SHARE</TH
><TH
>FOR SHARE</TH
><TH
>FOR NO KEY UPDATE</TH
><TH
>FOR UPDATE</TH
></TR
></THEAD
><TBODY
><TR
><TD
>FOR KEY SHARE</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>X</TD
></TR
><TR
><TD
>FOR SHARE</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
></TR
><TR
><TD
>FOR NO KEY UPDATE</TD
><TD
ALIGN="CENTER"
>&nbsp;</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
></TR
><TR
><TD
>FOR UPDATE</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
><TD
ALIGN="CENTER"
>X</TD
></TR
></TBODY
></TABLE
></DIV
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="LOCKING-PAGES"
>13.3.3. Page-level Locks</A
></H2
><P
>     In addition to table and row locks, page-level share/exclusive locks are
     used to control read/write access to table pages in the shared buffer
     pool.  These locks are released immediately after a row is fetched or
     updated.  Application developers normally need not be concerned with
     page-level locks, but they are mentioned here for completeness.
    </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="LOCKING-DEADLOCKS"
>13.3.4. Deadlocks</A
></H2
><P
>     The use of explicit locking can increase the likelihood of
     <I
CLASS="FIRSTTERM"
>deadlocks</I
>, wherein two (or more) transactions each
     hold locks that the other wants.  For example, if transaction 1
     acquires an exclusive lock on table A and then tries to acquire
     an exclusive lock on table B, while transaction 2 has already
     exclusive-locked table B and now wants an exclusive lock on table
     A, then neither one can proceed.
     <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> automatically detects
     deadlock situations and resolves them by aborting one of the
     transactions involved, allowing the other(s) to complete.
     (Exactly which transaction will be aborted is difficult to
     predict and should not be relied upon.)
    </P
><P
>     Note that deadlocks can also occur as the result of row-level
     locks (and thus, they can occur even if explicit locking is not
     used). Consider the case in which two concurrent
     transactions modify a table. The first transaction executes:

</P><PRE
CLASS="SCREEN"
>UPDATE accounts SET balance = balance + 100.00 WHERE acctnum = 11111;</PRE
><P>

     This acquires a row-level lock on the row with the specified
     account number. Then, the second transaction executes:

</P><PRE
CLASS="SCREEN"
>UPDATE accounts SET balance = balance + 100.00 WHERE acctnum = 22222;
UPDATE accounts SET balance = balance - 100.00 WHERE acctnum = 11111;</PRE
><P>

     The first <TT
CLASS="COMMAND"
>UPDATE</TT
> statement successfully
     acquires a row-level lock on the specified row, so it succeeds in
     updating that row. However, the second <TT
CLASS="COMMAND"
>UPDATE</TT
>
     statement finds that the row it is attempting to update has
     already been locked, so it waits for the transaction that
     acquired the lock to complete. Transaction two is now waiting on
     transaction one to complete before it continues execution. Now,
     transaction one executes:

</P><PRE
CLASS="SCREEN"
>UPDATE accounts SET balance = balance - 100.00 WHERE acctnum = 22222;</PRE
><P>

     Transaction one attempts to acquire a row-level lock on the
     specified row, but it cannot: transaction two already holds such
     a lock. So it waits for transaction two to complete. Thus,
     transaction one is blocked on transaction two, and transaction
     two is blocked on transaction one: a deadlock
     condition. <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> will detect this
     situation and abort one of the transactions.
    </P
><P
>     The best defense against deadlocks is generally to avoid them by
     being certain that all applications using a database acquire
     locks on multiple objects in a consistent order. In the example
     above, if both transactions
     had updated the rows in the same order, no deadlock would have
     occurred. One should also ensure that the first lock acquired on
     an object in a transaction is the most restrictive mode that will be
     needed for that object.  If it is not feasible to verify this in
     advance, then deadlocks can be handled on-the-fly by retrying
     transactions that abort due to deadlocks.
    </P
><P
>     So long as no deadlock situation is detected, a transaction seeking
     either a table-level or row-level lock will wait indefinitely for
     conflicting locks to be released.  This means it is a bad idea for
     applications to hold transactions open for long periods of time
     (e.g., while waiting for user input).
    </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="ADVISORY-LOCKS"
>13.3.5. Advisory Locks</A
></H2
><P
>     <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> provides a means for
     creating locks that have application-defined meanings.  These are
     called <I
CLASS="FIRSTTERM"
>advisory locks</I
>, because the system does not
     enforce their use &mdash; it is up to the application to use them
     correctly.  Advisory locks can be useful for locking strategies
     that are an awkward fit for the MVCC model.
     For example, a common use of advisory locks is to emulate pessimistic
     locking strategies typical of so-called <SPAN
CLASS="QUOTE"
>"flat file"</SPAN
> data
     management systems.
     While a flag stored in a table could be used for the same purpose,
     advisory locks are faster, avoid table bloat, and are automatically
     cleaned up by the server at the end of the session.
    </P
><P
>     There are two ways to acquire an advisory lock in
     <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>: at session level or at
     transaction level.
     Once acquired at session level, an advisory lock is held until
     explicitly released or the session ends.  Unlike standard lock requests,
     session-level advisory lock requests do not honor transaction semantics:
     a lock acquired during a transaction that is later rolled back will still
     be held following the rollback, and likewise an unlock is effective even
     if the calling transaction fails later.  A lock can be acquired multiple
     times by its owning process; for each completed lock request there must
     be a corresponding unlock request before the lock is actually released.
     Transaction-level lock requests, on the other hand, behave more like
     regular lock requests: they are automatically released at the end of the
     transaction, and there is no explicit unlock operation.  This behavior
     is often more convenient than the session-level behavior for short-term
     usage of an advisory lock.
     Session-level and transaction-level lock requests for the same advisory
     lock identifier will block each other in the expected way.
     If a session already holds a given advisory lock, additional requests by
     it will always succeed, even if other sessions are awaiting the lock; this
     statement is true regardless of whether the existing lock hold and new
     request are at session level or transaction level.
    </P
><P
>     Like all locks in
     <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>, a complete list of advisory locks
     currently held by any session can be found in the <A
HREF="view-pg-locks.html"
><TT
CLASS="STRUCTNAME"
>pg_locks</TT
></A
> system
     view.
    </P
><P
>     Both advisory locks and regular locks are stored in a shared memory
     pool whose size is defined by the configuration variables
     <A
HREF="runtime-config-locks.html#GUC-MAX-LOCKS-PER-TRANSACTION"
>max_locks_per_transaction</A
> and
     <A
HREF="runtime-config-connection.html#GUC-MAX-CONNECTIONS"
>max_connections</A
>.
     Care must be taken not to exhaust this
     memory or the server will be unable to grant any locks at all.
     This imposes an upper limit on the number of advisory locks
     grantable by the server, typically in the tens to hundreds of thousands
     depending on how the server is configured.
    </P
><P
>     In certain cases using advisory locking methods, especially in queries
     involving explicit ordering and <TT
CLASS="LITERAL"
>LIMIT</TT
> clauses, care must be
     taken to control the locks acquired because of the order in which SQL
     expressions are evaluated.  For example:
</P><PRE
CLASS="SCREEN"
>SELECT pg_advisory_lock(id) FROM foo WHERE id = 12345; -- ok
SELECT pg_advisory_lock(id) FROM foo WHERE id &gt; 12345 LIMIT 100; -- danger!
SELECT pg_advisory_lock(q.id) FROM
(
  SELECT id FROM foo WHERE id &gt; 12345 LIMIT 100
) q; -- ok</PRE
><P>
     In the above queries, the second form is dangerous because the
     <TT
CLASS="LITERAL"
>LIMIT</TT
> is not guaranteed to be applied before the locking
     function is executed.  This might cause some locks to be acquired
     that the application was not expecting, and hence would fail to release
     (until it ends the session).
     From the point of view of the application, such locks
     would be dangling, although still viewable in
     <TT
CLASS="STRUCTNAME"
>pg_locks</TT
>.
    </P
><P
>     The functions provided to manipulate advisory locks are described in
     <A
HREF="functions-admin.html#FUNCTIONS-ADVISORY-LOCKS"
>Section 9.26.9</A
>.
    </P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="transaction-iso.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="applevel-consistency.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Transaction Isolation</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="mvcc.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Data Consistency Checks at the Application Level</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>