403Webshell
Server IP : 103.119.228.120  /  Your IP : 18.220.206.141
Web Server : Apache
System : Linux v8.techscape8.com 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64
User : nobody ( 99)
PHP Version : 5.6.40
Disable Function : shell_exec,symlink,system,exec,proc_get_status,proc_nice,proc_terminate,define_syslog_variables,syslog,openlog,closelog,escapeshellcmd,passthru,ocinum cols,ini_alter,leak,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dl,dll,myshellexec,proc_open,socket_bind,proc_close,escapeshellarg,parse_ini_filepopen,fpassthru,exec,passthru,escapeshellarg,escapeshellcmd,proc_close,proc_open,ini_alter,popen,show_source,proc_nice,proc_terminate,proc_get_status,proc_close,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,dl,symlink,shell_exec,system,dl,passthru,escapeshellarg,escapeshellcmd,myshellexec,c99_buff_prepare,c99_sess_put,fpassthru,getdisfunc,fx29exec,fx29exec2,is_windows,disp_freespace,fx29sh_getupdate,fx29_buff_prepare,fx29_sess_put,fx29shexit,fx29fsearch,fx29ftpbrutecheck,fx29sh_tools,fx29sh_about,milw0rm,imagez,sh_name,myshellexec,checkproxyhost,dosyayicek,c99_buff_prepare,c99_sess_put,c99getsource,c99sh_getupdate,c99fsearch,c99shexit,view_perms,posix_getpwuid,posix_getgrgid,posix_kill,parse_perms,parsesort,view_perms_color,set_encoder_input,ls_setcheckboxall,ls_reverse_all,rsg_read,rsg_glob,selfURL,dispsecinfo,unix2DosTime,addFile,system,get_users,view_size,DirFiles,DirFilesWide,DirPrintHTMLHeaders,GetFilesTotal,GetTitles,GetTimeTotal,GetMatchesCount,GetFileMatchesCount,GetResultFiles,fs_copy_dir,fs_copy_obj,fs_move_dir,fs_move_obj,fs_rmdir,SearchText,getmicrotime
MySQL : ON |  cURL : ON |  WGET : ON |  Perl : ON |  Python : ON |  Sudo : ON |  Pkexec : ON
Directory :  /usr/share/doc/postgresql-9.2.24/html/

Upload File :
current_dir [ Writeable] document_root [ Writeable]

 

Command :


[ Back ]     

Current File : /usr/share/doc/postgresql-9.2.24/html/transaction-iso.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Transaction Isolation</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.2.24 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Concurrency Control"
HREF="mvcc.html"><LINK
REL="PREVIOUS"
TITLE="Introduction"
HREF="mvcc-intro.html"><LINK
REL="NEXT"
TITLE="Explicit Locking"
HREF="explicit-locking.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-06T22:43:11"></HEAD
><BODY
CLASS="SECT1"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.2.24 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="Introduction"
HREF="mvcc-intro.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="Explicit Locking"
HREF="explicit-locking.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="TRANSACTION-ISO"
>13.2. Transaction Isolation</A
></H1
><P
>    The <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> standard defines four levels of
    transaction isolation.  The most strict is Serializable,
    which is defined by the standard in a paragraph which says that any
    concurrent execution of a set of Serializable transactions is guaranteed
    to produce the same effect as running them one at a time in some order.
    The other three levels are defined in terms of phenomena, resulting from
    interaction between concurrent transactions, which must not occur at
    each level.  The standard notes that due to the definition of
    Serializable, none of these phenomena are possible at that level.  (This
    is hardly surprising -- if the effect of the transactions must be
    consistent with having been run one at a time, how could you see any
    phenomena caused by interactions?)
   </P
><P
>    The phenomena which are prohibited at various levels are:

    <P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
>dirty read
       </DT
><DD
><P
>        A transaction reads data written by a concurrent uncommitted transaction.
       </P
></DD
><DT
>nonrepeatable read
       </DT
><DD
><P
>        A transaction re-reads data it has previously read and finds that data
        has been modified by another transaction (that committed since the
        initial read).
       </P
></DD
><DT
>phantom read
       </DT
><DD
><P
>        A transaction re-executes a query returning a set of rows that satisfy a
        search condition and finds that the set of rows satisfying the condition
        has changed due to another recently-committed transaction.
       </P
></DD
></DL
></DIV
><P>
   </P
><P
>    
    The four transaction isolation levels and the corresponding
    behaviors are described in <A
HREF="transaction-iso.html#MVCC-ISOLEVEL-TABLE"
>Table 13-1</A
>.
   </P
><DIV
CLASS="TABLE"
><A
NAME="MVCC-ISOLEVEL-TABLE"
></A
><P
><B
>Table 13-1. Standard <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> Transaction Isolation Levels</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><COL><COL><THEAD
><TR
><TH
>         Isolation Level
        </TH
><TH
>         Dirty Read
        </TH
><TH
>         Nonrepeatable Read
        </TH
><TH
>         Phantom Read
        </TH
></TR
></THEAD
><TBODY
><TR
><TD
>         Read uncommitted
        </TD
><TD
>         Possible
        </TD
><TD
>         Possible
        </TD
><TD
>         Possible
        </TD
></TR
><TR
><TD
>         Read committed
        </TD
><TD
>         Not possible
        </TD
><TD
>         Possible
        </TD
><TD
>         Possible
        </TD
></TR
><TR
><TD
>         Repeatable read
        </TD
><TD
>         Not possible
        </TD
><TD
>         Not possible
        </TD
><TD
>         Possible
        </TD
></TR
><TR
><TD
>         Serializable
        </TD
><TD
>         Not possible
        </TD
><TD
>         Not possible
        </TD
><TD
>         Not possible
        </TD
></TR
></TBODY
></TABLE
></DIV
><P
>    In <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>, you can request any of the
    four standard transaction isolation levels.  But internally, there are
    only three distinct isolation levels, which correspond to the levels Read
    Committed, Repeatable Read, and Serializable.  When you select the level Read
    Uncommitted you really get Read Committed, and phantom reads are not possible
    in the <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> implementation of Repeatable
    Read, so the actual
    isolation level might be stricter than what you select.  This is
    permitted by the SQL standard: the four isolation levels only
    define which phenomena must not happen, they do not define which
    phenomena must happen.  The reason that <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>
    only provides three isolation levels is that this is the only
    sensible way to map the standard isolation levels to the multiversion
    concurrency control architecture.  The behavior of the available
    isolation levels is detailed in the following subsections.
   </P
><P
>    To set the transaction isolation level of a transaction, use the
    command <A
HREF="sql-set-transaction.html"
>SET TRANSACTION</A
>.
   </P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="XACT-READ-COMMITTED"
>13.2.1. Read Committed Isolation Level</A
></H2
><P
>    <I
CLASS="FIRSTTERM"
>Read Committed</I
> is the default isolation
    level in <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>.  When a transaction
    uses this isolation level, a <TT
CLASS="COMMAND"
>SELECT</TT
> query
    (without a <TT
CLASS="LITERAL"
>FOR UPDATE/SHARE</TT
> clause) sees only data
    committed before the query began; it never sees either uncommitted
    data or changes committed during query execution by concurrent
    transactions.  In effect, a <TT
CLASS="COMMAND"
>SELECT</TT
> query sees
    a snapshot of the database as of the instant the query begins to
    run.   However, <TT
CLASS="COMMAND"
>SELECT</TT
> does see the effects
    of previous updates executed within its own transaction, even
    though they are not yet committed.  Also note that two successive
    <TT
CLASS="COMMAND"
>SELECT</TT
> commands can see different data, even
    though they are within a single transaction, if other transactions
    commit changes during execution of the first <TT
CLASS="COMMAND"
>SELECT</TT
>.
   </P
><P
>    <TT
CLASS="COMMAND"
>UPDATE</TT
>, <TT
CLASS="COMMAND"
>DELETE</TT
>, <TT
CLASS="COMMAND"
>SELECT
    FOR UPDATE</TT
>, and <TT
CLASS="COMMAND"
>SELECT FOR SHARE</TT
> commands
    behave the same as <TT
CLASS="COMMAND"
>SELECT</TT
>
    in terms of searching for target rows: they will only find target rows
    that were committed as of the command start time.  However, such a target
    row might have already been updated (or deleted or locked) by
    another concurrent transaction by the time it is found.  In this case, the
    would-be updater will wait for the first updating transaction to commit or
    roll back (if it is still in progress).  If the first updater rolls back,
    then its effects are negated and the second updater can proceed with
    updating the originally found row.  If the first updater commits, the
    second updater will ignore the row if the first updater deleted it,
    otherwise it will attempt to apply its operation to the updated version of
    the row.  The search condition of the command (the <TT
CLASS="LITERAL"
>WHERE</TT
> clause) is
    re-evaluated to see if the updated version of the row still matches the
    search condition.  If so, the second updater proceeds with its operation
    using the updated version of the row.  In the case of
    <TT
CLASS="COMMAND"
>SELECT FOR UPDATE</TT
> and <TT
CLASS="COMMAND"
>SELECT FOR
    SHARE</TT
>, this means it is the updated version of the row that is
    locked and returned to the client.
   </P
><P
>    Because of the above rule, it is possible for an updating command to see an
    inconsistent snapshot: it can see the effects of concurrent updating
    commands on the same rows it is trying to update, but it
    does not see effects of those commands on other rows in the database.
    This behavior makes Read Committed mode unsuitable for commands that
    involve complex search conditions; however, it is just right for simpler
    cases.  For example, consider updating bank balances with transactions
    like:

</P><PRE
CLASS="SCREEN"
>BEGIN;
UPDATE accounts SET balance = balance + 100.00 WHERE acctnum = 12345;
UPDATE accounts SET balance = balance - 100.00 WHERE acctnum = 7534;
COMMIT;</PRE
><P>

    If two such transactions concurrently try to change the balance of account
    12345, we clearly want the second transaction to start with the updated
    version of the account's row.  Because each command is affecting only a
    predetermined row, letting it see the updated version of the row does
    not create any troublesome inconsistency.
   </P
><P
>    More complex usage can produce undesirable results in Read Committed
    mode.  For example, consider a <TT
CLASS="COMMAND"
>DELETE</TT
> command
    operating on data that is being both added and removed from its
    restriction criteria by another command, e.g., assume
    <TT
CLASS="LITERAL"
>website</TT
> is a two-row table with
    <TT
CLASS="LITERAL"
>website.hits</TT
> equaling <TT
CLASS="LITERAL"
>9</TT
> and
    <TT
CLASS="LITERAL"
>10</TT
>:

</P><PRE
CLASS="SCREEN"
>BEGIN;
UPDATE website SET hits = hits + 1;
-- run from another session:  DELETE FROM website WHERE hits = 10;
COMMIT;</PRE
><P>

    The <TT
CLASS="COMMAND"
>DELETE</TT
> will have no effect even though
    there is a <TT
CLASS="LITERAL"
>website.hits = 10</TT
> row before and
    after the <TT
CLASS="COMMAND"
>UPDATE</TT
>. This occurs because the
    pre-update row value <TT
CLASS="LITERAL"
>9</TT
> is skipped, and when the
    <TT
CLASS="COMMAND"
>UPDATE</TT
> completes and <TT
CLASS="COMMAND"
>DELETE</TT
>
    obtains a lock, the new row value is no longer <TT
CLASS="LITERAL"
>10</TT
> but
    <TT
CLASS="LITERAL"
>11</TT
>, which no longer matches the criteria.
   </P
><P
>    Because Read Committed mode starts each command with a new snapshot
    that includes all transactions committed up to that instant,
    subsequent commands in the same transaction will see the effects
    of the committed concurrent transaction in any case.  The point
    at issue above is whether or not a <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>single</I
></SPAN
> command
    sees an absolutely consistent view of the database.
   </P
><P
>    The partial transaction isolation provided by Read Committed mode
    is adequate for many applications, and this mode is fast and simple
    to use;  however, it is not sufficient for all cases.  Applications
    that do complex queries and updates might require a more rigorously
    consistent view of the database than Read Committed mode provides.
   </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="XACT-REPEATABLE-READ"
>13.2.2. Repeatable Read Isolation Level</A
></H2
><P
>    The <I
CLASS="FIRSTTERM"
>Repeatable Read</I
> isolation level only sees
    data committed before the transaction began; it never sees either
    uncommitted data or changes committed during transaction execution
    by concurrent transactions.  (However, the query does see the
    effects of previous updates executed within its own transaction,
    even though they are not yet committed.)  This is a stronger
    guarantee than is required by the <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> standard
    for this isolation level, and prevents all of the phenomena described
    in <A
HREF="transaction-iso.html#MVCC-ISOLEVEL-TABLE"
>Table 13-1</A
>.  As mentioned above, this is
    specifically allowed by the standard, which only describes the
    <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>minimum</I
></SPAN
> protections each isolation level must
    provide.
   </P
><P
>    This level is different from Read Committed in that a query in a
    repeatable read transaction sees a snapshot as of the start of the
    <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>transaction</I
></SPAN
>, not as of the start
    of the current query within the transaction.  Thus, successive
    <TT
CLASS="COMMAND"
>SELECT</TT
> commands within a <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>single</I
></SPAN
>
    transaction see the same data, i.e., they do not see changes made by
    other transactions that committed after their own transaction started.
   </P
><P
>    Applications using this level must be prepared to retry transactions
    due to serialization failures.
   </P
><P
>    <TT
CLASS="COMMAND"
>UPDATE</TT
>, <TT
CLASS="COMMAND"
>DELETE</TT
>, <TT
CLASS="COMMAND"
>SELECT
    FOR UPDATE</TT
>, and <TT
CLASS="COMMAND"
>SELECT FOR SHARE</TT
> commands
    behave the same as <TT
CLASS="COMMAND"
>SELECT</TT
>
    in terms of searching for target rows: they will only find target rows
    that were committed as of the transaction start time.  However, such a
    target row might have already been updated (or deleted or locked) by
    another concurrent transaction by the time it is found.  In this case, the
    repeatable read transaction will wait for the first updating transaction to commit or
    roll back (if it is still in progress).  If the first updater rolls back,
    then its effects are negated and the repeatable read transaction can proceed
    with updating the originally found row.  But if the first updater commits
    (and actually updated or deleted the row, not just locked it)
    then the repeatable read transaction will be rolled back with the message

</P><PRE
CLASS="SCREEN"
>ERROR:  could not serialize access due to concurrent update</PRE
><P>

    because a repeatable read transaction cannot modify or lock rows changed by
    other transactions after the repeatable read transaction began.
   </P
><P
>    When an application receives this error message, it should abort
    the current transaction and retry the whole transaction from
    the beginning.  The second time through, the transaction will see the
    previously-committed change as part of its initial view of the database,
    so there is no logical conflict in using the new version of the row
    as the starting point for the new transaction's update.
   </P
><P
>    Note that only updating transactions might need to be retried; read-only
    transactions will never have serialization conflicts.
   </P
><P
>    The Repeatable Read mode provides a rigorous guarantee that each
    transaction sees a completely stable view of the database.  However,
    this view will not necessarily always be consistent with some serial
    (one at a time) execution of concurrent transactions of the same level.
    For example, even a read only transaction at this level may see a
    control record updated to show that a batch has been completed but
    <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>not</I
></SPAN
> see one of the detail records which is logically
    part of the batch because it read an earlier revision of the control
    record.  Attempts to enforce business rules by transactions running at
    this isolation level are not likely to work correctly without careful use
    of explicit locks to block conflicting transactions.
   </P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
>     Prior to <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> version 9.1, a request
     for the Serializable transaction isolation level provided exactly the
     same behavior described here.  To retain the legacy Serializable
     behavior, Repeatable Read should now be requested.
    </P
></BLOCKQUOTE
></DIV
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="XACT-SERIALIZABLE"
>13.2.3. Serializable Isolation Level</A
></H2
><P
>    The <I
CLASS="FIRSTTERM"
>Serializable</I
> isolation level provides the strictest transaction
    isolation.  This level emulates serial transaction execution,
    as if transactions had been executed one after another, serially,
    rather than concurrently.  However, like the Repeatable Read level,
    applications using this level must
    be prepared to retry transactions due to serialization failures.
    In fact, this isolation level works exactly the same as Repeatable
    Read except that it monitors for conditions which could make
    execution of a concurrent set of serializable transactions behave
    in a manner inconsistent with all possible serial (one at a time)
    executions of those transactions.  This monitoring does not
    introduce any blocking beyond that present in repeatable read, but
    there is some overhead to the monitoring, and detection of the
    conditions which could cause a
    <I
CLASS="FIRSTTERM"
>serialization anomaly</I
> will trigger a
    <I
CLASS="FIRSTTERM"
>serialization failure</I
>.
   </P
><P
>    As an example,
    consider a table <TT
CLASS="STRUCTNAME"
>mytab</TT
>, initially containing:
</P><PRE
CLASS="SCREEN"
> class | value
-------+-------
     1 |    10
     1 |    20
     2 |   100
     2 |   200</PRE
><P>
    Suppose that serializable transaction A computes:
</P><PRE
CLASS="SCREEN"
>SELECT SUM(value) FROM mytab WHERE class = 1;</PRE
><P>
    and then inserts the result (30) as the <TT
CLASS="STRUCTFIELD"
>value</TT
> in a
    new row with <TT
CLASS="STRUCTFIELD"
>class</TT
><TT
CLASS="LITERAL"
> = 2</TT
>.  Concurrently, serializable
    transaction B computes:
</P><PRE
CLASS="SCREEN"
>SELECT SUM(value) FROM mytab WHERE class = 2;</PRE
><P>
    and obtains the result 300, which it inserts in a new row with
    <TT
CLASS="STRUCTFIELD"
>class</TT
><TT
CLASS="LITERAL"
> = 1</TT
>.  Then both transactions try to commit.
    If either transaction were running at the Repeatable Read isolation level,
    both would be allowed to commit; but since there is no serial order of execution
    consistent with the result, using Serializable transactions will allow one
    transaction to commit and will roll the other back with this message:

</P><PRE
CLASS="SCREEN"
>ERROR:  could not serialize access due to read/write dependencies among transactions</PRE
><P>

    This is because if A had
    executed before B, B would have computed the sum 330, not 300, and
    similarly the other order would have resulted in a different sum
    computed by A.
   </P
><P
>    To guarantee true serializability <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>
    uses <I
CLASS="FIRSTTERM"
>predicate locking</I
>, which means that it keeps locks
    which allow it to determine when a write would have had an impact on
    the result of a previous read from a concurrent transaction, had it run
    first.  In <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> these locks do not
    cause any blocking and therefore can <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>not</I
></SPAN
> play any part in
    causing a deadlock.  They are used to identify and flag dependencies
    among concurrent Serializable transactions which in certain combinations
    can lead to serialization anomalies.  In contrast, a Read Committed or
    Repeatable Read transaction which wants to ensure data consistency may
    need to take out a lock on an entire table, which could block other
    users attempting to use that table, or it may use <TT
CLASS="LITERAL"
>SELECT FOR
    UPDATE</TT
> or <TT
CLASS="LITERAL"
>SELECT FOR SHARE</TT
> which not only
    can block other transactions but cause disk access.
   </P
><P
>    Predicate locks in <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>, like in most
    other database systems, are based on data actually accessed by a
    transaction.  These will show up in the
    <A
HREF="view-pg-locks.html"
><TT
CLASS="STRUCTNAME"
>pg_locks</TT
></A
>
    system view with a <TT
CLASS="LITERAL"
>mode</TT
> of <TT
CLASS="LITERAL"
>SIReadLock</TT
>.  The
    particular locks
    acquired during execution of a query will depend on the plan used by
    the query, and multiple finer-grained locks (e.g., tuple locks) may be
    combined into fewer coarser-grained locks (e.g., page locks) during the
    course of the transaction to prevent exhaustion of the memory used to
    track the locks.  A <TT
CLASS="LITERAL"
>READ ONLY</TT
> transaction may be able to
    release its SIRead locks before completion, if it detects that no
    conflicts can still occur which could lead to a serialization anomaly.
    In fact, <TT
CLASS="LITERAL"
>READ ONLY</TT
> transactions will often be able to
    establish that fact at startup and avoid taking any predicate locks.
    If you explicitly request a <TT
CLASS="LITERAL"
>SERIALIZABLE READ ONLY DEFERRABLE</TT
>
    transaction, it will block until it can establish this fact.  (This is
    the <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>only</I
></SPAN
> case where Serializable transactions block but
    Repeatable Read transactions don't.)  On the other hand, SIRead locks
    often need to be kept past transaction commit, until overlapping read
    write transactions complete.
   </P
><P
>    Consistent use of Serializable transactions can simplify development.
    The guarantee that any set of successfully committed concurrent
    Serializable transactions will have the same effect as if they were run
    one at a time means that if you can demonstrate that a single transaction,
    as written, will do the right thing when run by itself, you can have
    confidence that it will do the right thing in any mix of Serializable
    transactions, even without any information about what those other
    transactions might do, or it will not successfully commit.  It is
    important that an environment which uses this technique have a
    generalized way of handling serialization failures (which always return
    with a SQLSTATE value of '40001'), because it will be very hard to
    predict exactly which transactions might contribute to the read/write
    dependencies and need to be rolled back to prevent serialization
    anomalies.  The monitoring of read/write dependencies has a cost, as does
    the restart of transactions which are terminated with a serialization
    failure, but balanced against the cost and blocking involved in use of
    explicit locks and <TT
CLASS="LITERAL"
>SELECT FOR UPDATE</TT
> or <TT
CLASS="LITERAL"
>SELECT FOR
    SHARE</TT
>, Serializable transactions are the best performance choice
    for some environments.
   </P
><P
>    While <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>'s Serializable transaction isolation
    level only allows concurrent transactions to commit if it can prove there
    is a serial order of execution that would produce the same effect, it
    doesn't always prevent errors from being raised that would not occur in
    true serial execution.  In particular, it is possible to see unique
    constraint violations caused by conflicts with overlapping Serializable
    transactions even after explicitly checking that the key isn't present
    before attempting to insert it.  This can be avoided by making sure
    that <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>all</I
></SPAN
> Serializable transactions that insert potentially
    conflicting keys explicitly check if they can do so first.  For example,
    imagine an application that asks the user for a new key and then checks
    that it doesn't exist already by trying to select it first, or generates
    a new key by selecting the maximum existing key and adding one.  If some
    Serializable transactions insert new keys directly without following this
    protocol, unique constraints violations might be reported even in cases
    where they could not occur in a serial execution of the concurrent
    transactions.
   </P
><P
>    For optimal performance when relying on Serializable transactions for
    concurrency control, these issues should be considered:

    <P
></P
></P><UL
><LI
><P
>       Declare transactions as <TT
CLASS="LITERAL"
>READ ONLY</TT
> when possible.
      </P
></LI
><LI
><P
>       Control the number of active connections, using a connection pool if
       needed.  This is always an important performance consideration, but
       it can be particularly important in a busy system using Serializable
       transactions.
      </P
></LI
><LI
><P
>       Don't put more into a single transaction than needed for integrity
       purposes.
      </P
></LI
><LI
><P
>       Don't leave connections dangling <SPAN
CLASS="QUOTE"
>"idle in transaction"</SPAN
>
       longer than necessary.
      </P
></LI
><LI
><P
>       Eliminate explicit locks, <TT
CLASS="LITERAL"
>SELECT FOR UPDATE</TT
>, and
       <TT
CLASS="LITERAL"
>SELECT FOR SHARE</TT
> where no longer needed due to the
       protections automatically provided by Serializable transactions.
      </P
></LI
><LI
><P
>       When the system is forced to combine multiple page-level predicate
       locks into a single relation-level predicate lock because the predicate
       lock table is short of memory, an increase in the rate of serialization
       failures may occur.  You can avoid this by increasing
       <A
HREF="runtime-config-locks.html#GUC-MAX-PRED-LOCKS-PER-TRANSACTION"
>max_pred_locks_per_transaction</A
>.
      </P
></LI
><LI
><P
>       A sequential scan will always necessitate a relation-level predicate
       lock.  This can result in an increased rate of serialization failures.
       It may be helpful to encourage the use of index scans by reducing
       <A
HREF="runtime-config-query.html#GUC-RANDOM-PAGE-COST"
>random_page_cost</A
> and/or increasing
       <A
HREF="runtime-config-query.html#GUC-CPU-TUPLE-COST"
>cpu_tuple_cost</A
>.  Be sure to weigh any decrease
       in transaction rollbacks and restarts against any overall change in
       query execution time.
      </P
></LI
></UL
><P>
   </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="mvcc-intro.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="explicit-locking.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Introduction</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="mvcc.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Explicit Locking</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>

Youez - 2016 - github.com/yon3zu
LinuXploit