]> granicus.if.org Git - postgresql/commitdiff
Implement sharable row-level locks, and use them for foreign key references
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 28 Apr 2005 21:47:18 +0000 (21:47 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 28 Apr 2005 21:47:18 +0000 (21:47 +0000)
to eliminate unnecessary deadlocks.  This commit adds SELECT ... FOR SHARE
paralleling SELECT ... FOR UPDATE.  The implementation uses a new SLRU
data structure (managed much like pg_subtrans) to represent multiple-
transaction-ID sets.  When more than one transaction is holding a shared
lock on a particular row, we create a MultiXactId representing that set
of transactions and store its ID in the row's XMAX.  This scheme allows
an effectively unlimited number of row locks, just as we did before,
while not costing any extra overhead except when a shared lock actually
has to be shared.   Still TODO: use the regular lock manager to control
the grant order when multiple backends are waiting for a row lock.

Alvaro Herrera and Tom Lane.

55 files changed:
doc/src/sgml/mvcc.sgml
doc/src/sgml/ref/grant.sgml
doc/src/sgml/ref/lock.sgml
doc/src/sgml/ref/pg_resetxlog.sgml
doc/src/sgml/ref/select.sgml
doc/src/sgml/ref/select_into.sgml
doc/src/sgml/sql.sgml
doc/src/sgml/storage.sgml
src/backend/access/heap/heapam.c
src/backend/access/transam/Makefile
src/backend/access/transam/multixact.c [new file with mode: 0644]
src/backend/access/transam/xact.c
src/backend/access/transam/xlog.c
src/backend/catalog/index.c
src/backend/commands/portalcmds.c
src/backend/commands/trigger.c
src/backend/commands/vacuum.c
src/backend/executor/README
src/backend/executor/execMain.c
src/backend/executor/execUtils.c
src/backend/nodes/copyfuncs.c
src/backend/nodes/equalfuncs.c
src/backend/nodes/outfuncs.c
src/backend/nodes/readfuncs.c
src/backend/optimizer/path/allpaths.c
src/backend/optimizer/plan/initsplan.c
src/backend/optimizer/plan/planner.c
src/backend/optimizer/prep/prepjointree.c
src/backend/optimizer/prep/preptlist.c
src/backend/parser/analyze.c
src/backend/parser/gram.y
src/backend/parser/parse_relation.c
src/backend/parser/parse_type.c
src/backend/rewrite/rewriteHandler.c
src/backend/storage/ipc/ipci.c
src/backend/storage/lmgr/lwlock.c
src/backend/tcop/utility.c
src/backend/utils/adt/ri_triggers.c
src/backend/utils/time/tqual.c
src/bin/initdb/initdb.c
src/bin/pg_controldata/pg_controldata.c
src/bin/pg_resetxlog/pg_resetxlog.c
src/include/access/heapam.h
src/include/access/htup.h
src/include/access/multixact.h [new file with mode: 0644]
src/include/access/xlog.h
src/include/c.h
src/include/catalog/pg_control.h
src/include/nodes/execnodes.h
src/include/nodes/parsenodes.h
src/include/parser/analyze.h
src/include/parser/parse_node.h
src/include/storage/bufpage.h
src/include/storage/lmgr.h
src/include/storage/lwlock.h

index 462fa99cb959489e584a349d0a6eb8fa43165815..c88974a8d7c1cc124eb2da1b6a77f6731ea72efb 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/mvcc.sgml,v 2.49 2005/03/24 00:03:18 neilc Exp $
+$PostgreSQL: pgsql/doc/src/sgml/mvcc.sgml,v 2.50 2005/04/28 21:47:09 tgl Exp $
 -->
 
  <chapter id="mvcc">
@@ -253,11 +253,12 @@ $PostgreSQL: pgsql/doc/src/sgml/mvcc.sgml,v 2.49 2005/03/24 00:03:18 neilc Exp $
    </para>
 
    <para>
-    <command>UPDATE</command>, <command>DELETE</command>, and <command>SELECT
-    FOR UPDATE</command> commands behave the same as <command>SELECT</command>
+    <command>UPDATE</command>, <command>DELETE</command>, <command>SELECT
+    FOR UPDATE</command>, and <command>SELECT FOR SHARE</command> commands
+    behave the same as <command>SELECT</command>
     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 may have already been updated (or deleted or marked for update) by
+    row may 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,
@@ -268,7 +269,10 @@ $PostgreSQL: pgsql/doc/src/sgml/mvcc.sgml,v 2.49 2005/03/24 00:03:18 neilc Exp $
     the row.  The search condition of the command (the <literal>WHERE</> 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,
-    starting from the updated version of the row.
+    starting from the updated version of the row.  (In the case of
+    <command>SELECT FOR UPDATE</command> and <command>SELECT FOR
+    SHARE</command>, that means it is the updated version of the row that is
+    locked and returned to the client.)
    </para>
 
    <para>
@@ -346,25 +350,26 @@ COMMIT;
    </para>
 
    <para>
-    <command>UPDATE</command>, <command>DELETE</command>, and <command>SELECT
-    FOR UPDATE</command> commands behave the same as <command>SELECT</command>
+    <command>UPDATE</command>, <command>DELETE</command>, <command>SELECT
+    FOR UPDATE</command>, and <command>SELECT FOR SHARE</command> commands
+    behave the same as <command>SELECT</command>
     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 may have already been updated (or deleted or marked for update) by
+    row may have already been updated (or deleted or locked) by
     another concurrent transaction by the time it is found.  In this case, the
     serializable 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 serializable transaction can proceed
     with updating the originally found row.  But if the first updater commits
-    (and actually updated or deleted the row, not just selected it for update)
+    (and actually updated or deleted the row, not just locked it)
     then the serializable transaction will be rolled back with the message
 
 <screen>
 ERROR:  could not serialize access due to concurrent update
 </screen>
 
-    because a serializable transaction cannot modify rows changed by
+    because a serializable transaction cannot modify or lock rows changed by
     other transactions after the serializable transaction began.
    </para>
 
@@ -571,10 +576,12 @@ SELECT SUM(value) FROM mytab WHERE class = 2;
        </para>
 
        <para>
-        The <command>SELECT FOR UPDATE</command> command acquires a
+        The <command>SELECT FOR UPDATE</command> and
+        <command>SELECT FOR SHARE</command> commands acquire a
         lock of this mode on the target table(s) (in addition to
         <literal>ACCESS SHARE</literal> locks on any other tables
-        that are referenced but not selected <option>FOR UPDATE</option>).
+        that are referenced but not selected
+        <option>FOR UPDATE/FOR SHARE</option>).
        </para>
        </listitem>
       </varlistentry>
@@ -714,7 +721,7 @@ SELECT SUM(value) FROM mytab WHERE class = 2;
      <tip>
       <para>
        Only an <literal>ACCESS EXCLUSIVE</literal> lock blocks a
-       <command>SELECT</command> (without <option>FOR UPDATE</option>)
+       <command>SELECT</command> (without <option>FOR UPDATE/SHARE</option>)
        statement.
       </para>
      </tip>
@@ -725,25 +732,37 @@ SELECT SUM(value) FROM mytab WHERE class = 2;
     <title>Row-Level Locks</title>
 
     <para>
-     In addition to table-level locks, there are row-level locks.
-     A row-level lock on a specific row is automatically acquired when the
-     row is updated (or deleted or marked for update).  The lock is held
-     until the transaction commits or rolls back.
-     Row-level locks do not affect data
-     querying; they block <emphasis>writers to the same row</emphasis>
-     only.  To acquire a row-level lock on a row without actually
+     In addition to table-level locks, there are row-level locks, which
+     can be exclusive or shared locks.  An exclusive row-level lock on a
+     specific row is automatically acquired when the row is updated or
+     deleted.  The lock is held until the transaction commits or rolls
+     back.  Row-level locks do not affect data querying; they block
+     <emphasis>writers to the same row</emphasis> only.
+    </para>
+
+    <para>
+     To acquire an exclusive row-level lock on a row without actually
      modifying the row, select the row with <command>SELECT FOR
-     UPDATE</command>.  Note that once a particular row-level lock is
-     acquired, the transaction may update the row multiple times without
+     UPDATE</command>.  Note that once the row-level lock is acquired,
+     the transaction may update the row multiple times without
      fear of conflicts.
     </para>
 
+    <para>
+     To acquire a shared row-level lock on a row, select the row with
+     <command>SELECT FOR SHARE</command>.  A shared lock does not prevent
+     other transactions from acquiring the same shared lock.  However,
+     no transaction is allowed to update, delete, or exclusively lock a
+     row on which any other transaction holds a shared lock.  Any attempt
+     to do so will block until the shared locks have been released.
+    </para>
+
     <para>
      <productname>PostgreSQL</productname> doesn't remember any
      information about modified rows in memory, so it has no limit to
      the number of rows locked at one time.  However, locking a row
      may cause a disk write; thus, for example, <command>SELECT FOR
-     UPDATE</command> will modify selected rows to mark them and so
+     UPDATE</command> will modify selected rows to mark them locked, and so
      will result in disk writes.
     </para>
 
@@ -873,9 +892,10 @@ UPDATE accounts SET balance = balance - 100.00 WHERE acctnum = 22222;
 
    <para>
     To ensure the current validity of a row and protect it against
-    concurrent updates one must use <command>SELECT FOR
-    UPDATE</command> or an appropriate <command>LOCK TABLE</command>
-    statement.  (<command>SELECT FOR UPDATE</command> locks just the
+    concurrent updates one must use <command>SELECT FOR UPDATE</command>,
+    <command>SELECT FOR SHARE</command>, or an appropriate <command>LOCK
+    TABLE</command> statement.  (<command>SELECT FOR UPDATE</command>
+    or <command>SELECT FOR SHARE</command> locks just the
     returned rows against concurrent updates, while <command>LOCK
     TABLE</command> locks the whole table.)  This should be taken into
     account when porting applications to
index 5e9adaa805103907f26b2c6beb31a4dfc28eae11..45f384f48684c9830ef46efefa6473901591a1fa 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/ref/grant.sgml,v 1.45 2005/01/22 23:22:18 momjian Exp $
+$PostgreSQL: pgsql/doc/src/sgml/ref/grant.sgml,v 1.46 2005/04/28 21:47:09 tgl Exp $
 PostgreSQL documentation
 -->
 
@@ -131,9 +131,10 @@ GRANT { CREATE | ALL [ PRIVILEGES ] }
      <term>UPDATE</term>
      <listitem>
       <para>
-       Allows <xref linkend="sql-update" endterm="sql-update-title"> of any column of the
-       specified table.  <literal>SELECT ... FOR UPDATE</literal>
-       also requires this privilege (besides the
+       Allows <xref linkend="sql-update" endterm="sql-update-title"> of any
+       column of the specified table.  <literal>SELECT ... FOR UPDATE</literal>
+       and <literal>SELECT ... FOR SHARE</literal>
+       also require this privilege (besides the
        <literal>SELECT</literal> privilege).  For sequences, this
        privilege allows the use of the <function>nextval</function> and
        <function>setval</function> functions.
index 3c762ac48b555a4a2a8f988f2802025d6a5dc187..9dfa85b180794d44bbd0003a802a59d74eb9fbb4 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/ref/lock.sgml,v 1.46 2005/01/22 23:22:19 momjian Exp $
+$PostgreSQL: pgsql/doc/src/sgml/ref/lock.sgml,v 1.47 2005/04/28 21:47:10 tgl Exp $
 PostgreSQL documentation
 -->
 
@@ -177,8 +177,8 @@ where <replaceable class="PARAMETER">lockmode</replaceable> is one of:
    <command>LOCK TABLE</> is concerned, differing only in the rules
    about which modes conflict with which. For information on how to
    acquire an actual row-level lock, see <xref linkend="locking-rows">
-   and the <xref linkend="sql-for-update"
-   endterm="sql-for-update-title"> in the <command>SELECT</command>
+   and the <xref linkend="sql-for-update-share"
+   endterm="sql-for-update-share-title"> in the <command>SELECT</command>
    reference documentation.
   </para>
  </refsect1>
index 6651a0b588cf28a30196be3bb7f5119f8d6ba855..f5915adacd43820209dbf5d171055d8ffca61933 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/ref/pg_resetxlog.sgml,v 1.9 2004/12/20 01:42:09 tgl Exp $
+$PostgreSQL: pgsql/doc/src/sgml/ref/pg_resetxlog.sgml,v 1.10 2005/04/28 21:47:10 tgl Exp $
 PostgreSQL documentation
 -->
 
@@ -22,6 +22,7 @@ PostgreSQL documentation
    <arg> -n </arg>
    <arg> -o <replaceable class="parameter">oid</replaceable> </arg>
    <arg> -x <replaceable class="parameter">xid</replaceable> </arg>
+   <arg> -m <replaceable class="parameter">mxid</replaceable> </arg>
    <arg> -l <replaceable class="parameter">timelineid</replaceable>,<replaceable class="parameter">fileid</replaceable>,<replaceable class="parameter">seg</replaceable> </arg>
    <arg choice="plain"><replaceable>datadir</replaceable></arg>
   </cmdsynopsis>
@@ -73,34 +74,65 @@ PostgreSQL documentation
   </para>
 
   <para>
-   The <literal>-o</>, <literal>-x</>, and <literal>-l</> switches allow
-   the next OID, next transaction ID, and WAL starting address values to
+   The <literal>-o</>, <literal>-x</>, <literal>-m</>, and <literal>-l</>
+   switches allow the next OID, next transaction ID, next multi-transaction
+   ID, and WAL starting address values to
    be set manually.  These are only needed when
    <command>pg_resetxlog</command> is unable to determine appropriate values
-   by reading <filename>pg_control</>.  A safe value for the
-   next transaction ID may be determined by looking for the numerically largest
-   file name in the directory <filename>pg_clog</> under the data directory,
-   adding one,
-   and then multiplying by 1048576.  Note that the file names are in
-   hexadecimal.  It is usually easiest to specify the switch value in
-   hexadecimal too. For example, if <filename>0011</> is the largest entry
-   in <filename>pg_clog</>, <literal>-x 0x1200000</> will work (five trailing
-   zeroes provide the proper multiplier).
-   The WAL starting address should be
-   larger than any file name currently existing in
-   the directory <filename>pg_xlog</> under the data directory.
-   These names are also in hexadecimal and have three parts.  The first
-   part is the <quote>timeline ID</> and should usually be kept the same.
-   Do not choose a value larger than 255 (<literal>0xFF</>) for the third
-   part; instead increment the second part and reset the third part to 0.
-   For example, if <filename>00000001000000320000004A</> is the
-   largest entry in <filename>pg_xlog</>, <literal>-l 0x1,0x32,0x4B</> will
-   work; but if the largest entry is
-   <filename>000000010000003A000000FF</>, choose <literal>-l 0x1,0x3B,0x0</>
-   or more.
-   There is no comparably easy way to determine a next OID that's beyond
-   the largest one in the database, but fortunately it is not critical to
-   get the next-OID setting right.
+   by reading <filename>pg_control</>.  Safe values may be determined as
+   follows:
+
+   <itemizedlist>
+    <listitem>
+     <para>
+      A safe value for the next transaction ID (<literal>-x</>)
+      may be determined by looking for the numerically largest
+      file name in the directory <filename>pg_clog</> under the data directory,
+      adding one,
+      and then multiplying by 1048576.  Note that the file names are in
+      hexadecimal.  It is usually easiest to specify the switch value in
+      hexadecimal too. For example, if <filename>0011</> is the largest entry
+      in <filename>pg_clog</>, <literal>-x 0x1200000</> will work (five
+      trailing zeroes provide the proper multiplier).
+     </para>
+    </listitem>
+
+    <listitem>
+     <para>
+      A safe value for the next multi-transaction ID (<literal>-m</>)
+      may be determined by looking for the numerically largest
+      file name in the directory <filename>pg_multixact/offsets</> under the
+      data directory, adding one, and then multiplying by 65536.  As above,
+      the file names are in hexadecimal, so the easiest way to do this is to
+      specify the switch value in hexadecimal and add four zeroes.
+     </para>
+    </listitem>
+
+    <listitem>
+     <para>
+      The WAL starting address (<literal>-l</>) should be
+      larger than any file name currently existing in
+      the directory <filename>pg_xlog</> under the data directory.
+      These names are also in hexadecimal and have three parts.  The first
+      part is the <quote>timeline ID</> and should usually be kept the same.
+      Do not choose a value larger than 255 (<literal>0xFF</>) for the third
+      part; instead increment the second part and reset the third part to 0.
+      For example, if <filename>00000001000000320000004A</> is the
+      largest entry in <filename>pg_xlog</>, <literal>-l 0x1,0x32,0x4B</> will
+      work; but if the largest entry is
+      <filename>000000010000003A000000FF</>, choose <literal>-l 0x1,0x3B,0x0</>
+      or more.
+     </para>
+    </listitem>
+
+    <listitem>
+     <para>
+      There is no comparably easy way to determine a next OID that's beyond
+      the largest one in the database, but fortunately it is not critical to
+      get the next-OID setting right.
+     </para>
+    </listitem>
+   </itemizedlist>
   </para>
 
   <para>
index 78e591acd7259855e239a134f8ced79b58d609e3..9b8b90bb167ac2746c2eafe16ea812fe0dfca117 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/ref/select.sgml,v 1.85 2005/04/22 15:32:58 momjian Exp $
+$PostgreSQL: pgsql/doc/src/sgml/ref/select.sgml,v 1.86 2005/04/28 21:47:10 tgl Exp $
 PostgreSQL documentation
 -->
 
@@ -30,7 +30,7 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="parameter">expression</replac
     [ ORDER BY <replaceable class="parameter">expression</replaceable> [ ASC | DESC | USING <replaceable class="parameter">operator</replaceable> ] [, ...] ]
     [ LIMIT { <replaceable class="parameter">count</replaceable> | ALL } ]
     [ OFFSET <replaceable class="parameter">start</replaceable> ]
-    [ FOR UPDATE [ OF <replaceable class="parameter">table_name</replaceable> [, ...] ] ]
+    [ FOR { UPDATE | SHARE } [ OF <replaceable class="parameter">table_name</replaceable> [, ...] ] ]
 
 where <replaceable class="parameter">from_item</replaceable> can be one of:
 
@@ -142,10 +142,11 @@ where <replaceable class="parameter">from_item</replaceable> can be one of:
 
     <listitem>
      <para>
-      The <literal>FOR UPDATE</literal> clause causes the
-      <command>SELECT</command> statement to lock the selected rows
-      against concurrent updates.  (See <xref linkend="sql-for-update"
-      endterm="sql-for-update-title"> below.)
+      If the <literal>FOR UPDATE</literal> or <literal>FOR SHARE</literal>
+      clause is specified, the
+      <command>SELECT</command> statement locks the selected rows
+      against concurrent updates.  (See <xref linkend="sql-for-update-share"
+      endterm="sql-for-update-share-title"> below.)
      </para>
     </listitem>
    </orderedlist>
@@ -153,7 +154,8 @@ where <replaceable class="parameter">from_item</replaceable> can be one of:
    
   <para>
    You must have <literal>SELECT</literal> privilege on a table to
-   read its values.  The use of <literal>FOR UPDATE</literal> requires
+   read its values.  The use of <literal>FOR UPDATE</literal> or
+   <literal>FOR SHARE</literal> requires
    <literal>UPDATE</literal> privilege as well.
   </para>
  </refsect1>
@@ -503,7 +505,8 @@ HAVING <replaceable class="parameter">condition</replaceable>
 </synopsis>
     <replaceable class="parameter">select_statement</replaceable> is
     any <command>SELECT</command> statement without an <literal>ORDER
-    BY</>, <literal>LIMIT</>, or <literal>FOR UPDATE</literal> clause.
+    BY</>, <literal>LIMIT</>, <literal>FOR UPDATE</literal>, or
+    <literal>FOR SHARE</literal> clause. 
     (<literal>ORDER BY</> and <literal>LIMIT</> can be attached to a
     subexpression if it is enclosed in parentheses.  Without
     parentheses, these clauses will be taken to apply to the result of
@@ -537,8 +540,9 @@ HAVING <replaceable class="parameter">condition</replaceable>
    </para>
     
    <para>
-    Currently, <literal>FOR UPDATE</> may not be specified either for
-    a <literal>UNION</> result or for any input of a <literal>UNION</>.
+    Currently, <literal>FOR UPDATE</> and <literal>FOR SHARE</> may not be
+    specified either for a <literal>UNION</> result or for any input of a
+    <literal>UNION</>.
    </para>
   </refsect2>
 
@@ -552,7 +556,8 @@ HAVING <replaceable class="parameter">condition</replaceable>
 </synopsis>
     <replaceable class="parameter">select_statement</replaceable> is
     any <command>SELECT</command> statement without an <literal>ORDER
-    BY</>, <literal>LIMIT</>, or <literal>FOR UPDATE</literal> clause.
+    BY</>, <literal>LIMIT</>, <literal>FOR UPDATE</literal>, or
+    <literal>FOR SHARE</literal> clause.
    </para>
 
    <para>
@@ -581,8 +586,9 @@ HAVING <replaceable class="parameter">condition</replaceable>
    </para>
     
    <para>
-    Currently, <literal>FOR UPDATE</> may not be specified either for
-    an <literal>INTERSECT</> result or for any input of an <literal>INTERSECT</>.
+    Currently, <literal>FOR UPDATE</> and <literal>FOR SHARE</> may not be
+    specified either for an <literal>INTERSECT</> result or for any input of
+    an <literal>INTERSECT</>.
    </para>
   </refsect2>
 
@@ -596,7 +602,8 @@ HAVING <replaceable class="parameter">condition</replaceable>
 </synopsis>
     <replaceable class="parameter">select_statement</replaceable> is
     any <command>SELECT</command> statement without an <literal>ORDER
-    BY</>, <literal>LIMIT</>, or <literal>FOR UPDATE</literal> clause.
+    BY</>, <literal>LIMIT</>, <literal>FOR UPDATE</literal>, or
+    <literal>FOR SHARE</literal> clause.
    </para>
 
    <para>
@@ -621,8 +628,9 @@ HAVING <replaceable class="parameter">condition</replaceable>
    </para>
     
    <para>
-    Currently, <literal>FOR UPDATE</> may not be specified either for
-    an <literal>EXCEPT</> result or for any input of an <literal>EXCEPT</>.
+    Currently, <literal>FOR UPDATE</> and <literal>FOR SHARE</> may not be
+    specified either for an <literal>EXCEPT</> result or for any input of
+    an <literal>EXCEPT</>.
    </para>
   </refsect2>
 
@@ -789,8 +797,8 @@ OFFSET <replaceable class="parameter">start</replaceable>
    </para>
   </refsect2>
 
-  <refsect2 id="SQL-FOR-UPDATE">
-   <title id="sql-for-update-title"><literal>FOR UPDATE</literal> Clause</title>
+  <refsect2 id="SQL-FOR-UPDATE-SHARE">
+   <title id="sql-for-update-share-title"><literal>FOR UPDATE</literal>/<literal>FOR SHARE</literal> Clause</title>
 
    <para>
     The <literal>FOR UPDATE</literal> clause has this form:
@@ -799,6 +807,13 @@ FOR UPDATE [ OF <replaceable class="parameter">table_name</replaceable> [, ...]
 </synopsis>
    </para>
 
+   <para>
+    The closely related <literal>FOR SHARE</literal> clause has this form:
+<synopsis>
+FOR SHARE [ OF <replaceable class="parameter">table_name</replaceable> [, ...] ]
+</synopsis>
+   </para>
+
    <para>
     <literal>FOR UPDATE</literal> causes the rows retrieved by the
     <command>SELECT</command> statement to be locked as though for
@@ -817,26 +832,44 @@ FOR UPDATE [ OF <replaceable class="parameter">table_name</replaceable> [, ...]
    </para>
 
    <para>
-    If specific tables are named in <literal>FOR UPDATE</literal>,
+    <literal>FOR SHARE</literal> behaves similarly, except that it
+    acquires a shared rather than exclusive lock on each retrieved
+    row.  A shared lock blocks other transactions from performing
+    <command>UPDATE</command>, <command>DELETE</command>, or <command>SELECT
+    FOR UPDATE</command> on these rows, but it does not prevent them
+    from performing <command>SELECT FOR SHARE</command>.
+   </para>
+
+   <para>
+    It is currently not allowed for a single <command>SELECT</command>
+    statement to include both <literal>FOR UPDATE</literal> and
+    <literal>FOR SHARE</literal>.
+   </para>
+
+   <para>
+    If specific tables are named in <literal>FOR UPDATE</literal>
+    or <literal>FOR SHARE</literal>,
     then only rows coming from those tables are locked; any other
     tables used in the <command>SELECT</command> are simply read as
     usual.
    </para>
 
    <para>
-    <literal>FOR UPDATE</literal> cannot be used in contexts where
-    returned rows can't be clearly identified with individual table
-    rows; for example it can't be used with aggregation.
+    <literal>FOR UPDATE</literal> and <literal>FOR SHARE</literal> cannot be
+    used in contexts where returned rows can't be clearly identified with
+    individual table rows; for example they can't be used with aggregation.
    </para>
 
    <para>
     It is possible for a <command>SELECT</> command using both 
-    <literal>LIMIT</literal> and  <literal>FOR UPDATE</literal> 
+    <literal>LIMIT</literal> and  <literal>FOR UPDATE/SHARE</literal> 
     clauses to return fewer rows than specified by <literal>LIMIT</literal>.
-    This is because <literal>LIMIT</> selects a number of rows,
-    but might then block requesting a <literal>FOR UPDATE</literal> lock.
-    Once the <literal>SELECT</> unblocks, the query qualification might not
-    be met and the row not be returned by <literal>SELECT</>.
+    This is because <literal>LIMIT</> is applied first.  The command
+    selects the specified number of rows,
+    but might then block trying to obtain lock on one or more of them.
+    Once the <literal>SELECT</> unblocks, the row might have been deleted
+    or updated so that it does not meet the query <literal>WHERE</> condition
+    anymore, in which case it will not be returned.
    </para>
   </refsect2>
  </refsect1>
index 9198a02e4c424747a345823d19e7b7bf7e0d7fd9..7e6a4807b7b4d136894f984e80c0ed1c0f0652f1 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/ref/select_into.sgml,v 1.34 2005/03/13 09:36:31 neilc Exp $
+$PostgreSQL: pgsql/doc/src/sgml/ref/select_into.sgml,v 1.35 2005/04/28 21:47:10 tgl Exp $
 PostgreSQL documentation
 -->
 
@@ -31,7 +31,7 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
     [ ORDER BY <replaceable class="PARAMETER">expression</replaceable> [ ASC | DESC | USING <replaceable class="PARAMETER">operator</replaceable> ] [, ...] ]
     [ LIMIT { <replaceable class="PARAMETER">count</replaceable> | ALL } ]
     [ OFFSET <replaceable class="PARAMETER">start</replaceable> ]
-    [ FOR UPDATE [ OF <replaceable class="PARAMETER">tablename</replaceable> [, ...] ] ]
+    [ FOR { UPDATE | SHARE } [ OF <replaceable class="PARAMETER">tablename</replaceable> [, ...] ] ]
 </synopsis>
  </refsynopsisdiv>
 
index 5ca4e2faca68415ec4ada835d5885bb9acec3ae0..04a640929a7a6d8133d904ae9cb020724df8fe51 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/sql.sgml,v 1.35 2005/02/21 02:21:03 neilc Exp $
+$PostgreSQL: pgsql/doc/src/sgml/sql.sgml,v 1.36 2005/04/28 21:47:09 tgl Exp $
 -->
 
  <chapter id="sql-intro">
@@ -866,7 +866,7 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
     [ ORDER BY <replaceable class="PARAMETER">expression</replaceable> [ ASC | DESC | USING <replaceable class="PARAMETER">operator</replaceable> ] [, ...] ]
     [ LIMIT { <replaceable class="PARAMETER">count</replaceable> | ALL } ]
     [ OFFSET <replaceable class="PARAMETER">start</replaceable> ]
-    [ FOR UPDATE [ OF <replaceable class="PARAMETER">class_name</replaceable> [, ...] ] ]
+    [ FOR { UPDATE | SHARE } [ OF <replaceable class="PARAMETER">class_name</replaceable> [, ...] ] ]
      </synopsis>
     </para>
 
index 31bf3c365dc37fe195a7853b4efe966f8589f2b0..1076020f99a79dc3402f116c6a0d91dd2fa3711e 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/storage.sgml,v 1.5 2005/04/07 03:31:42 neilc Exp $
+$PostgreSQL: pgsql/doc/src/sgml/storage.sgml,v 1.6 2005/04/28 21:47:09 tgl Exp $
 -->
 
 <chapter id="storage">
@@ -74,6 +74,12 @@ Item
  <entry>Subdirectory containing transaction commit status data</entry>
 </row>
 
+<row>
+ <entry><filename>pg_multixact</></entry>
+ <entry>Subdirectory containing multi-transaction status data
+  (used for shared row locks)</entry> 
+</row>
+
 <row>
  <entry><filename>pg_subtrans</></entry>
  <entry>Subdirectory containing subtransaction status data</entry>
index 605ed629426f4ccda41dbe1e7c76bb8d1b8d47e8..ee604df2caeb9153bb7345e607b052dd00acb4a5 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/access/heap/heapam.c,v 1.187 2005/04/14 20:03:22 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/access/heap/heapam.c,v 1.188 2005/04/28 21:47:10 tgl Exp $
  *
  *
  * INTERFACE ROUTINES
 
 #include "access/heapam.h"
 #include "access/hio.h"
+#include "access/multixact.h"
 #include "access/tuptoaster.h"
 #include "access/valid.h"
 #include "access/xlogutils.h"
 #include "catalog/catalog.h"
 #include "catalog/namespace.h"
 #include "miscadmin.h"
+#include "storage/sinval.h"
 #include "utils/inval.h"
 #include "utils/relcache.h"
 #include "pgstat.h"
@@ -1238,30 +1240,81 @@ l1:
        }
        else if (result == HeapTupleBeingUpdated && wait)
        {
-               TransactionId xwait = HeapTupleHeaderGetXmax(tp.t_data);
-
-               /* sleep until concurrent transaction ends */
-               LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
-               XactLockTableWait(xwait);
-
-               LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
-               if (!TransactionIdDidCommit(xwait))
-                       goto l1;
+               TransactionId xwait;
+               uint16  infomask;
 
                /*
-                * xwait is committed but if xwait had just marked the tuple for
-                * update then some other xaction could update this tuple before
-                * we got to this point.
+                * Sleep until concurrent transaction ends.  Note that we don't care
+                * if the locker has an exclusive or shared lock, because we need
+                * exclusive.
                 */
-               if (!TransactionIdEquals(HeapTupleHeaderGetXmax(tp.t_data), xwait))
-                       goto l1;
-               if (!(tp.t_data->t_infomask & HEAP_XMAX_COMMITTED))
+
+               /* must copy state data before unlocking buffer */
+               xwait = HeapTupleHeaderGetXmax(tp.t_data);
+               infomask = tp.t_data->t_infomask;
+
+               if (infomask & HEAP_XMAX_IS_MULTI)
                {
-                       tp.t_data->t_infomask |= HEAP_XMAX_COMMITTED;
-                       SetBufferCommitInfoNeedsSave(buffer);
+                       /* wait for multixact */
+                       LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
+                       MultiXactIdWait((MultiXactId) xwait);
+                       LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
+
+                       /*
+                        * If xwait had just locked the tuple then some other xact could
+                        * update this tuple before we get to this point.  Check for xmax
+                        * change, and start over if so.
+                        */
+                       if (!(tp.t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
+                               !TransactionIdEquals(HeapTupleHeaderGetXmax(tp.t_data),
+                                                                        xwait))
+                               goto l1;
+
+                       /*
+                        * You might think the multixact is necessarily done here, but
+                        * not so: it could have surviving members, namely our own xact
+                        * or other subxacts of this backend.  It is legal for us to
+                        * delete the tuple in either case, however (the latter case is
+                        * essentially a situation of upgrading our former shared lock
+                        * to exclusive).  We don't bother changing the on-disk hint bits
+                        * since we are about to overwrite the xmax altogether.
+                        */
                }
-               /* if tuple was marked for update but not updated... */
-               if (tp.t_data->t_infomask & HEAP_MARKED_FOR_UPDATE)
+               else
+               {
+                       /* wait for regular transaction to end */
+                       LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
+                       XactLockTableWait(xwait);
+                       LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
+
+                       /*
+                        * xwait is done, but if xwait had just locked the tuple then some
+                        * other xact could update this tuple before we get to this point.
+                        * Check for xmax change, and start over if so.
+                        */
+                       if ((tp.t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
+                               !TransactionIdEquals(HeapTupleHeaderGetXmax(tp.t_data),
+                                                                        xwait))
+                               goto l1;
+
+                       /* Otherwise we can mark it committed or aborted */
+                       if (!(tp.t_data->t_infomask & (HEAP_XMAX_COMMITTED |
+                                                                                  HEAP_XMAX_INVALID)))
+                       {
+                               if (TransactionIdDidCommit(xwait))
+                                       tp.t_data->t_infomask |= HEAP_XMAX_COMMITTED;
+                               else
+                                       tp.t_data->t_infomask |= HEAP_XMAX_INVALID;
+                               SetBufferCommitInfoNeedsSave(buffer);
+                       }
+               }
+
+               /*
+                * We may overwrite if previous xmax aborted, or if it committed
+                * but only locked the tuple without updating it.
+                */
+               if (tp.t_data->t_infomask & (HEAP_XMAX_INVALID |
+                                                                        HEAP_IS_LOCKED))
                        result = HeapTupleMayBeUpdated;
                else
                        result = HeapTupleUpdated;
@@ -1290,7 +1343,8 @@ l1:
        /* store transaction information of xact deleting the tuple */
        tp.t_data->t_infomask &= ~(HEAP_XMAX_COMMITTED |
                                                           HEAP_XMAX_INVALID |
-                                                          HEAP_MARKED_FOR_UPDATE |
+                                                          HEAP_XMAX_IS_MULTI |
+                                                          HEAP_IS_LOCKED |
                                                           HEAP_MOVED);
        HeapTupleHeaderSetXmax(tp.t_data, xid);
        HeapTupleHeaderSetCmax(tp.t_data, cid);
@@ -1465,30 +1519,81 @@ l2:
        }
        else if (result == HeapTupleBeingUpdated && wait)
        {
-               TransactionId xwait = HeapTupleHeaderGetXmax(oldtup.t_data);
-
-               /* sleep until concurrent transaction ends */
-               LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
-               XactLockTableWait(xwait);
-
-               LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
-               if (!TransactionIdDidCommit(xwait))
-                       goto l2;
+               TransactionId xwait;
+               uint16  infomask;
 
                /*
-                * xwait is committed but if xwait had just marked the tuple for
-                * update then some other xaction could update this tuple before
-                * we got to this point.
+                * Sleep until concurrent transaction ends.  Note that we don't care
+                * if the locker has an exclusive or shared lock, because we need
+                * exclusive.
                 */
-               if (!TransactionIdEquals(HeapTupleHeaderGetXmax(oldtup.t_data), xwait))
-                       goto l2;
-               if (!(oldtup.t_data->t_infomask & HEAP_XMAX_COMMITTED))
+
+               /* must copy state data before unlocking buffer */
+               xwait = HeapTupleHeaderGetXmax(oldtup.t_data);
+               infomask = oldtup.t_data->t_infomask;
+
+               if (infomask & HEAP_XMAX_IS_MULTI)
                {
-                       oldtup.t_data->t_infomask |= HEAP_XMAX_COMMITTED;
-                       SetBufferCommitInfoNeedsSave(buffer);
+                       /* wait for multixact */
+                       LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
+                       MultiXactIdWait((MultiXactId) xwait);
+                       LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
+
+                       /*
+                        * If xwait had just locked the tuple then some other xact could
+                        * update this tuple before we get to this point.  Check for xmax
+                        * change, and start over if so.
+                        */
+                       if (!(oldtup.t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
+                               !TransactionIdEquals(HeapTupleHeaderGetXmax(oldtup.t_data),
+                                                                        xwait))
+                               goto l2;
+
+                       /*
+                        * You might think the multixact is necessarily done here, but
+                        * not so: it could have surviving members, namely our own xact
+                        * or other subxacts of this backend.  It is legal for us to
+                        * update the tuple in either case, however (the latter case is
+                        * essentially a situation of upgrading our former shared lock
+                        * to exclusive).  We don't bother changing the on-disk hint bits
+                        * since we are about to overwrite the xmax altogether.
+                        */
                }
-               /* if tuple was marked for update but not updated... */
-               if (oldtup.t_data->t_infomask & HEAP_MARKED_FOR_UPDATE)
+               else
+               {
+                       /* wait for regular transaction to end */
+                       LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
+                       XactLockTableWait(xwait);
+                       LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
+
+                       /*
+                        * xwait is done, but if xwait had just locked the tuple then some
+                        * other xact could update this tuple before we get to this point.
+                        * Check for xmax change, and start over if so.
+                        */
+                       if ((oldtup.t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
+                               !TransactionIdEquals(HeapTupleHeaderGetXmax(oldtup.t_data),
+                                                                        xwait))
+                               goto l2;
+
+                       /* Otherwise we can mark it committed or aborted */
+                       if (!(oldtup.t_data->t_infomask & (HEAP_XMAX_COMMITTED |
+                                                                                          HEAP_XMAX_INVALID)))
+                       {
+                               if (TransactionIdDidCommit(xwait))
+                                       oldtup.t_data->t_infomask |= HEAP_XMAX_COMMITTED;
+                               else
+                                       oldtup.t_data->t_infomask |= HEAP_XMAX_INVALID;
+                               SetBufferCommitInfoNeedsSave(buffer);
+                       }
+               }
+
+               /*
+                * We may overwrite if previous xmax aborted, or if it committed
+                * but only locked the tuple without updating it.
+                */
+               if (oldtup.t_data->t_infomask & (HEAP_XMAX_INVALID |
+                                                                                HEAP_IS_LOCKED))
                        result = HeapTupleMayBeUpdated;
                else
                        result = HeapTupleUpdated;
@@ -1556,7 +1661,8 @@ l2:
        {
                oldtup.t_data->t_infomask &= ~(HEAP_XMAX_COMMITTED |
                                                                           HEAP_XMAX_INVALID |
-                                                                          HEAP_MARKED_FOR_UPDATE |
+                                                                          HEAP_XMAX_IS_MULTI |
+                                                                          HEAP_IS_LOCKED |
                                                                           HEAP_MOVED);
                HeapTupleHeaderSetXmax(oldtup.t_data, xid);
                HeapTupleHeaderSetCmax(oldtup.t_data, cid);
@@ -1642,7 +1748,8 @@ l2:
        {
                oldtup.t_data->t_infomask &= ~(HEAP_XMAX_COMMITTED |
                                                                           HEAP_XMAX_INVALID |
-                                                                          HEAP_MARKED_FOR_UPDATE |
+                                                                          HEAP_XMAX_IS_MULTI |
+                                                                          HEAP_IS_LOCKED |
                                                                           HEAP_MOVED);
                HeapTupleHeaderSetXmax(oldtup.t_data, xid);
                HeapTupleHeaderSetCmax(oldtup.t_data, cid);
@@ -1739,17 +1846,18 @@ simple_heap_update(Relation relation, ItemPointer otid, HeapTuple tup)
 }
 
 /*
- *     heap_mark4update                - mark a tuple for update
+ *     heap_lock_tuple         - lock a tuple in shared or exclusive mode
  */
 HTSU_Result
-heap_mark4update(Relation relation, HeapTuple tuple, Buffer *buffer,
-                                CommandId cid)
+heap_lock_tuple(Relation relation, HeapTuple tuple, Buffer *buffer,
+                                CommandId cid, LockTupleMode mode)
 {
-       TransactionId xid = GetCurrentTransactionId();
+       TransactionId   xid;
        ItemPointer tid = &(tuple->t_self);
        ItemId          lp;
        PageHeader      dp;
        HTSU_Result     result;
+       uint16          new_infomask;
 
        *buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid));
        LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
@@ -1767,38 +1875,93 @@ l3:
        {
                LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
                ReleaseBuffer(*buffer);
-               elog(ERROR, "attempted to mark4update invisible tuple");
+               elog(ERROR, "attempted to lock invisible tuple");
        }
        else if (result == HeapTupleBeingUpdated)
        {
-               TransactionId xwait = HeapTupleHeaderGetXmax(tuple->t_data);
+               if (mode == LockTupleShared &&
+                       (tuple->t_data->t_infomask & HEAP_XMAX_SHARED_LOCK))
+                       result = HeapTupleMayBeUpdated;
+               else
+               {
+                       TransactionId xwait;
+                       uint16  infomask;
 
-               /* sleep until concurrent transaction ends */
-               LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
-               XactLockTableWait(xwait);
+                       /*
+                        * Sleep until concurrent transaction ends.
+                        */
 
-               LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
-               if (!TransactionIdDidCommit(xwait))
-                       goto l3;
+                       /* must copy state data before unlocking buffer */
+                       xwait = HeapTupleHeaderGetXmax(tuple->t_data);
+                       infomask = tuple->t_data->t_infomask;
 
-               /*
-                * xwait is committed but if xwait had just marked the tuple for
-                * update then some other xaction could update this tuple before
-                * we got to this point.
-                */
-               if (!TransactionIdEquals(HeapTupleHeaderGetXmax(tuple->t_data), xwait))
-                       goto l3;
-               if (!(tuple->t_data->t_infomask & HEAP_XMAX_COMMITTED))
-               {
-                       tuple->t_data->t_infomask |= HEAP_XMAX_COMMITTED;
-                       SetBufferCommitInfoNeedsSave(*buffer);
+                       if (infomask & HEAP_XMAX_IS_MULTI)
+                       {
+                               /* wait for multixact */
+                               LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
+                               MultiXactIdWait((MultiXactId) xwait);
+                               LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
+
+                               /*
+                                * If xwait had just locked the tuple then some other xact
+                                * could update this tuple before we get to this point.
+                                * Check for xmax change, and start over if so.
+                                */
+                               if (!(tuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
+                                       !TransactionIdEquals(HeapTupleHeaderGetXmax(tuple->t_data),
+                                                                                xwait))
+                                       goto l3;
+
+                               /*
+                                * You might think the multixact is necessarily done here, but
+                                * not so: it could have surviving members, namely our own xact
+                                * or other subxacts of this backend.  It is legal for us to
+                                * lock the tuple in either case, however.  We don't bother
+                                * changing the on-disk hint bits since we are about to
+                                * overwrite the xmax altogether.
+                                */
+                       }
+                       else
+                       {
+                               /* wait for regular transaction to end */
+                               LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
+                               XactLockTableWait(xwait);
+                               LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
+
+                               /*
+                                * xwait is done, but if xwait had just locked the tuple then
+                                * some other xact could update this tuple before we get to
+                                * this point.  Check for xmax change, and start over if so.
+                                */
+                               if ((tuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
+                                       !TransactionIdEquals(HeapTupleHeaderGetXmax(tuple->t_data),
+                                                                                xwait))
+                                       goto l3;
+
+                               /* Otherwise we can mark it committed or aborted */
+                               if (!(tuple->t_data->t_infomask & (HEAP_XMAX_COMMITTED |
+                                                                                                  HEAP_XMAX_INVALID)))
+                               {
+                                       if (TransactionIdDidCommit(xwait))
+                                               tuple->t_data->t_infomask |= HEAP_XMAX_COMMITTED;
+                                       else
+                                               tuple->t_data->t_infomask |= HEAP_XMAX_INVALID;
+                                       SetBufferCommitInfoNeedsSave(*buffer);
+                               }
+                       }
+
+                       /*
+                        * We may lock if previous xmax aborted, or if it committed
+                        * but only locked the tuple without updating it.
+                        */
+                       if (tuple->t_data->t_infomask & (HEAP_XMAX_INVALID |
+                                                                                        HEAP_IS_LOCKED))
+                               result = HeapTupleMayBeUpdated;
+                       else
+                               result = HeapTupleUpdated;
                }
-               /* if tuple was marked for update but not updated... */
-               if (tuple->t_data->t_infomask & HEAP_MARKED_FOR_UPDATE)
-                       result = HeapTupleMayBeUpdated;
-               else
-                       result = HeapTupleUpdated;
        }
+
        if (result != HeapTupleMayBeUpdated)
        {
                Assert(result == HeapTupleSelfUpdated || result == HeapTupleUpdated);
@@ -1808,21 +1971,173 @@ l3:
        }
 
        /*
-        * XLOG stuff: no logging is required as long as we have no
-        * savepoints. For savepoints private log could be used...
+        * Compute the new xmax and infomask to store into the tuple.  Note we
+        * do not modify the tuple just yet, because that would leave it in the
+        * wrong state if multixact.c elogs.
         */
-       PageSetTLI(BufferGetPage(*buffer), ThisTimeLineID);
+       xid = GetCurrentTransactionId();
+
+       new_infomask = tuple->t_data->t_infomask;
+
+       new_infomask &= ~(HEAP_XMAX_COMMITTED |
+                                         HEAP_XMAX_INVALID |
+                                         HEAP_XMAX_IS_MULTI |
+                                         HEAP_IS_LOCKED |
+                                         HEAP_MOVED);
+
+       if (mode == LockTupleShared)
+       {
+               TransactionId   xmax = HeapTupleHeaderGetXmax(tuple->t_data);
+               uint16          old_infomask = tuple->t_data->t_infomask;
+
+               /*
+                * If this is the first acquisition of a shared lock in the current
+                * transaction, set my per-backend OldestMemberMXactId setting.
+                * We can be certain that the transaction will never become a
+                * member of any older MultiXactIds than that.  (We have to do this
+                * even if we end up just using our own TransactionId below, since
+                * some other backend could incorporate our XID into a MultiXact
+                * immediately afterwards.)
+                */
+               MultiXactIdSetOldestMember();
+
+               new_infomask |= HEAP_XMAX_SHARED_LOCK;
+
+               /*
+                * Check to see if we need a MultiXactId because there are multiple
+                * lockers.
+                *
+                * HeapTupleSatisfiesUpdate will have set the HEAP_XMAX_INVALID
+                * bit if the xmax was a MultiXactId but it was not running anymore.
+                * There is a race condition, which is that the MultiXactId may have
+                * finished since then, but that uncommon case is handled within
+                * MultiXactIdExpand.
+                *
+                * There is a similar race condition possible when the old xmax was
+                * a regular TransactionId.  We test TransactionIdIsInProgress again
+                * just to narrow the window, but it's still possible to end up
+                * creating an unnecessary MultiXactId.  Fortunately this is harmless.
+                */
+               if (!(old_infomask & (HEAP_XMAX_INVALID | HEAP_XMAX_COMMITTED)))
+               {
+                       if (old_infomask & HEAP_XMAX_IS_MULTI)
+                       {
+                               /*
+                                * If the XMAX is already a MultiXactId, then we need to
+                                * expand it to include our own TransactionId.
+                                */
+                               xid = MultiXactIdExpand(xmax, true, xid);
+                               new_infomask |= HEAP_XMAX_IS_MULTI;
+                       }
+                       else if (TransactionIdIsInProgress(xmax))
+                       {
+                               if (TransactionIdEquals(xmax, xid))
+                               {
+                                       /*
+                                        * If the old locker is ourselves, we'll just mark the
+                                        * tuple again with our own TransactionId.  However we
+                                        * have to consider the possibility that we had
+                                        * exclusive rather than shared lock before --- if so,
+                                        * be careful to preserve the exclusivity of the lock.
+                                        */
+                                       if (!(old_infomask & HEAP_XMAX_SHARED_LOCK))
+                                       {
+                                               new_infomask &= ~HEAP_XMAX_SHARED_LOCK;
+                                               new_infomask |= HEAP_XMAX_EXCL_LOCK;
+                                               mode = LockTupleExclusive;
+                                       }
+                               }
+                               else
+                               {
+                                       /*
+                                        * If the Xmax is a valid TransactionId, then we need to
+                                        * create a new MultiXactId that includes both the old
+                                        * locker and our own TransactionId.
+                                        */
+                                       xid = MultiXactIdExpand(xmax, false, xid);
+                                       new_infomask |= HEAP_XMAX_IS_MULTI;
+                               }
+                       }
+                       else
+                       {
+                               /*
+                                * Can get here iff HeapTupleSatisfiesUpdate saw the old
+                                * xmax as running, but it finished before
+                                * TransactionIdIsInProgress() got to run.  Treat it like
+                                * there's no locker in the tuple.
+                                */
+                       }
+               }
+               else
+               {
+                       /*
+                        * There was no previous locker, so just insert our own
+                        * TransactionId.
+                        */
+               }
+       }
+       else
+       {
+               /* We want an exclusive lock on the tuple */
+               new_infomask |= HEAP_XMAX_EXCL_LOCK;
+       }
+
+       START_CRIT_SECTION();
 
-       /* store transaction information of xact marking the tuple */
-       tuple->t_data->t_infomask &= ~(HEAP_XMAX_COMMITTED |
-                                                                  HEAP_XMAX_INVALID |
-                                                                  HEAP_MOVED);
-       tuple->t_data->t_infomask |= HEAP_MARKED_FOR_UPDATE;
+       /*
+        * Store transaction information of xact locking the tuple.
+        *
+        * Note: our CID is meaningless if storing a MultiXactId, but no harm
+        * in storing it anyway.
+        */
+       tuple->t_data->t_infomask = new_infomask;
        HeapTupleHeaderSetXmax(tuple->t_data, xid);
        HeapTupleHeaderSetCmax(tuple->t_data, cid);
        /* Make sure there is no forward chain link in t_ctid */
        tuple->t_data->t_ctid = *tid;
 
+       /*
+        * XLOG stuff.  You might think that we don't need an XLOG record because
+        * there is no state change worth restoring after a crash.  You would be
+        * wrong however: we have just written either a TransactionId or a
+        * MultiXactId that may never have been seen on disk before, and we need
+        * to make sure that there are XLOG entries covering those ID numbers.
+        * Else the same IDs might be re-used after a crash, which would be
+        * disastrous if this page made it to disk before the crash.  Essentially
+        * we have to enforce the WAL log-before-data rule even in this case.
+        */
+       if (!relation->rd_istemp)
+       {
+               xl_heap_lock xlrec;
+               XLogRecPtr      recptr;
+               XLogRecData rdata[2];
+
+               xlrec.target.node = relation->rd_node;
+               xlrec.target.tid = tuple->t_self;
+               xlrec.shared_lock = (mode == LockTupleShared);
+               rdata[0].buffer = InvalidBuffer;
+               rdata[0].data = (char *) &xlrec;
+               rdata[0].len = SizeOfHeapLock;
+               rdata[0].next = &(rdata[1]);
+
+               rdata[1].buffer = *buffer;
+               rdata[1].data = NULL;
+               rdata[1].len = 0;
+               rdata[1].next = NULL;
+
+               recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_LOCK, rdata);
+
+               PageSetLSN(dp, recptr);
+               PageSetTLI(dp, ThisTimeLineID);
+       }
+       else
+       {
+               /* No XLOG record, but still need to flag that XID exists on disk */
+               MyXactMadeTempRelUpdate = true;
+       }
+
+       END_CRIT_SECTION();
+
        LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
 
        WriteNoReleaseBuffer(*buffer);
@@ -1832,17 +2147,6 @@ l3:
 
 /* ----------------
  *             heap_markpos    - mark scan position
- *
- *             Note:
- *                             Should only one mark be maintained per scan at one time.
- *             Check if this can be done generally--say calls to get the
- *             next/previous tuple and NEVER pass struct scandesc to the
- *             user AM's.  Now, the mark is sent to the executor for safekeeping.
- *             Probably can store this info into a GENERAL scan structure.
- *
- *             May be best to change this call to store the marked position
- *             (up to 2?) in the scan structure itself.
- *             Fix to use the proper caching structure.
  * ----------------
  */
 void
@@ -1858,19 +2162,6 @@ heap_markpos(HeapScanDesc scan)
 
 /* ----------------
  *             heap_restrpos   - restore position to marked location
- *
- *             Note:  there are bad side effects here.  If we were past the end
- *             of a relation when heapmarkpos is called, then if the relation is
- *             extended via insert, then the next call to heaprestrpos will set
- *             cause the added tuples to be visible when the scan continues.
- *             Problems also arise if the TID's are rearranged!!!
- *
- * XXX might be better to do direct access instead of
- *             using the generality of heapgettup().
- *
- * XXX It is very possible that when a scan is restored, that a tuple
- * XXX which previously qualified may fail for time range purposes, unless
- * XXX some form of locking exists (ie., portals currently can act funny.
  * ----------------
  */
 void
@@ -1996,8 +2287,7 @@ log_heap_update(Relation reln, Buffer oldbuf, ItemPointerData from,
        {
                TransactionId xid[2];   /* xmax, xmin */
 
-               if (newtup->t_data->t_infomask & (HEAP_XMAX_INVALID |
-                                                                                 HEAP_MARKED_FOR_UPDATE))
+               if (newtup->t_data->t_infomask & (HEAP_XMAX_INVALID | HEAP_IS_LOCKED))
                        xid[0] = InvalidTransactionId;
                else
                        xid[0] = HeapTupleHeaderGetXmax(newtup->t_data);
@@ -2185,7 +2475,8 @@ heap_xlog_delete(bool redo, XLogRecPtr lsn, XLogRecord *record)
        {
                htup->t_infomask &= ~(HEAP_XMAX_COMMITTED |
                                                          HEAP_XMAX_INVALID |
-                                                         HEAP_MARKED_FOR_UPDATE |
+                                                         HEAP_XMAX_IS_MULTI |
+                                                         HEAP_IS_LOCKED |
                                                          HEAP_MOVED);
                HeapTupleHeaderSetXmax(htup, record->xl_xid);
                HeapTupleHeaderSetCmax(htup, FirstCommandId);
@@ -2365,7 +2656,8 @@ heap_xlog_update(bool redo, XLogRecPtr lsn, XLogRecord *record, bool move)
                {
                        htup->t_infomask &= ~(HEAP_XMAX_COMMITTED |
                                                                  HEAP_XMAX_INVALID |
-                                                                 HEAP_MARKED_FOR_UPDATE |
+                                                                 HEAP_XMAX_IS_MULTI |
+                                                                 HEAP_IS_LOCKED |
                                                                  HEAP_MOVED);
                        HeapTupleHeaderSetXmax(htup, record->xl_xid);
                        HeapTupleHeaderSetCmax(htup, FirstCommandId);
@@ -2487,6 +2779,82 @@ newsame:;
 
 }
 
+static void
+heap_xlog_lock(bool redo, XLogRecPtr lsn, XLogRecord *record)
+{
+       xl_heap_lock *xlrec = (xl_heap_lock *) XLogRecGetData(record);
+       Relation        reln;
+       Buffer          buffer;
+       Page            page;
+       OffsetNumber offnum;
+       ItemId          lp = NULL;
+       HeapTupleHeader htup;
+
+       if (redo && (record->xl_info & XLR_BKP_BLOCK_1))
+               return;
+
+       reln = XLogOpenRelation(redo, RM_HEAP_ID, xlrec->target.node);
+
+       if (!RelationIsValid(reln))
+               return;
+
+       buffer = XLogReadBuffer(false, reln,
+                                               ItemPointerGetBlockNumber(&(xlrec->target.tid)));
+       if (!BufferIsValid(buffer))
+               elog(PANIC, "heap_lock_%sdo: no block", (redo) ? "re" : "un");
+
+       page = (Page) BufferGetPage(buffer);
+       if (PageIsNew((PageHeader) page))
+               elog(PANIC, "heap_lock_%sdo: uninitialized page", (redo) ? "re" : "un");
+
+       if (redo)
+       {
+               if (XLByteLE(lsn, PageGetLSN(page)))    /* changes are applied */
+               {
+                       LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
+                       ReleaseBuffer(buffer);
+                       return;
+               }
+       }
+       else if (XLByteLT(PageGetLSN(page), lsn))       /* changes are not applied
+                                                                                                * ?! */
+               elog(PANIC, "heap_lock_undo: bad page LSN");
+
+       offnum = ItemPointerGetOffsetNumber(&(xlrec->target.tid));
+       if (PageGetMaxOffsetNumber(page) >= offnum)
+               lp = PageGetItemId(page, offnum);
+
+       if (PageGetMaxOffsetNumber(page) < offnum || !ItemIdIsUsed(lp))
+               elog(PANIC, "heap_lock_%sdo: invalid lp", (redo) ? "re" : "un");
+
+       htup = (HeapTupleHeader) PageGetItem(page, lp);
+
+       if (redo)
+       {
+               /*
+                * Presently, we don't bother to restore the locked state, but
+                * just set the XMAX_INVALID bit.
+                */
+               htup->t_infomask &= ~(HEAP_XMAX_COMMITTED |
+                                                         HEAP_XMAX_INVALID |
+                                                         HEAP_XMAX_IS_MULTI |
+                                                         HEAP_IS_LOCKED |
+                                                         HEAP_MOVED);
+               htup->t_infomask |= HEAP_XMAX_INVALID;
+               HeapTupleHeaderSetXmax(htup, record->xl_xid);
+               HeapTupleHeaderSetCmax(htup, FirstCommandId);
+               /* Make sure there is no forward chain link in t_ctid */
+               htup->t_ctid = xlrec->target.tid;
+               PageSetLSN(page, lsn);
+               PageSetTLI(page, ThisTimeLineID);
+               LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
+               WriteBuffer(buffer);
+               return;
+       }
+
+       elog(PANIC, "heap_lock_undo: unimplemented");
+}
+
 void
 heap_redo(XLogRecPtr lsn, XLogRecord *record)
 {
@@ -2505,6 +2873,8 @@ heap_redo(XLogRecPtr lsn, XLogRecord *record)
                heap_xlog_clean(true, lsn, record);
        else if (info == XLOG_HEAP_NEWPAGE)
                heap_xlog_newpage(true, lsn, record);
+       else if (info == XLOG_HEAP_LOCK)
+               heap_xlog_lock(true, lsn, record);
        else
                elog(PANIC, "heap_redo: unknown op code %u", info);
 }
@@ -2527,6 +2897,8 @@ heap_undo(XLogRecPtr lsn, XLogRecord *record)
                heap_xlog_clean(false, lsn, record);
        else if (info == XLOG_HEAP_NEWPAGE)
                heap_xlog_newpage(false, lsn, record);
+       else if (info == XLOG_HEAP_LOCK)
+               heap_xlog_lock(false, lsn, record);
        else
                elog(PANIC, "heap_undo: unknown op code %u", info);
 }
@@ -2589,6 +2961,16 @@ heap_desc(char *buf, uint8 xl_info, char *rec)
                                xlrec->node.spcNode, xlrec->node.dbNode,
                                xlrec->node.relNode, xlrec->blkno);
        }
+       else if (info == XLOG_HEAP_LOCK)
+       {
+               xl_heap_lock *xlrec = (xl_heap_lock *) rec;
+
+               if (xlrec->shared_lock)
+                       strcat(buf, "shared_lock: ");
+               else
+                       strcat(buf, "exclusive_lock: ");
+               out_target(buf, &(xlrec->target));
+       }
        else
                strcat(buf, "UNKNOWN");
 }
index fe740a045f8ce55bef533ea90bd576a48521cdab..295ecf9e14f64d0ba94f657251f729583441a400 100644 (file)
@@ -4,7 +4,7 @@
 #    Makefile for access/transam
 #
 # IDENTIFICATION
-#    $PostgreSQL: pgsql/src/backend/access/transam/Makefile,v 1.19 2004/07/01 00:49:42 tgl Exp $
+#    $PostgreSQL: pgsql/src/backend/access/transam/Makefile,v 1.20 2005/04/28 21:47:10 tgl Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -12,7 +12,7 @@ subdir = src/backend/access/transam
 top_builddir = ../../../..
 include $(top_builddir)/src/Makefile.global
 
-OBJS = clog.o transam.o varsup.o xact.o xlog.o xlogutils.o rmgr.o slru.o subtrans.o
+OBJS = clog.o transam.o varsup.o xact.o xlog.o xlogutils.o rmgr.o slru.o subtrans.o multixact.o
 
 all: SUBSYS.o
 
diff --git a/src/backend/access/transam/multixact.c b/src/backend/access/transam/multixact.c
new file mode 100644 (file)
index 0000000..de1a882
--- /dev/null
@@ -0,0 +1,1557 @@
+/*-------------------------------------------------------------------------
+ *
+ * multixact.c
+ *             PostgreSQL multi-transaction-log manager
+ *
+ * The pg_multixact manager is a pg_clog-like manager that stores an array
+ * of TransactionIds for each MultiXactId.  It is a fundamental part of the
+ * shared-row-lock implementation.  A share-locked tuple stores a
+ * MultiXactId in its Xmax, and a transaction that needs to wait for the
+ * tuple to be unlocked can sleep on the potentially-several TransactionIds
+ * that compose the MultiXactId.
+ *
+ * We use two SLRU areas, one for storing the offsets on which the data
+ * starts for each MultiXactId in the other one.  This trick allows us to
+ * store variable length arrays of TransactionIds.  (We could alternatively
+ * use one area containing counts and TransactionIds, with valid MultiXactId
+ * values pointing at slots containing counts; but that way seems less robust
+ * since it would get completely confused if someone inquired about a bogus
+ * MultiXactId that pointed to an intermediate slot containing an XID.)
+ *
+ * This code is based on subtrans.c; see it for additional discussion.
+ * Like the subtransaction manager, we only need to remember multixact
+ * information for currently-open transactions.  Thus, there is
+ * no need to preserve data over a crash and restart.
+ *
+ * The only XLOG interaction we need to take care of is that generated
+ * MultiXactId values must continue to increase across a system crash.
+ * Thus we log groups of MultiXactIds acquisition in the same fashion we do
+ * for Oids (see XLogPutNextMultiXactId).
+ *
+ * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * $PostgreSQL: pgsql/src/backend/access/transam/multixact.c,v 1.1 2005/04/28 21:47:10 tgl Exp $
+ *
+ *-------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+#include "access/multixact.h"
+#include "access/slru.h"
+#include "access/xact.h"
+#include "miscadmin.h"
+#include "utils/memutils.h"
+#include "storage/backendid.h"
+#include "storage/lmgr.h"
+#include "storage/sinval.h"
+
+
+/*
+ * Defines for MultiXactOffset page sizes.  A page is the same BLCKSZ as is
+ * used everywhere else in Postgres.
+ *
+ * Note: because both uint32 and TransactionIds are 32 bits and wrap around at
+ * 0xFFFFFFFF, MultiXact page numbering also wraps around at
+ * 0xFFFFFFFF/MULTIXACT_*_PER_PAGE, and segment numbering at
+ * 0xFFFFFFFF/MULTIXACT_*_PER_PAGE/SLRU_SEGMENTS_PER_PAGE.  We need take no
+ * explicit notice of that fact in this module, except when comparing segment
+ * and page numbers in TruncateMultiXact
+ * (see MultiXact{Offset,Member}PagePrecedes).
+ */
+
+/* We need four bytes per offset and also four bytes per member */
+#define MULTIXACT_OFFSETS_PER_PAGE (BLCKSZ / sizeof(uint32))
+#define MULTIXACT_MEMBERS_PER_PAGE (BLCKSZ / sizeof(TransactionId))
+
+#define MultiXactIdToOffsetPage(xid) \
+       ((xid) / (uint32) MULTIXACT_OFFSETS_PER_PAGE)
+#define MultiXactIdToOffsetEntry(xid) \
+       ((xid) % (uint32) MULTIXACT_OFFSETS_PER_PAGE)
+
+#define MXOffsetToMemberPage(xid) \
+       ((xid) / (TransactionId) MULTIXACT_MEMBERS_PER_PAGE)
+#define MXOffsetToMemberEntry(xid) \
+       ((xid) % (TransactionId) MULTIXACT_MEMBERS_PER_PAGE)
+
+/* Arbitrary number of MultiXactIds to allocate at each XLog call */
+#define MXACT_PREFETCH 8192
+
+/*
+ * Links to shared-memory data structures for MultiXact control
+ */
+static SlruCtlData MultiXactOffsetCtlData;
+static SlruCtlData MultiXactMemberCtlData;
+
+#define MultiXactOffsetCtl  (&MultiXactOffsetCtlData)
+#define MultiXactMemberCtl  (&MultiXactMemberCtlData)
+
+/*
+ * MultiXact state shared across all backends.  All this state is protected
+ * by MultiXactGenLock.  (We also use MultiXactOffsetControlLock and
+ * MultiXactMemberControlLock to guard accesses to the two sets of SLRU
+ * buffers.  For concurrency's sake, we avoid holding more than one of these
+ * locks at a time.)
+ */
+typedef struct MultiXactStateData
+{
+       /* next-to-be-assigned MultiXactId */
+       MultiXactId             nextMXact;
+
+       /* MultiXactIds we have left before logging more */
+       uint32                  mXactCount;
+
+       /* next-to-be-assigned offset */
+       uint32                  nextOffset;
+
+       /* the Offset SLRU area was last truncated at this MultiXactId */
+       MultiXactId             lastTruncationPoint;
+
+       /*
+        * Per-backend data starts here.  We have two arrays stored in
+        * the area immediately following the MultiXactStateData struct.
+        * Each is indexed by BackendId.  (Note: valid BackendIds run from 1 to
+        * MaxBackends; element zero of each array is never used.)
+        *
+        * OldestMemberMXactId[k] is the oldest MultiXactId each backend's
+        * current transaction(s) could possibly be a member of, or
+        * InvalidMultiXactId when the backend has no live transaction that
+        * could possibly be a member of a MultiXact.  Each backend sets its
+        * entry to the current nextMXact counter just before first acquiring a
+        * shared lock in a given transaction, and clears it at transaction end.
+        * (This works because only during or after acquiring a shared lock
+        * could an XID possibly become a member of a MultiXact, and that
+        * MultiXact would have to be created during or after the lock
+        * acquisition.)
+        *
+        * OldestVisibleMXactId[k] is the oldest MultiXactId each backend's
+        * current transaction(s) think is potentially live, or InvalidMultiXactId
+        * when not in a transaction or not in a transaction that's paid any
+        * attention to MultiXacts yet.  This is computed when first needed in
+        * a given transaction, and cleared at transaction end.  We can compute
+        * it as the minimum of the valid OldestMemberMXactId[] entries at the
+        * time we compute it (using nextMXact if none are valid).  Each backend
+        * is required not to attempt to access any SLRU data for MultiXactIds
+        * older than its own OldestVisibleMXactId[] setting; this is necessary
+        * because the checkpointer could truncate away such data at any instant.
+        *
+        * The checkpointer can compute the safe truncation point as the oldest
+        * valid value among all the OldestMemberMXactId[] and
+        * OldestVisibleMXactId[] entries, or nextMXact if none are valid.
+        * Clearly, it is not possible for any later-computed OldestVisibleMXactId
+        * value to be older than this, and so there is no risk of truncating
+        * data that is still needed.
+        */
+       MultiXactId        perBackendXactIds[1];        /* VARIABLE LENGTH ARRAY */
+} MultiXactStateData;
+
+/* Pointers to the state data in shared memory */
+static MultiXactStateData *MultiXactState;
+static MultiXactId *OldestMemberMXactId;
+static MultiXactId *OldestVisibleMXactId;
+
+
+/*
+ * Definitions for the backend-local MultiXactId cache.
+ *
+ * We use this cache to store known MultiXacts, so we don't need to go to
+ * SLRU areas everytime.
+ *
+ * The cache lasts for the duration of a single transaction, the rationale
+ * for this being that most entries will contain our own TransactionId and
+ * so they will be uninteresting by the time our next transaction starts.
+ * (XXX not clear that this is correct --- other members of the MultiXact
+ * could hang around longer than we did.)
+ *
+ * We allocate the cache entries in a memory context that is deleted at
+ * transaction end, so we don't need to do retail freeing of entries.
+ */
+typedef struct mXactCacheEnt
+{
+       struct mXactCacheEnt *next;
+       MultiXactId             multi;
+       int                             nxids;
+       TransactionId   xids[1];        /* VARIABLE LENGTH ARRAY */
+} mXactCacheEnt;
+
+static mXactCacheEnt   *MXactCache = NULL;
+static MemoryContext   MXactContext = NULL;
+
+
+#ifdef MULTIXACT_DEBUG
+#define debug_elog2(a,b) elog(a,b)
+#define debug_elog3(a,b,c) elog(a,b,c)
+#define debug_elog4(a,b,c,d) elog(a,b,c,d)
+#define debug_elog5(a,b,c,d,e) elog(a,b,c,d,e)
+#else
+#define debug_elog2(a,b)
+#define debug_elog3(a,b,c)
+#define debug_elog4(a,b,c,d)
+#define debug_elog5(a,b,c,d,e)
+#endif
+
+/* internal MultiXactId management */
+static void MultiXactIdSetOldestVisible(void);
+static MultiXactId CreateMultiXactId(int nxids, TransactionId *xids);
+static int GetMultiXactIdMembers(MultiXactId multi, TransactionId **xids);
+static MultiXactId GetNewMultiXactId(int nxids, uint32 *offset);
+
+/* MultiXact cache management */
+static MultiXactId mXactCacheGetBySet(int nxids, TransactionId *xids);
+static int mXactCacheGetById(MultiXactId multi, TransactionId **xids);
+static void mXactCachePut(MultiXactId multi, int nxids, TransactionId *xids);
+static int xidComparator(const void *arg1, const void *arg2);
+#ifdef MULTIXACT_DEBUG
+static char *mxid_to_string(MultiXactId multi, int nxids, TransactionId *xids);
+#endif
+
+/* management of SLRU infrastructure */
+static int     ZeroMultiXactOffsetPage(int pageno);
+static int     ZeroMultiXactMemberPage(int pageno);
+static bool MultiXactOffsetPagePrecedes(int page1, int page2);
+static bool MultiXactMemberPagePrecedes(int page1, int page2);
+static bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2);
+static bool MultiXactOffsetPrecedes(uint32 offset1, uint32 offset2);
+static void ExtendMultiXactOffset(MultiXactId multi);
+static void ExtendMultiXactMember(uint32 offset);
+static void TruncateMultiXact(void);
+
+
+/*
+ * MultiXactIdExpand
+ *             Add a TransactionId to a possibly-already-existing MultiXactId.
+ *
+ * We abuse the notation for the first argument: if "isMulti" is true, then
+ * it's really a MultiXactId; else it's a TransactionId.  We are already
+ * storing MultiXactId in HeapTupleHeader's xmax so assuming the datatypes
+ * are equivalent is necessary anyway.
+ *
+ * If isMulti is true, then get the members of the passed MultiXactId, add
+ * the passed TransactionId, and create a new MultiXactId.  If isMulti is
+ * false, then take the two TransactionIds and create a new MultiXactId with
+ * them.  The caller must ensure that the multi and xid are different
+ * in the latter case.
+ *
+ * If the TransactionId is already a member of the passed MultiXactId,
+ * just return it as-is.
+ *
+ * Note that we do NOT actually modify the membership of a pre-existing
+ * MultiXactId; instead we create a new one.  This is necessary to avoid
+ * a race condition against MultiXactIdWait (see notes there).
+ *
+ * NB - we don't worry about our local MultiXactId cache here, because that
+ * is handled by the lower-level routines.
+ */
+MultiXactId
+MultiXactIdExpand(MultiXactId multi, bool isMulti, TransactionId xid)
+{
+       MultiXactId             newMulti;
+       TransactionId  *members;
+       TransactionId  *newMembers;
+       int                             nmembers;
+       int                             i;
+       int                             j;
+
+       AssertArg(MultiXactIdIsValid(multi));
+       AssertArg(TransactionIdIsValid(xid));
+
+       debug_elog5(DEBUG2, "Expand: received %s %u, xid %u",
+                               isMulti ? "MultiXactId" : "TransactionId",
+                               multi, xid);
+
+       if (!isMulti)
+       {
+               /*
+                * The first argument is a TransactionId, not a MultiXactId.
+                */
+               TransactionId   xids[2];
+
+               Assert(!TransactionIdEquals(multi, xid));
+
+               xids[0] = multi;
+               xids[1] = xid;
+
+               newMulti = CreateMultiXactId(2, xids);
+
+               debug_elog5(DEBUG2, "Expand: returning %u two-elem %u/%u",
+                                       newMulti, multi, xid);
+
+               return newMulti;
+       }
+
+       nmembers = GetMultiXactIdMembers(multi, &members);
+
+       if (nmembers < 0)
+       {
+               /*
+                * The MultiXactId is obsolete.  This can only happen if all the
+                * MultiXactId members stop running between the caller checking and
+                * passing it to us.  It would be better to return that fact to the
+                * caller, but it would complicate the API and it's unlikely to happen
+                * too often, so just deal with it by creating a singleton MultiXact.
+                */
+               newMulti = CreateMultiXactId(1, &xid);
+
+               debug_elog4(DEBUG2, "Expand: %u has no members, create singleton %u",
+                                       multi, newMulti);
+               return newMulti;
+       }
+
+       /*
+        * If the TransactionId is already a member of the MultiXactId,
+        * just return the existing MultiXactId.
+        */
+       for (i = 0; i < nmembers; i++)
+       {
+               if (TransactionIdEquals(members[i], xid))
+               {
+                       pfree(members);
+                       debug_elog4(DEBUG2, "Expand: %u is already a member of %u",
+                                               xid, multi);
+                       return multi;
+               }
+       }
+
+       /*
+        * Determine which of the members of the MultiXactId are still running,
+        * and use them to create a new one.  (Removing dead members is just
+        * an optimization, but a useful one.  Note we have the same race
+        * condition here as above: j could be 0 at the end of the loop.)
+        */
+       newMembers = (TransactionId *)
+               palloc(sizeof(TransactionId) * (nmembers + 1));
+
+       for (i = 0, j = 0; i < nmembers; i++)
+       {
+               if (TransactionIdIsInProgress(members[i]))
+                       newMembers[j++] = members[i];
+       }
+
+       newMembers[j++] = xid;
+       newMulti = CreateMultiXactId(j, newMembers);
+
+       pfree(members);
+       pfree(newMembers);
+
+       debug_elog3(DEBUG2, "Expand: returning new multi %u", newMulti);
+
+       return newMulti;
+}
+
+/*
+ * MultiXactIdIsRunning
+ *             Returns whether a MultiXactId is "running".
+ *
+ * We return true if at least one member of the given MultiXactId is still
+ * running.  Note that a "false" result is certain not to change,
+ * because it is not legal to add members to an existing MultiXactId.
+ */
+bool
+MultiXactIdIsRunning(MultiXactId multi)
+{
+       TransactionId *members;
+       TransactionId  myXid;
+       int             nmembers;
+       int             i;
+
+       debug_elog3(DEBUG2, "IsRunning %u?", multi);
+
+       nmembers = GetMultiXactIdMembers(multi, &members);
+
+       if (nmembers < 0)
+       {
+               debug_elog2(DEBUG2, "IsRunning: no members");
+               return false;
+       }
+
+       /* checking for myself is cheap */
+       myXid = GetTopTransactionId();
+
+       for (i = 0; i < nmembers; i++)
+       {
+               if (TransactionIdEquals(members[i], myXid))
+               {
+                       pfree(members);
+                       debug_elog3(DEBUG2, "IsRunning: I (%d) am running!", i);
+                       return true;
+               }
+       }
+
+       /*
+        * This could be made better by having a special entry point in sinval.c,
+        * walking the PGPROC array only once for the whole array.  But in most
+        * cases nmembers should be small enough that it doesn't much matter.
+        */
+       for (i = 0; i < nmembers; i++)
+       {
+               if (TransactionIdIsInProgress(members[i]))
+               {
+                       pfree(members);
+                       debug_elog4(DEBUG2, "IsRunning: member %d (%u) is running",
+                                       i, members[i]);
+                       return true;
+               }
+       }
+
+       pfree(members);
+       debug_elog3(DEBUG2, "IsRunning: %u is not running", multi);
+
+       return false;
+}
+
+/*
+ * MultiXactIdSetOldestMember
+ *             Save the oldest MultiXactId this transaction could be a member of.
+ *
+ * We set the OldestMemberMXactId for a given transaction the first time
+ * it's going to acquire a shared lock.  We need to do this even if we end
+ * up using a TransactionId instead of a MultiXactId, because there is a
+ * chance that another transaction would add our XID to a MultiXactId.
+ *
+ * The value to set is the next-to-be-assigned MultiXactId, so this is meant
+ * to be called just before acquiring a shared lock.
+ */
+void
+MultiXactIdSetOldestMember(void)
+{
+       if (!MultiXactIdIsValid(OldestMemberMXactId[MyBackendId]))
+       {
+               MultiXactId nextMXact;
+
+               /*
+                * You might think we don't need to acquire a lock here, since
+                * fetching and storing of TransactionIds is probably atomic,
+                * but in fact we do: suppose we pick up nextMXact and then
+                * lose the CPU for a long time.  Someone else could advance
+                * nextMXact, and then another someone else could compute an
+                * OldestVisibleMXactId that would be after the value we are
+                * going to store when we get control back.  Which would be wrong.
+                */
+               LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
+
+               /*
+                * We have to beware of the possibility that nextMXact is in the
+                * wrapped-around state.  We don't fix the counter itself here,
+                * but we must be sure to store a valid value in our array entry.
+                */
+               nextMXact = MultiXactState->nextMXact;
+               if (nextMXact < FirstMultiXactId)
+                       nextMXact = FirstMultiXactId;
+
+               OldestMemberMXactId[MyBackendId] = nextMXact;
+
+               LWLockRelease(MultiXactGenLock);
+
+               debug_elog4(DEBUG2, "MultiXact: setting OldestMember[%d] = %u",
+                                       MyBackendId, nextMXact);
+       }
+}
+
+/*
+ * MultiXactIdSetOldestVisible
+ *             Save the oldest MultiXactId this transaction considers possibly live.
+ *
+ * We set the OldestVisibleMXactId for a given transaction the first time
+ * it's going to inspect any MultiXactId.  Once we have set this, we are
+ * guaranteed that the checkpointer won't truncate off SLRU data for
+ * MultiXactIds at or after our OldestVisibleMXactId.
+ *
+ * The value to set is the oldest of nextMXact and all the valid per-backend
+ * OldestMemberMXactId[] entries.  Because of the locking we do, we can be
+ * certain that no subsequent call to MultiXactIdSetOldestMember can set
+ * an OldestMemberMXactId[] entry older than what we compute here.  Therefore
+ * there is no live transaction, now or later, that can be a member of any
+ * MultiXactId older than the OldestVisibleMXactId we compute here.
+ */
+static void
+MultiXactIdSetOldestVisible(void)
+{
+       if (!MultiXactIdIsValid(OldestVisibleMXactId[MyBackendId]))
+       {
+               MultiXactId oldestMXact;
+               int             i;
+
+               LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
+
+               /*
+                * We have to beware of the possibility that nextMXact is in the
+                * wrapped-around state.  We don't fix the counter itself here,
+                * but we must be sure to store a valid value in our array entry.
+                */
+               oldestMXact = MultiXactState->nextMXact;
+               if (oldestMXact < FirstMultiXactId)
+                       oldestMXact = FirstMultiXactId;
+
+               for (i = 1; i <= MaxBackends; i++)
+               {
+                       MultiXactId thisoldest = OldestMemberMXactId[i];
+
+                       if (MultiXactIdIsValid(thisoldest) &&
+                               MultiXactIdPrecedes(thisoldest, oldestMXact))
+                               oldestMXact = thisoldest;
+               }
+
+               OldestVisibleMXactId[MyBackendId] = oldestMXact;
+
+               LWLockRelease(MultiXactGenLock);
+
+               debug_elog4(DEBUG2, "MultiXact: setting OldestVisible[%d] = %u",
+                                       MyBackendId, oldestMXact);
+       }
+}
+
+/*
+ * MultiXactIdWait
+ *             Sleep on a MultiXactId.
+ *
+ * We do this by sleeping on each member using XactLockTableWait.  Any
+ * members that belong to the current backend are *not* waited for, however;
+ * this would not merely be useless but would lead to Assert failure inside
+ * XactLockTableWait.  By the time this returns, it is certain that all
+ * transactions *of other backends* that were members of the MultiXactId
+ * are dead (and no new ones can have been added, since it is not legal
+ * to add members to an existing MultiXactId).
+ *
+ * But by the time we finish sleeping, someone else may have changed the Xmax
+ * of the containing tuple, so the caller needs to iterate on us somehow.
+ */
+void
+MultiXactIdWait(MultiXactId multi)
+{
+       TransactionId *members;
+       int             nmembers;
+
+       nmembers = GetMultiXactIdMembers(multi, &members);
+
+       if (nmembers >= 0)
+       {
+               int             i;
+
+               for (i = 0; i < nmembers; i++)
+               {
+                       TransactionId   member = members[i];
+
+                       debug_elog4(DEBUG2, "MultiXactIdWait: waiting for %d (%u)",
+                                               i, member);
+                       if (!TransactionIdIsCurrentTransactionId(member))
+                               XactLockTableWait(member);
+               }
+
+               pfree(members);
+       }
+}
+
+/*
+ * CreateMultiXactId
+ *             Make a new MultiXactId
+ *
+ * Make SLRU and cache entries for a new MultiXactId, recording the given
+ * TransactionIds as members.  Returns the newly created MultiXactId.
+ *
+ * NB: the passed xids[] array will be sorted in-place.
+ */
+static MultiXactId
+CreateMultiXactId(int nxids, TransactionId *xids)
+{
+       MultiXactId     multi;
+       int                     pageno;
+       int                     prev_pageno;
+       int                     entryno;
+       int                     slotno;
+       uint32     *offptr;
+       uint32          offset;
+       int                     i;
+
+       debug_elog3(DEBUG2, "Create: %s",
+                               mxid_to_string(InvalidMultiXactId, nxids, xids));
+
+       /*
+        * See if the same set of XIDs already exists in our cache; if so, just
+        * re-use that MultiXactId.  (Note: it might seem that looking in our
+        * cache is insufficient, and we ought to search disk to see if a
+        * duplicate definition already exists.  But since we only ever create
+        * MultiXacts containing our own XID, in most cases any such MultiXacts
+        * were in fact created by us, and so will be in our cache.  There are
+        * corner cases where someone else added us to a MultiXact without our
+        * knowledge, but it's not worth checking for.)
+        */
+       multi = mXactCacheGetBySet(nxids, xids);
+       if (MultiXactIdIsValid(multi))
+       {       
+               debug_elog2(DEBUG2, "Create: in cache!");
+               return multi;
+       }
+
+       multi = GetNewMultiXactId(nxids, &offset);
+
+       LWLockAcquire(MultiXactOffsetControlLock, LW_EXCLUSIVE);
+
+       ExtendMultiXactOffset(multi);
+
+       pageno = MultiXactIdToOffsetPage(multi);
+       entryno = MultiXactIdToOffsetEntry(multi);
+
+       /*
+        * Note: we pass the MultiXactId to SimpleLruReadPage as the "transaction"
+        * to complain about if there's any I/O error.  This is kinda bogus, but
+        * since the errors will always give the full pathname, it should be
+        * clear enough that a MultiXactId is really involved.  Perhaps someday
+        * we'll take the trouble to generalize the slru.c error reporting code.
+        */
+       slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, multi);
+       offptr = (uint32 *) MultiXactOffsetCtl->shared->page_buffer[slotno];
+       offptr += entryno;
+       *offptr = offset;
+
+       MultiXactOffsetCtl->shared->page_status[slotno] = SLRU_PAGE_DIRTY;
+
+       /* Exchange our lock */
+       LWLockRelease(MultiXactOffsetControlLock);
+
+       debug_elog3(DEBUG2, "Create: got offset %u", offset);
+
+       LWLockAcquire(MultiXactMemberControlLock, LW_EXCLUSIVE);
+
+       prev_pageno = -1;
+
+       for (i = 0; i < nxids; i++, offset++)
+       {
+               TransactionId *memberptr;
+
+               ExtendMultiXactMember(offset);
+
+               pageno = MXOffsetToMemberPage(offset);
+               entryno = MXOffsetToMemberEntry(offset);
+
+               if (pageno != prev_pageno)
+               {
+                       slotno = SimpleLruReadPage(MultiXactMemberCtl, pageno, multi);
+                       prev_pageno = pageno;
+               }
+
+               memberptr = (TransactionId *)
+                       MultiXactMemberCtl->shared->page_buffer[slotno];
+               memberptr += entryno;
+
+               *memberptr = xids[i];
+               MultiXactMemberCtl->shared->page_status[slotno] = SLRU_PAGE_DIRTY;
+       }
+
+       LWLockRelease(MultiXactMemberControlLock);
+
+       /* Store the new MultiXactId in the local cache, too */
+       mXactCachePut(multi, nxids, xids);
+       debug_elog2(DEBUG2, "Create: all done");
+
+       return multi;
+}
+
+/*
+ * GetNewMultiXactId
+ *             Get the next MultiXactId.
+ *
+ * Get the next MultiXactId, XLogging if needed.  Also, reserve the needed
+ * amount of space in the "members" area.  The starting offset of the
+ * reserved space is returned in *offset.
+ */
+static MultiXactId
+GetNewMultiXactId(int nxids, uint32 *offset)
+{
+       MultiXactId             result;
+
+       debug_elog3(DEBUG2, "GetNew: for %d xids", nxids);
+
+       /* MultiXactIdSetOldestMember() must have been called already */
+       Assert(MultiXactIdIsValid(OldestMemberMXactId[MyBackendId]));
+
+       LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
+
+       /* Handle wraparound of the nextMXact counter */
+       if (MultiXactState->nextMXact < FirstMultiXactId)
+       {
+               MultiXactState->nextMXact = FirstMultiXactId;
+               MultiXactState->mXactCount = 0;
+       }
+
+       /* If we run out of logged for use multixacts then we must log more */
+       if (MultiXactState->mXactCount == 0)
+       {
+               XLogPutNextMultiXactId(MultiXactState->nextMXact + MXACT_PREFETCH);
+               MultiXactState->mXactCount = MXACT_PREFETCH;
+       }
+
+       result = MultiXactState->nextMXact;
+
+       /*
+        * We don't care about MultiXactId wraparound here; it will be handled by
+        * the next iteration.  But note that nextMXact may be InvalidMultiXactId
+        * after this routine exits, so anyone else looking at the variable must
+        * be prepared to deal with that.
+        */
+       (MultiXactState->nextMXact)++;
+       (MultiXactState->mXactCount)--;
+
+       /*
+        * Reserve the members space.
+        */
+       *offset = MultiXactState->nextOffset;
+       MultiXactState->nextOffset += nxids;
+
+       LWLockRelease(MultiXactGenLock);
+
+       debug_elog4(DEBUG2, "GetNew: returning %u offset %u", result, *offset);
+       return result;
+}
+
+/*
+ * GetMultiXactIdMembers
+ *             Returns the set of TransactionIds that make up a MultiXactId
+ *
+ * We return -1 if the MultiXactId is too old to possibly have any members
+ * still running; in that case we have not actually looked them up, and
+ * *xids is not set.
+ */
+static int
+GetMultiXactIdMembers(MultiXactId multi, TransactionId **xids)
+{
+       int                     pageno;
+       int                     prev_pageno;
+       int                     entryno;
+       int                     slotno;
+       uint32     *offptr;
+       uint32          offset;
+       int                     length;
+       int                     i;
+       MultiXactId     nextMXact;
+       MultiXactId     tmpMXact;
+       uint32          nextOffset;
+       TransactionId *ptr;
+
+       debug_elog3(DEBUG2, "GetMembers: asked for %u", multi);
+
+       Assert(MultiXactIdIsValid(multi));
+
+       /* See if the MultiXactId is in the local cache */
+       length = mXactCacheGetById(multi, xids);
+       if (length >= 0)
+       {
+               debug_elog3(DEBUG2, "GetMembers: found %s in the cache",
+                                       mxid_to_string(multi, length, *xids));
+               return length;
+       }
+
+       /* Set our OldestVisibleMXactId[] entry if we didn't already */
+       MultiXactIdSetOldestVisible();
+
+       /*
+        * We check known limits on MultiXact before resorting to the SLRU area.
+        *
+        * An ID older than our OldestVisibleMXactId[] entry can't possibly still
+        * be running, and we'd run the risk of trying to read already-truncated
+        * SLRU data if we did try to examine it.
+        *
+        * Conversely, an ID >= nextMXact shouldn't ever be seen here; if it is
+        * seen, it implies undetected ID wraparound has occurred.  We just
+        * silently assume that such an ID is no longer running.
+        *
+        * Shared lock is enough here since we aren't modifying any global state.
+        * Also, we can examine our own OldestVisibleMXactId without the lock,
+        * since no one else is allowed to change it.
+        */
+       if (MultiXactIdPrecedes(multi, OldestVisibleMXactId[MyBackendId]))
+       {
+               debug_elog2(DEBUG2, "GetMembers: it's too old");
+               *xids = NULL;
+               return -1;
+       }
+
+       LWLockAcquire(MultiXactGenLock, LW_SHARED);
+
+       if (!MultiXactIdPrecedes(multi, MultiXactState->nextMXact))
+       {
+               LWLockRelease(MultiXactGenLock);
+               debug_elog2(DEBUG2, "GetMembers: it's too new!");
+               *xids = NULL;
+               return -1;
+       }
+
+       /*
+        * Before releasing the lock, save the current counter values, because
+        * the target MultiXactId may be just one less than nextMXact.  We will
+        * need to use nextOffset as the endpoint if so.
+        */
+       nextMXact = MultiXactState->nextMXact;
+       nextOffset = MultiXactState->nextOffset;
+
+       LWLockRelease(MultiXactGenLock);
+
+       /* Get the offset at which we need to start reading MultiXactMembers */
+       LWLockAcquire(MultiXactOffsetControlLock, LW_EXCLUSIVE);
+
+       pageno = MultiXactIdToOffsetPage(multi);
+       entryno = MultiXactIdToOffsetEntry(multi);
+
+       slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, multi);
+       offptr = (uint32 *) MultiXactOffsetCtl->shared->page_buffer[slotno];
+       offptr += entryno;
+       offset = *offptr;
+
+       /*
+        * How many members do we need to read?  If we are at the end of the
+        * assigned MultiXactIds, use the offset just saved above.  Else we
+        * need to check the MultiXactId following ours.
+        *
+        * Use the same increment rule as GetNewMultiXactId(), that is, don't
+        * handle wraparound explicitly until needed.
+        */
+       tmpMXact = multi + 1;
+
+       if (nextMXact == tmpMXact)
+               length = nextOffset - offset;
+       else
+       {
+               /* handle wraparound if needed */
+               if (tmpMXact < FirstMultiXactId)
+                       tmpMXact = FirstMultiXactId;
+
+               prev_pageno = pageno;
+
+               pageno = MultiXactIdToOffsetPage(tmpMXact);
+               entryno = MultiXactIdToOffsetEntry(tmpMXact);
+
+               if (pageno != prev_pageno)
+                       slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, tmpMXact);
+
+               offptr = (uint32 *) MultiXactOffsetCtl->shared->page_buffer[slotno];
+               offptr += entryno;
+               length = *offptr - offset;
+       }
+
+       LWLockRelease(MultiXactOffsetControlLock);
+
+       ptr = (TransactionId *) palloc(length * sizeof(TransactionId));
+       *xids = ptr;
+
+       /* Now get the members themselves. */
+       LWLockAcquire(MultiXactMemberControlLock, LW_EXCLUSIVE);
+
+       prev_pageno = -1;
+       for (i = 0; i < length; i++, offset++)
+       {
+               TransactionId *xactptr;
+
+               pageno = MXOffsetToMemberPage(offset);
+               entryno = MXOffsetToMemberEntry(offset);
+
+               if (pageno != prev_pageno)
+               {
+                       slotno = SimpleLruReadPage(MultiXactMemberCtl, pageno, multi);
+                       prev_pageno = pageno;
+               }
+
+               xactptr = (TransactionId *)
+                       MultiXactMemberCtl->shared->page_buffer[slotno];
+               xactptr += entryno;
+
+               ptr[i] = *xactptr;
+       }
+
+       LWLockRelease(MultiXactMemberControlLock);
+
+       /*
+        * Copy the result into the local cache.
+        */
+       mXactCachePut(multi, length, ptr);
+
+       debug_elog3(DEBUG2, "GetMembers: no cache for %s",
+                               mxid_to_string(multi, length, ptr));
+       return length;
+}
+
+/*
+ * mXactCacheGetBySet
+ *             returns a MultiXactId from the cache based on the set of
+ *             TransactionIds that compose it, or InvalidMultiXactId if
+ *             none matches.
+ *
+ * This is helpful, for example, if two transactions want to lock a huge
+ * table.  By using the cache, the second will use the same MultiXactId
+ * for the majority of tuples, thus keeping MultiXactId usage low (saving
+ * both I/O and wraparound issues).
+ *
+ * NB: the passed xids[] array will be sorted in-place.
+ */
+static MultiXactId
+mXactCacheGetBySet(int nxids, TransactionId *xids)
+{
+       mXactCacheEnt   *entry;
+
+       debug_elog3(DEBUG2, "CacheGet: looking for %s",
+                               mxid_to_string(InvalidMultiXactId, nxids, xids));
+
+       /* sort the array so comparison is easy */
+       qsort(xids, nxids, sizeof(TransactionId), xidComparator);
+
+       for (entry = MXactCache; entry != NULL; entry = entry->next)
+       {
+               if (entry->nxids != nxids)
+                       continue;
+
+               /* We assume the cache entries are sorted */
+               if (memcmp(xids, entry->xids, nxids * sizeof(TransactionId)) == 0)
+               {
+                       debug_elog3(DEBUG2, "CacheGet: found %u", entry->multi);
+                       return entry->multi;
+               }
+       }
+
+       debug_elog2(DEBUG2, "CacheGet: not found :-(");
+       return InvalidMultiXactId;
+}
+
+/*
+ * mXactCacheGetById
+ *             returns the composing TransactionId set from the cache for a
+ *             given MultiXactId, if present.
+ *
+ * If successful, *xids is set to the address of a palloc'd copy of the
+ * TransactionId set.  Return value is number of members, or -1 on failure.
+ */
+static int
+mXactCacheGetById(MultiXactId multi, TransactionId **xids)
+{
+       mXactCacheEnt   *entry;
+
+       debug_elog3(DEBUG2, "CacheGet: looking for %u", multi);
+
+       for (entry = MXactCache; entry != NULL; entry = entry->next)
+       {
+               if (entry->multi == multi)
+               {
+                       TransactionId   *ptr;
+                       Size            size;
+
+                       size = sizeof(TransactionId) * entry->nxids;
+                       ptr = (TransactionId *) palloc(size);
+                       *xids = ptr;
+
+                       memcpy(ptr, entry->xids, size);
+
+                       debug_elog3(DEBUG2, "CacheGet: found %s",
+                                       mxid_to_string(multi, entry->nxids, entry->xids));
+                       return entry->nxids;
+               }
+       }
+
+       debug_elog2(DEBUG2, "CacheGet: not found");
+       return -1;
+}
+
+/*
+ * mXactCachePut
+ *             Add a new MultiXactId and its composing set into the local cache.
+ */
+static void
+mXactCachePut(MultiXactId multi, int nxids, TransactionId *xids)
+{
+       mXactCacheEnt  *entry;
+
+       debug_elog3(DEBUG2, "CachePut: storing %s",
+                               mxid_to_string(multi, nxids, xids));
+
+       if (MXactContext == NULL)
+       {
+               /* The cache only lives as long as the current transaction */
+               debug_elog2(DEBUG2, "CachePut: initializing memory context");
+               MXactContext = AllocSetContextCreate(TopTransactionContext,
+                                                                                        "MultiXact Cache Context",
+                                                                                        ALLOCSET_SMALL_MINSIZE,
+                                                                                        ALLOCSET_SMALL_INITSIZE,
+                                                                                        ALLOCSET_SMALL_MAXSIZE);
+       }
+
+       entry = (mXactCacheEnt *)
+               MemoryContextAlloc(MXactContext,
+                                                  offsetof(mXactCacheEnt, xids) +
+                                                  nxids * sizeof(TransactionId));
+
+       entry->multi = multi;
+       entry->nxids = nxids;
+       memcpy(entry->xids, xids, nxids * sizeof(TransactionId));
+
+       /* mXactCacheGetBySet assumes the entries are sorted, so sort them */
+       qsort(entry->xids, nxids, sizeof(TransactionId), xidComparator);
+
+       entry->next = MXactCache;
+       MXactCache = entry;
+}
+
+/*
+ * xidComparator
+ *             qsort comparison function for XIDs
+ *
+ * We don't need to use wraparound comparison for XIDs, and indeed must
+ * not do so since that does not respect the triangle inequality!  Any
+ * old sort order will do.
+ */
+static int
+xidComparator(const void *arg1, const void *arg2)
+{
+       TransactionId xid1 = * (const TransactionId *) arg1;
+       TransactionId xid2 = * (const TransactionId *) arg2;
+
+       if (xid1 > xid2)
+               return 1;
+       if (xid1 < xid2)
+               return -1;
+       return 0;
+}
+
+#ifdef MULTIXACT_DEBUG
+static char *
+mxid_to_string(MultiXactId multi, int nxids, TransactionId *xids)
+{
+       char *str = palloc(15 * (nxids + 1) + 4);
+       int i;
+       snprintf(str, 47, "%u %d[%u", multi, nxids, xids[0]);
+
+       for (i = 1; i < nxids; i++)
+               snprintf(str + strlen(str), 17, ", %u", xids[i]);
+
+       strcat(str, "]");
+       return str;
+}
+#endif
+
+/*
+ * AtEOXact_MultiXact
+ *             Handle transaction end for MultiXact
+ *
+ * This is called at top transaction commit or abort (we don't care which).
+ */
+void
+AtEOXact_MultiXact(void)
+{
+       /*
+        * Reset our OldestMemberMXactId and OldestVisibleMXactId values,
+        * both of which should only be valid while within a transaction.
+        *
+        * We assume that storing a MultiXactId is atomic and so we need
+        * not take MultiXactGenLock to do this.
+        */
+       OldestMemberMXactId[MyBackendId] = InvalidMultiXactId;
+       OldestVisibleMXactId[MyBackendId] = InvalidMultiXactId;
+
+       /*
+        * Discard the local MultiXactId cache.  Since MXactContext was created
+        * as a child of TopTransactionContext, we needn't delete it explicitly.
+        */
+       MXactContext = NULL;
+       MXactCache = NULL;
+}
+
+/*
+ * Initialization of shared memory for MultiXact.  We use two SLRU areas,
+ * thus double memory.  Also, reserve space for the shared MultiXactState
+ * struct and the per-backend MultiXactId arrays (two of those, too).
+ */
+int
+MultiXactShmemSize(void)
+{
+#define SHARED_MULTIXACT_STATE_SIZE \
+       (sizeof(MultiXactStateData) + sizeof(MultiXactId) * 2 * MaxBackends)
+
+       return (SimpleLruShmemSize() * 2 + SHARED_MULTIXACT_STATE_SIZE);
+}
+
+void
+MultiXactShmemInit(void)
+{
+       bool    found;
+
+       debug_elog2(DEBUG2, "Shared Memory Init for MultiXact");
+
+       MultiXactOffsetCtl->PagePrecedes = MultiXactOffsetPagePrecedes;
+       MultiXactMemberCtl->PagePrecedes = MultiXactMemberPagePrecedes;
+
+       SimpleLruInit(MultiXactOffsetCtl, "MultiXactOffset Ctl",
+                                 MultiXactOffsetControlLock, "pg_multixact/offsets");
+       SimpleLruInit(MultiXactMemberCtl, "MultiXactMember Ctl",
+                                 MultiXactMemberControlLock, "pg_multixact/members");
+
+       /* Override default assumption that writes should be fsync'd */
+       MultiXactOffsetCtl->do_fsync = false;
+       MultiXactMemberCtl->do_fsync = false;
+
+       /* Initialize our shared state struct */
+       MultiXactState = ShmemInitStruct("Shared MultiXact State",
+                                                                        SHARED_MULTIXACT_STATE_SIZE,
+                                                                        &found);
+       if (!IsUnderPostmaster)
+       {
+               Assert(!found);
+
+               /* Make sure we zero out the per-backend state */
+               MemSet(MultiXactState, 0, SHARED_MULTIXACT_STATE_SIZE);
+       }
+       else
+               Assert(found);
+
+       /*
+        * Set up array pointers.  Note that perBackendXactIds[0] is wasted
+        * space since we only use indexes 1..MaxBackends in each array.
+        */
+       OldestMemberMXactId = MultiXactState->perBackendXactIds;
+       OldestVisibleMXactId = OldestMemberMXactId + MaxBackends;
+}
+
+/*
+ * This func must be called ONCE on system install.  It creates the initial
+ * MultiXact segments.  (The MultiXacts directories are assumed to have been
+ * created by initdb, and MultiXactShmemInit must have been called already.)
+ *
+ * Note: it's not really necessary to create the initial segments now,
+ * since slru.c would create 'em on first write anyway.        But we may as well
+ * do it to be sure the directories are set up correctly.
+ */
+void
+BootStrapMultiXact(void)
+{
+       int                     slotno;
+
+       LWLockAcquire(MultiXactOffsetControlLock, LW_EXCLUSIVE);
+
+       /* Offsets first page */
+       slotno = ZeroMultiXactOffsetPage(0);
+       SimpleLruWritePage(MultiXactOffsetCtl, slotno, NULL);
+       Assert(MultiXactOffsetCtl->shared->page_status[slotno] == SLRU_PAGE_CLEAN);
+
+       LWLockRelease(MultiXactOffsetControlLock);
+
+       LWLockAcquire(MultiXactMemberControlLock, LW_EXCLUSIVE);
+
+       /* Members first page */
+       slotno = ZeroMultiXactMemberPage(0);
+       SimpleLruWritePage(MultiXactMemberCtl, slotno, NULL);
+       Assert(MultiXactMemberCtl->shared->page_status[slotno] == SLRU_PAGE_CLEAN);
+
+       LWLockRelease(MultiXactMemberControlLock);
+}
+
+/*
+ * Initialize (or reinitialize) a page of MultiXactOffset to zeroes.
+ *
+ * The page is not actually written, just set up in shared memory.
+ * The slot number of the new page is returned.
+ *
+ * Control lock must be held at entry, and will be held at exit.
+ */
+static int
+ZeroMultiXactOffsetPage(int pageno)
+{
+       return SimpleLruZeroPage(MultiXactOffsetCtl, pageno);
+}
+
+/*
+ * Ditto, for MultiXactMember
+ */
+static int
+ZeroMultiXactMemberPage(int pageno)
+{
+       return SimpleLruZeroPage(MultiXactMemberCtl, pageno);
+}
+
+/*
+ * This must be called ONCE during postmaster or standalone-backend startup.
+ *
+ * StartupXLOG has already established nextMXact by calling
+ * MultiXactSetNextMXact and/or MultiXactAdvanceNextMXact.
+ *
+ * We don't need any locks here, really; the SLRU locks are taken
+ * only because slru.c expects to be called with locks held.
+ */
+void
+StartupMultiXact(void)
+{
+       int                     startPage;
+       int                     cutoffPage;
+       uint32          offset;
+
+       /*
+        * We start nextOffset at zero after every reboot; there is no need to
+        * avoid offset values that were used in the previous system lifecycle.
+        */
+       MultiXactState->nextOffset = 0;
+
+       /*
+        * Because of the above, a shutdown and restart is likely to leave
+        * high-numbered MultiXactMember page files that would not get recycled
+        * for a long time (about as long as the system had been up in the
+        * previous cycle of life).  To clean out such page files, we issue an
+        * artificial truncation call that will zap any page files in the first
+        * half of the offset cycle.  Should there be any page files in the last
+        * half, they will get cleaned out by the first checkpoint.
+        *
+        * XXX it might be a good idea to disable this when debugging, since it
+        * will tend to destroy evidence after a crash.  To not be *too* ruthless,
+        * we arbitrarily spare the first 64 pages.  (Note this will get
+        * rounded off to a multiple of SLRU_PAGES_PER_SEGMENT ...)
+        */
+       offset = ((~ (uint32) 0) >> 1) + 1;
+
+       cutoffPage = MXOffsetToMemberPage(offset) + 64;
+
+       /*
+        * Defeat safety interlock in SimpleLruTruncate; this hack will be
+        * cleaned up by ZeroMultiXactMemberPage call below.
+        */
+       MultiXactMemberCtl->shared->latest_page_number = cutoffPage;
+
+       SimpleLruTruncate(MultiXactMemberCtl, cutoffPage);
+
+       /*
+        * Initialize lastTruncationPoint to invalid, ensuring that the first
+        * checkpoint will try to do truncation.
+        */
+       MultiXactState->lastTruncationPoint = InvalidMultiXactId;
+
+       /*
+        * Since we don't expect MultiXact to be valid across crashes, we
+        * initialize the currently-active pages to zeroes during startup.
+        * Whenever we advance into a new page, both ExtendMultiXact routines
+        * will likewise zero the new page without regard to whatever was
+        * previously on disk.
+        */
+       LWLockAcquire(MultiXactOffsetControlLock, LW_EXCLUSIVE);
+
+       startPage = MultiXactIdToOffsetPage(MultiXactState->nextMXact);
+       (void) ZeroMultiXactOffsetPage(startPage);
+
+       LWLockRelease(MultiXactOffsetControlLock);
+
+       LWLockAcquire(MultiXactMemberControlLock, LW_EXCLUSIVE);
+
+       startPage = MXOffsetToMemberPage(MultiXactState->nextOffset);
+       (void) ZeroMultiXactMemberPage(startPage);
+
+       LWLockRelease(MultiXactMemberControlLock);
+}
+
+/*
+ * This must be called ONCE during postmaster or standalone-backend shutdown
+ */
+void
+ShutdownMultiXact(void)
+{
+       /*
+        * Flush dirty MultiXact pages to disk
+        *
+        * This is not actually necessary from a correctness point of view. We do
+        * it merely as a debugging aid.
+        */
+       SimpleLruFlush(MultiXactOffsetCtl, false);
+       SimpleLruFlush(MultiXactMemberCtl, false);
+}
+
+/*
+ * Get the next MultiXactId to save in a checkpoint record
+ */
+MultiXactId
+MultiXactGetCheckptMulti(bool is_shutdown)
+{
+       MultiXactId     retval;
+
+       LWLockAcquire(MultiXactGenLock, LW_SHARED);
+
+       retval = MultiXactState->nextMXact;
+       if (!is_shutdown)
+               retval += MultiXactState->mXactCount;
+
+       LWLockRelease(MultiXactGenLock);
+
+       debug_elog3(DEBUG2, "MultiXact: MultiXact for checkpoint record is %u",
+                               retval);
+
+       return retval;
+}
+
+/*
+ * Perform a checkpoint --- either during shutdown, or on-the-fly
+ */
+void
+CheckPointMultiXact(void)
+{
+       /*
+        * Flush dirty MultiXact pages to disk
+        *
+        * This is not actually necessary from a correctness point of view. We do
+        * it merely to improve the odds that writing of dirty pages is done
+        * by the checkpoint process and not by backends.
+        */
+       SimpleLruFlush(MultiXactOffsetCtl, true);
+       SimpleLruFlush(MultiXactMemberCtl, true);
+
+       /*
+        * Truncate the SLRU files
+        */
+       TruncateMultiXact();
+}
+
+/*
+ * Set the next-to-be-assigned MultiXactId
+ *
+ * This is used when we can determine the correct next Id exactly
+ * from an XLog record.  We need no locking since it is only called
+ * during bootstrap and XLog replay.
+ */
+void
+MultiXactSetNextMXact(MultiXactId nextMulti)
+{
+       debug_elog3(DEBUG2, "MultiXact: setting next multi to %u", nextMulti);
+       MultiXactState->nextMXact = nextMulti;
+       MultiXactState->mXactCount = 0;
+}
+
+/*
+ * Ensure the next-to-be-assigned MultiXactId is at least minMulti
+ *
+ * This is used when we can determine a minimum safe value
+ * from an XLog record.  We need no locking since it is only called
+ * during XLog replay.
+ */
+void
+MultiXactAdvanceNextMXact(MultiXactId minMulti)
+{
+       if (MultiXactIdPrecedes(MultiXactState->nextMXact, minMulti))
+       {
+               debug_elog3(DEBUG2, "MultiXact: setting next multi to %u", minMulti);
+               MultiXactState->nextMXact = minMulti;
+               MultiXactState->mXactCount = 0;
+       }
+}
+
+/*
+ * Make sure that MultiXactOffset has room for a newly-allocated MultiXactId.
+ *
+ * The MultiXactOffsetControlLock should be held at entry, and will
+ * be held at exit.
+ */
+void
+ExtendMultiXactOffset(MultiXactId multi)
+{
+       int                     pageno;
+
+       /*
+        * No work except at first MultiXactId of a page.  But beware: just after
+        * wraparound, the first MultiXactId of page zero is FirstMultiXactId.
+        */
+       if (MultiXactIdToOffsetEntry(multi) != 0 &&
+               multi != FirstMultiXactId)
+               return;
+
+       pageno = MultiXactIdToOffsetPage(multi);
+
+       /* Zero the page */
+       ZeroMultiXactOffsetPage(pageno);
+}
+
+/*
+ * Make sure that MultiXactMember has room for the members of a newly-
+ * allocated MultiXactId.
+ *
+ * The MultiXactMemberControlLock should be held at entry, and will be held
+ * at exit.
+ */
+void
+ExtendMultiXactMember(uint32 offset)
+{
+       int             pageno;
+
+       /*
+        * No work except at first entry of a page.
+        */
+       if (MXOffsetToMemberEntry(offset) != 0)
+               return;
+
+       pageno = MXOffsetToMemberPage(offset);
+
+       /* Zero the page */
+       ZeroMultiXactMemberPage(pageno);
+}
+
+/*
+ * Remove all MultiXactOffset and MultiXactMember segments before the oldest
+ * ones still of interest.
+ *
+ * This is called only during checkpoints.  We assume no more than one
+ * backend does this at a time.
+ */
+static void
+TruncateMultiXact(void)
+{
+       MultiXactId nextMXact;
+       uint32          nextOffset;
+       MultiXactId oldestMXact;
+       uint32          oldestOffset;
+       int                     cutoffPage;
+       int                     i;
+
+       /*
+        * First, compute where we can safely truncate.  Per notes above,
+        * this is the oldest valid value among all the OldestMemberMXactId[] and
+        * OldestVisibleMXactId[] entries, or nextMXact if none are valid.
+        */
+       LWLockAcquire(MultiXactGenLock, LW_SHARED);
+
+       /*
+        * We have to beware of the possibility that nextMXact is in the
+        * wrapped-around state.  We don't fix the counter itself here,
+        * but we must be sure to use a valid value in our calculation.
+        */
+       nextMXact = MultiXactState->nextMXact;
+       if (nextMXact < FirstMultiXactId)
+               nextMXact = FirstMultiXactId;
+
+       oldestMXact = nextMXact;
+       for (i = 1; i <= MaxBackends; i++)
+       {
+               MultiXactId thisoldest;
+
+               thisoldest = OldestMemberMXactId[i];
+               if (MultiXactIdIsValid(thisoldest) &&
+                       MultiXactIdPrecedes(thisoldest, oldestMXact))
+                       oldestMXact = thisoldest;
+               thisoldest = OldestVisibleMXactId[i];
+               if (MultiXactIdIsValid(thisoldest) &&
+                       MultiXactIdPrecedes(thisoldest, oldestMXact))
+                       oldestMXact = thisoldest;
+       }
+
+       /* Save the current nextOffset too */
+       nextOffset = MultiXactState->nextOffset;
+
+       LWLockRelease(MultiXactGenLock);
+
+       debug_elog3(DEBUG2, "MultiXact: truncation point = %u", oldestMXact);
+
+       /*
+        * If we already truncated at this point, do nothing.  This saves time
+        * when no MultiXacts are getting used, which is probably not uncommon.
+        */
+       if (MultiXactState->lastTruncationPoint == oldestMXact)
+               return;
+
+       /*
+        * We need to determine where to truncate MultiXactMember.  If we
+        * found a valid oldest MultiXactId, read its starting offset;
+        * otherwise we use the nextOffset value we saved above.
+        */
+       if (oldestMXact == nextMXact)
+               oldestOffset = nextOffset;
+       else
+       {
+               int                     pageno;
+               int                     slotno;
+               int                     entryno;
+               uint32     *offptr;
+
+               LWLockAcquire(MultiXactOffsetControlLock, LW_EXCLUSIVE);
+
+               pageno = MultiXactIdToOffsetPage(oldestMXact);
+               entryno = MultiXactIdToOffsetEntry(oldestMXact);
+
+               slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, oldestMXact);
+               offptr = (uint32 *) MultiXactOffsetCtl->shared->page_buffer[slotno];
+               offptr += entryno;
+               oldestOffset = *offptr;
+
+               LWLockRelease(MultiXactOffsetControlLock);
+       }
+
+       /*
+        * The cutoff point is the start of the segment containing oldestMXact.
+        * We pass the *page* containing oldestMXact to SimpleLruTruncate.
+        */
+       cutoffPage = MultiXactIdToOffsetPage(oldestMXact);
+
+       SimpleLruTruncate(MultiXactOffsetCtl, cutoffPage);
+
+       /*
+        * Also truncate MultiXactMember at the previously determined offset.
+        */
+       cutoffPage = MXOffsetToMemberPage(oldestOffset);
+
+       SimpleLruTruncate(MultiXactMemberCtl, cutoffPage);
+
+       /*
+        * Set the last known truncation point.  We don't need a lock for this
+        * since only one backend does checkpoints at a time.
+        */
+       MultiXactState->lastTruncationPoint = oldestMXact;
+}
+
+/*
+ * Decide which of two MultiXactOffset page numbers is "older" for truncation
+ * purposes.
+ *
+ * We need to use comparison of MultiXactId here in order to do the right
+ * thing with wraparound.  However, if we are asked about page number zero, we
+ * don't want to hand InvalidMultiXactId to MultiXactIdPrecedes: it'll get
+ * weird.  So, offset both multis by FirstMultiXactId to avoid that.
+ * (Actually, the current implementation doesn't do anything weird with
+ * InvalidMultiXactId, but there's no harm in leaving this code like this.)
+ */
+static bool
+MultiXactOffsetPagePrecedes(int page1, int page2)
+{
+       MultiXactId multi1;
+       MultiXactId multi2;
+
+       multi1 = ((MultiXactId) page1) * MULTIXACT_OFFSETS_PER_PAGE;
+       multi1 += FirstMultiXactId;
+       multi2 = ((MultiXactId) page2) * MULTIXACT_OFFSETS_PER_PAGE;
+       multi2 += FirstMultiXactId;
+
+       return MultiXactIdPrecedes(multi1, multi2);
+}
+
+/*
+ * Decide which of two MultiXactMember page numbers is "older" for truncation
+ * purposes.  There is no "invalid offset number" so use the numbers verbatim.
+ */
+static bool
+MultiXactMemberPagePrecedes(int page1, int page2)
+{
+       uint32  offset1;
+       uint32  offset2;
+
+       offset1 = ((uint32) page1) * MULTIXACT_MEMBERS_PER_PAGE;
+       offset2 = ((uint32) page2) * MULTIXACT_MEMBERS_PER_PAGE;
+
+       return MultiXactOffsetPrecedes(offset1, offset2);
+}
+
+/*
+ * Decide which of two MultiXactIds is earlier.
+ *
+ * XXX do we need to do something special for InvalidMultiXactId?
+ * (Doesn't look like it.)
+ */
+static bool
+MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
+{
+       int32 diff = (int32) (multi1 - multi2);
+
+       return (diff < 0);
+}
+
+/*
+ * Decide which of two offsets is earlier.
+ */
+static bool
+MultiXactOffsetPrecedes(uint32 offset1, uint32 offset2)
+{
+       int32 diff = (int32) (offset1 - offset2);
+
+       return (diff < 0);
+}
index 4d896ef551da662342c340f595d1f4a1d708f21c..a318db613434a79c8555b0c4c5f27c3138826b8b 100644 (file)
@@ -10,7 +10,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.199 2005/04/11 19:51:14 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.200 2005/04/28 21:47:10 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -20,6 +20,7 @@
 #include <time.h>
 #include <unistd.h>
 
+#include "access/multixact.h"
 #include "access/subtrans.h"
 #include "access/xact.h"
 #include "catalog/heap.h"
@@ -1565,6 +1566,8 @@ CommitTransaction(void)
         */
        smgrDoPendingDeletes(true);
 
+       AtEOXact_MultiXact();
+
        ResourceOwnerRelease(TopTransactionResourceOwner,
                                                 RESOURCE_RELEASE_LOCKS,
                                                 true, true);
@@ -1710,6 +1713,7 @@ AbortTransaction(void)
        AtEOXact_Buffers(false);
        AtEOXact_Inval(false);
        smgrDoPendingDeletes(false);
+       AtEOXact_MultiXact();
        ResourceOwnerRelease(TopTransactionResourceOwner,
                                                 RESOURCE_RELEASE_LOCKS,
                                                 false, true);
@@ -3622,9 +3626,9 @@ static void
 ShowTransactionState(const char *str)
 {
        /* skip work if message will definitely not be printed */
-       if (log_min_messages <= DEBUG2 || client_min_messages <= DEBUG2)
+       if (log_min_messages <= DEBUG3 || client_min_messages <= DEBUG3)
        {
-               elog(DEBUG2, "%s", str);
+               elog(DEBUG3, "%s", str);
                ShowTransactionStateRec(CurrentTransactionState);
        }
 }
@@ -3640,7 +3644,7 @@ ShowTransactionStateRec(TransactionState s)
                ShowTransactionStateRec(s->parent);
 
        /* use ereport to suppress computation if msg will not be printed */
-       ereport(DEBUG2,
+       ereport(DEBUG3,
                        (errmsg_internal("name: %s; blockState: %13s; state: %7s, xid/subid/cid: %u/%u/%u, nestlvl: %d, children: %s",
                                                   PointerIsValid(s->name) ? s->name : "unnamed",
                                                         BlockStateAsString(s->blockState),
index f384bbbe7161c6984b338ab92e143ec287f68a74..48e8b425deee5912234618553b4652392e971c3f 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.188 2005/04/23 18:49:54 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.189 2005/04/28 21:47:10 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -23,6 +23,7 @@
 #include <sys/time.h>
 
 #include "access/clog.h"
+#include "access/multixact.h"
 #include "access/subtrans.h"
 #include "access/xact.h"
 #include "access/xlog.h"
@@ -3534,11 +3535,13 @@ BootStrapXLOG(void)
        checkPoint.ThisTimeLineID = ThisTimeLineID;
        checkPoint.nextXid = FirstNormalTransactionId;
        checkPoint.nextOid = FirstBootstrapObjectId;
+       checkPoint.nextMulti = FirstMultiXactId;
        checkPoint.time = time(NULL);
 
        ShmemVariableCache->nextXid = checkPoint.nextXid;
        ShmemVariableCache->nextOid = checkPoint.nextOid;
        ShmemVariableCache->oidCount = 0;
+       MultiXactSetNextMXact(checkPoint.nextMulti);
 
        /* Set up the XLOG page header */
        page->xlp_magic = XLOG_PAGE_MAGIC;
@@ -3613,6 +3616,7 @@ BootStrapXLOG(void)
        /* Bootstrap the commit log, too */
        BootStrapCLOG();
        BootStrapSUBTRANS();
+       BootStrapMultiXact();
 }
 
 static char *
@@ -4187,8 +4191,8 @@ StartupXLOG(void)
                                        checkPoint.undo.xlogid, checkPoint.undo.xrecoff,
                                        wasShutdown ? "TRUE" : "FALSE")));
        ereport(LOG,
-                       (errmsg("next transaction ID: %u; next OID: %u",
-                                       checkPoint.nextXid, checkPoint.nextOid)));
+                       (errmsg("next transaction ID: %u; next OID: %u; next MultiXactId: %u",
+                                       checkPoint.nextXid, checkPoint.nextOid, checkPoint.nextMulti)));
        if (!TransactionIdIsNormal(checkPoint.nextXid))
                ereport(PANIC,
                                (errmsg("invalid next transaction ID")));
@@ -4196,6 +4200,7 @@ StartupXLOG(void)
        ShmemVariableCache->nextXid = checkPoint.nextXid;
        ShmemVariableCache->nextOid = checkPoint.nextOid;
        ShmemVariableCache->oidCount = 0;
+       MultiXactSetNextMXact(checkPoint.nextMulti);
 
        /*
         * We must replay WAL entries using the same TimeLineID they were
@@ -4546,9 +4551,10 @@ StartupXLOG(void)
        ControlFile->time = time(NULL);
        UpdateControlFile();
 
-       /* Start up the commit log, too */
+       /* Start up the commit log and related stuff, too */
        StartupCLOG();
        StartupSUBTRANS();
+       StartupMultiXact();
 
        ereport(LOG,
                        (errmsg("database system is ready")));
@@ -4737,6 +4743,7 @@ ShutdownXLOG(int code, Datum arg)
        CreateCheckPoint(true, true);
        ShutdownCLOG();
        ShutdownSUBTRANS();
+       ShutdownMultiXact();
        CritSectionCount--;
 
        ereport(LOG,
@@ -4919,6 +4926,8 @@ CreateCheckPoint(bool shutdown, bool force)
                checkPoint.nextOid += ShmemVariableCache->oidCount;
        LWLockRelease(OidGenLock);
 
+       checkPoint.nextMulti = MultiXactGetCheckptMulti(shutdown);
+
        /*
         * Having constructed the checkpoint record, ensure all shmem disk
         * buffers and commit-log buffers are flushed to disk.
@@ -4938,6 +4947,7 @@ CreateCheckPoint(bool shutdown, bool force)
 
        CheckPointCLOG();
        CheckPointSUBTRANS();
+       CheckPointMultiXact();
        FlushBufferPool();
 
        START_CRIT_SECTION();
@@ -5054,6 +5064,33 @@ XLogPutNextOid(Oid nextOid)
        rdata.len = sizeof(Oid);
        rdata.next = NULL;
        (void) XLogInsert(RM_XLOG_ID, XLOG_NEXTOID, &rdata);
+       /*
+        * We need not flush the NEXTOID record immediately, because any of the
+        * just-allocated OIDs could only reach disk as part of a tuple insert
+        * or update that would have its own XLOG record that must follow the
+        * NEXTOID record.  Therefore, the standard buffer LSN interlock applied
+        * to those records will ensure no such OID reaches disk before the
+        * NEXTOID record does.
+        */
+}
+
+/*
+ * Write a NEXT_MULTIXACT log record
+ */
+void
+XLogPutNextMultiXactId(MultiXactId nextMulti)
+{
+       XLogRecData rdata;
+
+       rdata.buffer = InvalidBuffer;
+       rdata.data = (char *) (&nextMulti);
+       rdata.len = sizeof(MultiXactId);
+       rdata.next = NULL;
+       (void) XLogInsert(RM_XLOG_ID, XLOG_NEXTMULTI, &rdata);
+       /*
+        * We do not flush here either; this assumes that heap_lock_tuple() will
+        * always generate a WAL record.  See notes therein.
+        */
 }
 
 /*
@@ -5075,6 +5112,14 @@ xlog_redo(XLogRecPtr lsn, XLogRecord *record)
                        ShmemVariableCache->oidCount = 0;
                }
        }
+       else if (info == XLOG_NEXTMULTI)
+       {
+               MultiXactId     nextMulti;
+
+               memcpy(&nextMulti, XLogRecGetData(record), sizeof(MultiXactId));
+
+               MultiXactAdvanceNextMXact(nextMulti);
+       }
        else if (info == XLOG_CHECKPOINT_SHUTDOWN)
        {
                CheckPoint      checkPoint;
@@ -5084,6 +5129,7 @@ xlog_redo(XLogRecPtr lsn, XLogRecord *record)
                ShmemVariableCache->nextXid = checkPoint.nextXid;
                ShmemVariableCache->nextOid = checkPoint.nextOid;
                ShmemVariableCache->oidCount = 0;
+               MultiXactSetNextMXact(checkPoint.nextMulti);
 
                /*
                 * TLI may change in a shutdown checkpoint, but it shouldn't
@@ -5115,6 +5161,7 @@ xlog_redo(XLogRecPtr lsn, XLogRecord *record)
                        ShmemVariableCache->nextOid = checkPoint.nextOid;
                        ShmemVariableCache->oidCount = 0;
                }
+               MultiXactAdvanceNextMXact(checkPoint.nextMulti);
                /* TLI should not change in an on-line checkpoint */
                if (checkPoint.ThisTimeLineID != ThisTimeLineID)
                        ereport(PANIC,
@@ -5139,11 +5186,12 @@ xlog_desc(char *buf, uint8 xl_info, char *rec)
                CheckPoint *checkpoint = (CheckPoint *) rec;
 
                sprintf(buf + strlen(buf), "checkpoint: redo %X/%X; undo %X/%X; "
-                               "tli %u; xid %u; oid %u; %s",
+                               "tli %u; xid %u; oid %u; multi %u; %s",
                                checkpoint->redo.xlogid, checkpoint->redo.xrecoff,
                                checkpoint->undo.xlogid, checkpoint->undo.xrecoff,
                                checkpoint->ThisTimeLineID, checkpoint->nextXid,
                                checkpoint->nextOid,
+                               checkpoint->nextMulti,
                         (info == XLOG_CHECKPOINT_SHUTDOWN) ? "shutdown" : "online");
        }
        else if (info == XLOG_NEXTOID)
@@ -5153,6 +5201,13 @@ xlog_desc(char *buf, uint8 xl_info, char *rec)
                memcpy(&nextOid, rec, sizeof(Oid));
                sprintf(buf + strlen(buf), "nextOid: %u", nextOid);
        }
+       else if (info == XLOG_NEXTMULTI)
+       {
+               MultiXactId     multi;
+
+               memcpy(&multi, rec, sizeof(MultiXactId));
+               sprintf(buf + strlen(buf), "nextMultiXact: %u", multi);
+       }
        else
                strcat(buf, "UNKNOWN");
 }
index e3f3ab67045752af8c1abcccd7fbd051c9931f60..b18d07878d0e022d06962fa169ce783f236cb661 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/catalog/index.c,v 1.252 2005/04/14 20:03:23 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/catalog/index.c,v 1.253 2005/04/28 21:47:11 tgl Exp $
  *
  *
  * INTERFACE ROUTINES
@@ -471,7 +471,7 @@ index_create(Oid heapRelationId,
        int                     i;
 
        /*
-        * Only SELECT ... FOR UPDATE are allowed while doing this
+        * Only SELECT ... FOR UPDATE/SHARE are allowed while doing this
         */
        heapRelation = heap_open(heapRelationId, ShareLock);
 
@@ -1460,6 +1460,7 @@ IndexBuildHeapScan(Relation heapRelation,
                                         * a system catalog, because we often release lock on
                                         * system catalogs before committing.
                                         */
+                                       Assert(!(heapTuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI));
                                        if (!TransactionIdIsCurrentTransactionId(
                                                           HeapTupleHeaderGetXmax(heapTuple->t_data))
                                                && !IsSystemRelation(heapRelation))
index b3170499adc888690dbadb11e1234f24a99ff1cd..affbe2b3e4998b579fc48f46a9718876b7cc107c 100644 (file)
@@ -14,7 +14,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/commands/portalcmds.c,v 1.40 2005/04/11 15:59:34 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/commands/portalcmds.c,v 1.41 2005/04/28 21:47:11 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -90,7 +90,7 @@ PerformCursorOpen(DeclareCursorStmt *stmt, ParamListInfo params)
        if (query->rowMarks != NIL)
                ereport(ERROR,
                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                                errmsg("DECLARE CURSOR ... FOR UPDATE is not supported"),
+                                errmsg("DECLARE CURSOR ... FOR UPDATE/SHARE is not supported"),
                                 errdetail("Cursors must be READ ONLY.")));
 
        plan = planner(query, true, stmt->options, NULL);
index c298cba3047e68e1d088d549803ae472c89995dd..70759b9d76a202039852539052d5fb7fda7ddeac 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/commands/trigger.c,v 1.186 2005/04/14 20:03:24 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/commands/trigger.c,v 1.187 2005/04/28 21:47:11 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1592,12 +1592,12 @@ GetTupleForTrigger(EState *estate, ResultRelInfo *relinfo,
                HTSU_Result     test;
 
                /*
-                * mark tuple for update
+                * lock tuple for update
                 */
                *newSlot = NULL;
                tuple.t_self = *tid;
 ltrmark:;
-               test = heap_mark4update(relation, &tuple, &buffer, cid);
+               test = heap_lock_tuple(relation, &tuple, &buffer, cid, LockTupleExclusive);
                switch (test)
                {
                        case HeapTupleSelfUpdated:
@@ -1636,8 +1636,7 @@ ltrmark:;
 
                        default:
                                ReleaseBuffer(buffer);
-                               elog(ERROR, "unrecognized heap_mark4update status: %u",
-                                        test);
+                               elog(ERROR, "invalid heap_lock_tuple status: %d", test);
                                return NULL;    /* keep compiler quiet */
                }
        }
index f52e9ecb3c2a2843f32ce4702ee19a31bc6bf920..ba4328875718ae14bed77c8c73dd5598cc30ea3b 100644 (file)
@@ -13,7 +13,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.306 2005/04/14 20:03:24 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.307 2005/04/28 21:47:12 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1800,7 +1800,7 @@ repair_frag(VRelStats *vacrelstats, Relation onerel,
                         !TransactionIdPrecedes(HeapTupleHeaderGetXmin(tuple.t_data),
                                                                        OldestXmin)) ||
                                (!(tuple.t_data->t_infomask & (HEAP_XMAX_INVALID |
-                                                                                          HEAP_MARKED_FOR_UPDATE)) &&
+                                                                                          HEAP_IS_LOCKED)) &&
                                 !(ItemPointerEquals(&(tuple.t_self),
                                                                         &(tuple.t_data->t_ctid)))))
                        {
@@ -1839,7 +1839,7 @@ repair_frag(VRelStats *vacrelstats, Relation onerel,
                                 * we have to move to the end of chain.
                                 */
                                while (!(tp.t_data->t_infomask & (HEAP_XMAX_INVALID |
-                                                                                         HEAP_MARKED_FOR_UPDATE)) &&
+                                                                                                 HEAP_IS_LOCKED)) &&
                                           !(ItemPointerEquals(&(tp.t_self),
                                                                                   &(tp.t_data->t_ctid))))
                                {
@@ -1984,7 +1984,8 @@ repair_frag(VRelStats *vacrelstats, Relation onerel,
                                         * and we are too close to 6.5 release. - vadim
                                         * 06/11/99
                                         */
-                                       if (!(TransactionIdEquals(HeapTupleHeaderGetXmax(Ptp.t_data),
+                                       if (Ptp.t_data->t_infomask & HEAP_XMAX_IS_MULTI ||
+                                               !(TransactionIdEquals(HeapTupleHeaderGetXmax(Ptp.t_data),
                                                                         HeapTupleHeaderGetXmin(tp.t_data))))
                                        {
                                                ReleaseBuffer(Pbuf);
index 0d3e16b6d9a69ce2609b5215ad473ad943b225bd..00e503744e4e52cb623b6d3bb436ed2296c7caa8 100644 (file)
@@ -1,4 +1,4 @@
-$PostgreSQL: pgsql/src/backend/executor/README,v 1.4 2003/11/29 19:51:48 pgsql Exp $
+$PostgreSQL: pgsql/src/backend/executor/README,v 1.5 2005/04/28 21:47:12 tgl Exp $
 
 The Postgres Executor
 ---------------------
@@ -154,8 +154,8 @@ committed by the concurrent transaction (after waiting for it to commit,
 if need be) and re-evaluate the query qualifications to see if it would
 still meet the quals.  If so, we regenerate the updated tuple (if we are
 doing an UPDATE) from the modified tuple, and finally update/delete the
-modified tuple.  SELECT FOR UPDATE behaves similarly, except that its action
-is just to mark the modified tuple for update by the current transaction.
+modified tuple.  SELECT FOR UPDATE/SHARE behaves similarly, except that its
+action is just to lock the modified tuple.
 
 To implement this checking, we actually re-run the entire query from scratch
 for each modified tuple, but with the scan node that sourced the original
@@ -184,14 +184,14 @@ that while we are executing a recheck query for one modified tuple, we will
 hit another modified tuple in another relation.  In this case we "stack up"
 recheck queries: a sub-recheck query is spawned in which both the first and
 second modified tuples will be returned as the only components of their
-relations.  (In event of success, all these modified tuples will be marked
-for update.)  Again, this isn't necessarily quite the right thing ... but in
-simple cases it works.  Potentially, recheck queries could get nested to the
-depth of the number of FOR UPDATE relations in the query.
+relations.  (In event of success, all these modified tuples will be locked.)
+Again, this isn't necessarily quite the right thing ... but in simple cases
+it works.  Potentially, recheck queries could get nested to the depth of the
+number of FOR UPDATE/SHARE relations in the query.
 
 It should be noted also that UPDATE/DELETE expect at most one tuple to
 result from the modified query, whereas in the FOR UPDATE case it's possible
 for multiple tuples to result (since we could be dealing with a join in
 which multiple tuples join to the modified tuple).  We want FOR UPDATE to
-mark all relevant tuples, so we pass all tuples output by all the stacked
-recheck queries back to the executor toplevel for marking.
+lock all relevant tuples, so we pass all tuples output by all the stacked
+recheck queries back to the executor toplevel for locking.
index 8facabbadad228b6e628d849c009ea715b147aac..3e2b986034ce476b664245e9274eeb8646f93b67 100644 (file)
@@ -26,7 +26,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/executor/execMain.c,v 1.246 2005/04/14 01:38:17 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/executor/execMain.c,v 1.247 2005/04/28 21:47:12 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -560,9 +560,10 @@ InitPlan(QueryDesc *queryDesc, bool explainOnly)
        }
 
        /*
-        * Have to lock relations selected for update
+        * Have to lock relations selected FOR UPDATE/FOR SHARE
         */
        estate->es_rowMark = NIL;
+       estate->es_forUpdate = parseTree->forUpdate;
        if (parseTree->rowMarks != NIL)
        {
                ListCell   *l;
@@ -986,7 +987,7 @@ ExecEndPlan(PlanState *planstate, EState *estate)
                heap_close(estate->es_into_relation_descriptor, NoLock);
 
        /*
-        * close any relations selected FOR UPDATE, again keeping locks
+        * close any relations selected FOR UPDATE/FOR SHARE, again keeping locks
         */
        foreach(l, estate->es_rowMark)
        {
@@ -1126,6 +1127,9 @@ lnext:    ;
                                                                                 * ctid!! */
                                tupleid = &tuple_ctid;
                        }
+                       /*
+                        * Process any FOR UPDATE or FOR SHARE locking requested.
+                        */
                        else if (estate->es_rowMark != NIL)
                        {
                                ListCell   *l;
@@ -1137,6 +1141,7 @@ lnext:    ;
                                        Buffer          buffer;
                                        HeapTupleData tuple;
                                        TupleTableSlot *newSlot;
+                                       LockTupleMode   lockmode;
                                        HTSU_Result             test;
 
                                        if (!ExecGetJunkAttribute(junkfilter,
@@ -1151,9 +1156,15 @@ lnext:   ;
                                        if (isNull)
                                                elog(ERROR, "\"%s\" is NULL", erm->resname);
 
+                                       if (estate->es_forUpdate)
+                                               lockmode = LockTupleExclusive;
+                                       else
+                                               lockmode = LockTupleShared;
+
                                        tuple.t_self = *((ItemPointer) DatumGetPointer(datum));
-                                       test = heap_mark4update(erm->relation, &tuple, &buffer,
-                                                                                       estate->es_snapshot->curcid);
+                                       test = heap_lock_tuple(erm->relation, &tuple, &buffer,
+                                                                                 estate->es_snapshot->curcid,
+                                                                                 lockmode);
                                        ReleaseBuffer(buffer);
                                        switch (test)
                                        {
@@ -1189,7 +1200,7 @@ lnext:    ;
                                                        goto lnext;
 
                                                default:
-                                                       elog(ERROR, "unrecognized heap_mark4update status: %u",
+                                                       elog(ERROR, "unrecognized heap_lock_tuple status: %u",
                                                                 test);
                                                        return (NULL);
                                        }
@@ -1574,8 +1585,8 @@ ExecUpdate(TupleTableSlot *slot,
         * If we generate a new candidate tuple after EvalPlanQual testing, we
         * must loop back here and recheck constraints.  (We don't need to
         * redo triggers, however.      If there are any BEFORE triggers then
-        * trigger.c will have done mark4update to lock the correct tuple, so
-        * there's no need to do them again.)
+        * trigger.c will have done heap_lock_tuple to lock the correct tuple,
+        * so there's no need to do them again.)
         */
 lreplace:;
        if (resultRelationDesc->rd_att->constr)
@@ -2088,6 +2099,7 @@ EvalPlanQualStart(evalPlanQual *epq, EState *estate, evalPlanQual *priorepq)
                epqstate->es_param_exec_vals = (ParamExecData *)
                        palloc0(estate->es_topPlan->nParamExec * sizeof(ParamExecData));
        epqstate->es_rowMark = estate->es_rowMark;
+       epqstate->es_forUpdate = estate->es_forUpdate;
        epqstate->es_instrument = estate->es_instrument;
        epqstate->es_select_into = estate->es_select_into;
        epqstate->es_into_oids = estate->es_into_oids;
index 905c7f89f61ad14cc313ad7e0d8da36945d8dc71..133bf57bca23724dc5c1351c45dabbe8cf151b9a 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/executor/execUtils.c,v 1.122 2005/04/23 21:32:34 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/executor/execUtils.c,v 1.123 2005/04/28 21:47:12 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -198,6 +198,7 @@ CreateExecutorState(void)
        estate->es_processed = 0;
        estate->es_lastoid = InvalidOid;
        estate->es_rowMark = NIL;
+       estate->es_forUpdate = false;
 
        estate->es_instrument = false;
        estate->es_select_into = false;
index f0d8666091d91c2bc9e5ddd565a36aedc668fe67..2d5dd7b2b5c74d39dd69ca8a3f7fbb56a0467f9a 100644 (file)
@@ -15,7 +15,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.303 2005/04/25 01:30:13 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.304 2005/04/28 21:47:12 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1605,6 +1605,7 @@ _copyQuery(Query *from)
        COPY_NODE_FIELD(rtable);
        COPY_NODE_FIELD(jointree);
        COPY_NODE_FIELD(rowMarks);
+       COPY_SCALAR_FIELD(forUpdate);
        COPY_NODE_FIELD(targetList);
        COPY_NODE_FIELD(groupClause);
        COPY_NODE_FIELD(havingQual);
@@ -1685,7 +1686,8 @@ _copySelectStmt(SelectStmt *from)
        COPY_NODE_FIELD(sortClause);
        COPY_NODE_FIELD(limitOffset);
        COPY_NODE_FIELD(limitCount);
-       COPY_NODE_FIELD(forUpdate);
+       COPY_NODE_FIELD(lockedRels);
+       COPY_SCALAR_FIELD(forUpdate);
        COPY_SCALAR_FIELD(op);
        COPY_SCALAR_FIELD(all);
        COPY_NODE_FIELD(larg);
index fb5990970059c265a47f6e2eb13185a2e0d65709..a9570201be26753f82e76fb9202c29a3f743453c 100644 (file)
@@ -18,7 +18,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.240 2005/04/07 01:51:38 neilc Exp $
+ *       $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.241 2005/04/28 21:47:12 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -642,6 +642,7 @@ _equalQuery(Query *a, Query *b)
        COMPARE_NODE_FIELD(rtable);
        COMPARE_NODE_FIELD(jointree);
        COMPARE_NODE_FIELD(rowMarks);
+       COMPARE_SCALAR_FIELD(forUpdate);
        COMPARE_NODE_FIELD(targetList);
        COMPARE_NODE_FIELD(groupClause);
        COMPARE_NODE_FIELD(havingQual);
@@ -706,7 +707,8 @@ _equalSelectStmt(SelectStmt *a, SelectStmt *b)
        COMPARE_NODE_FIELD(sortClause);
        COMPARE_NODE_FIELD(limitOffset);
        COMPARE_NODE_FIELD(limitCount);
-       COMPARE_NODE_FIELD(forUpdate);
+       COMPARE_NODE_FIELD(lockedRels);
+       COMPARE_SCALAR_FIELD(forUpdate);
        COMPARE_SCALAR_FIELD(op);
        COMPARE_SCALAR_FIELD(all);
        COMPARE_NODE_FIELD(larg);
index 484551a85039446f6e8feac577553bf20356ffff..2dc676b8d43c671865c83578995ead6505ee3ff7 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/nodes/outfuncs.c,v 1.250 2005/04/25 01:30:13 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/nodes/outfuncs.c,v 1.251 2005/04/28 21:47:13 tgl Exp $
  *
  * NOTES
  *       Every node type that can appear in stored rules' parsetrees *must*
@@ -1268,7 +1268,8 @@ _outSelectStmt(StringInfo str, SelectStmt *node)
        WRITE_NODE_FIELD(sortClause);
        WRITE_NODE_FIELD(limitOffset);
        WRITE_NODE_FIELD(limitCount);
-       WRITE_NODE_FIELD(forUpdate);
+       WRITE_NODE_FIELD(lockedRels);
+       WRITE_BOOL_FIELD(forUpdate);
        WRITE_ENUM_FIELD(op, SetOperation);
        WRITE_BOOL_FIELD(all);
        WRITE_NODE_FIELD(larg);
@@ -1385,6 +1386,7 @@ _outQuery(StringInfo str, Query *node)
        WRITE_NODE_FIELD(rtable);
        WRITE_NODE_FIELD(jointree);
        WRITE_NODE_FIELD(rowMarks);
+       WRITE_BOOL_FIELD(forUpdate);
        WRITE_NODE_FIELD(targetList);
        WRITE_NODE_FIELD(groupClause);
        WRITE_NODE_FIELD(havingQual);
index f457d0633154036be88823ffcb967a2ba043d1a4..ef405f7a0223ea0d7c006e341c97acb2eed0a988 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/nodes/readfuncs.c,v 1.176 2005/04/06 16:34:05 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/nodes/readfuncs.c,v 1.177 2005/04/28 21:47:13 tgl Exp $
  *
  * NOTES
  *       Path and Plan nodes do not have any readfuncs support, because we
@@ -145,6 +145,7 @@ _readQuery(void)
        READ_NODE_FIELD(rtable);
        READ_NODE_FIELD(jointree);
        READ_NODE_FIELD(rowMarks);
+       READ_BOOL_FIELD(forUpdate);
        READ_NODE_FIELD(targetList);
        READ_NODE_FIELD(groupClause);
        READ_NODE_FIELD(havingQual);
index 8675b6efd3ca74f552fe5458090479fab0d75327..84544a20115da91b8a49a41caf740fbb2e915833 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/optimizer/path/allpaths.c,v 1.128 2005/04/25 01:30:13 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/optimizer/path/allpaths.c,v 1.129 2005/04/28 21:47:13 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -214,13 +214,13 @@ set_inherited_rel_pathlist(Query *root, RelOptInfo *rel,
        ListCell   *il;
 
        /*
-        * XXX for now, can't handle inherited expansion of FOR UPDATE; can we
-        * do better?
+        * XXX for now, can't handle inherited expansion of FOR UPDATE/SHARE;
+        * can we do better?
         */
        if (list_member_int(root->rowMarks, parentRTindex))
                ereport(ERROR,
                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                                errmsg("SELECT FOR UPDATE is not supported for inheritance queries")));
+                                errmsg("SELECT FOR UPDATE/SHARE is not supported for inheritance queries")));
 
        /*
         * Initialize to compute size estimates for whole inheritance tree
index 292b5503566698294d144c816aecf233d79b9928..22878c0099e11145f1a1c42138d6101c2358e7a1 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/optimizer/plan/initsplan.c,v 1.104 2004/12/31 22:00:09 pgsql Exp $
+ *       $PostgreSQL: pgsql/src/backend/optimizer/plan/initsplan.c,v 1.105 2005/04/28 21:47:13 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -323,11 +323,11 @@ mark_baserels_for_outer_join(Query *root, Relids rels, Relids outerrels)
                Assert(bms_is_subset(rel->outerjoinset, outerrels));
 
                /*
-                * Presently the executor cannot support FOR UPDATE marking of
+                * Presently the executor cannot support FOR UPDATE/SHARE marking of
                 * rels appearing on the nullable side of an outer join. (It's
                 * somewhat unclear what that would mean, anyway: what should we
                 * mark when a result row is generated from no element of the
-                * nullable relation?)  So, complain if target rel is FOR UPDATE.
+                * nullable relation?)  So, complain if target rel is FOR UPDATE/SHARE.
                 * It's sufficient to make this check once per rel, so do it only
                 * if rel wasn't already known nullable.
                 */
@@ -336,7 +336,7 @@ mark_baserels_for_outer_join(Query *root, Relids rels, Relids outerrels)
                        if (list_member_int(root->rowMarks, relno))
                                ereport(ERROR,
                                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                                                errmsg("SELECT FOR UPDATE cannot be applied to the nullable side of an outer join")));
+                                                errmsg("SELECT FOR UPDATE/SHARE cannot be applied to the nullable side of an outer join")));
                }
 
                rel->outerjoinset = outerrels;
index b542fef61e26a697c1fbd44dec2be43147456b70..2e83a7417df466ed6da67458475ed4b4791ef835 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.184 2005/04/11 23:06:55 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.185 2005/04/28 21:47:13 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -635,13 +635,13 @@ grouping_planner(Query *parse, double tuple_fraction)
                tlist = postprocess_setop_tlist(result_plan->targetlist, tlist);
 
                /*
-                * Can't handle FOR UPDATE here (parser should have checked
+                * Can't handle FOR UPDATE/SHARE here (parser should have checked
                 * already, but let's make sure).
                 */
                if (parse->rowMarks)
                        ereport(ERROR,
                                        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                                        errmsg("SELECT FOR UPDATE is not allowed with UNION/INTERSECT/EXCEPT")));
+                                        errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
 
                /*
                 * Calculate pathkeys that represent result ordering requirements
index 603b8c43582706642e39f07b98838a4cff5d4a69..b5b658cf58a52124e66c19f10b7c3813f9e8bb7c 100644 (file)
@@ -16,7 +16,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/optimizer/prep/prepjointree.c,v 1.26 2005/04/06 16:34:06 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/optimizer/prep/prepjointree.c,v 1.27 2005/04/28 21:47:14 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -276,11 +276,22 @@ pull_up_subqueries(Query *parse, Node *jtnode, bool below_outer_join)
                        parse->rtable = list_concat(parse->rtable, subquery->rtable);
 
                        /*
-                        * Pull up any FOR UPDATE markers, too.  (OffsetVarNodes
+                        * Pull up any FOR UPDATE/SHARE markers, too.  (OffsetVarNodes
                         * already adjusted the marker values, so just list_concat the
                         * list.)
+                        *
+                        * Executor can't handle multiple FOR UPDATE/SHARE flags, so
+                        * complain if they are valid but different
                         */
+                       if (parse->rowMarks && subquery->rowMarks &&
+                               parse->forUpdate != subquery->forUpdate)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                                                errmsg("cannot use both FOR UPDATE and FOR SHARE in one query")));
+
                        parse->rowMarks = list_concat(parse->rowMarks, subquery->rowMarks);
+                       if (subquery->rowMarks)
+                               parse->forUpdate = subquery->forUpdate;
 
                        /*
                         * We also have to fix the relid sets of any parent
index ac8dae65ce7e391728c9571ea67648a08e5e1e3c..c4f3115443721debeff641cffa74738b4336030f 100644 (file)
@@ -15,7 +15,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/optimizer/prep/preptlist.c,v 1.74 2005/04/06 16:34:06 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/optimizer/prep/preptlist.c,v 1.75 2005/04/28 21:47:14 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -102,7 +102,7 @@ preprocess_targetlist(Query *parse, List *tlist)
        }
 
        /*
-        * Add TID targets for rels selected FOR UPDATE.  The executor
+        * Add TID targets for rels selected FOR UPDATE/SHARE.  The executor
         * uses the TID to know which rows to lock, much as for UPDATE or
         * DELETE.
         */
@@ -111,22 +111,22 @@ preprocess_targetlist(Query *parse, List *tlist)
                ListCell   *l;
 
                /*
-                * We've got trouble if the FOR UPDATE appears inside
+                * We've got trouble if the FOR UPDATE/SHARE appears inside
                 * grouping, since grouping renders a reference to individual
                 * tuple CTIDs invalid.  This is also checked at parse time,
                 * but that's insufficient because of rule substitution, query
                 * pullup, etc.
                 */
-               CheckSelectForUpdate(parse);
+               CheckSelectLocking(parse, parse->forUpdate);
 
                /*
-                * Currently the executor only supports FOR UPDATE at top
+                * Currently the executor only supports FOR UPDATE/SHARE at top
                 * level
                 */
                if (PlannerQueryLevel > 1)
                        ereport(ERROR,
                                        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                                        errmsg("SELECT FOR UPDATE is not allowed in subqueries")));
+                                        errmsg("SELECT FOR UPDATE/SHARE is not allowed in subqueries")));
 
                foreach(l, parse->rowMarks)
                {
index 3cb64dc8249f0250aad1efc002e7f7e200f313b9..ee6bfe6ae92ebe512e7d94aa7ecd67425974ea99 100644 (file)
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- *     $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.320 2005/04/14 20:03:24 tgl Exp $
+ *     $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.321 2005/04/28 21:47:14 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -134,7 +134,7 @@ static void transformFKConstraints(ParseState *pstate,
                                           bool isAddConstraint);
 static void applyColumnNames(List *dst, List *src);
 static List *getSetColTypes(ParseState *pstate, Node *node);
-static void transformForUpdate(Query *qry, List *forUpdate);
+static void transformLocking(Query *qry, List *lockedRels, bool forUpdate);
 static void transformConstraintAttrs(List *constraintList);
 static void transformColumnType(ParseState *pstate, ColumnDef *column);
 static void release_pstate_resources(ParseState *pstate);
@@ -1810,8 +1810,8 @@ transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
 
        qry->commandType = CMD_SELECT;
 
-       /* make FOR UPDATE clause available to addRangeTableEntry */
-       pstate->p_forUpdate = stmt->forUpdate;
+       /* make FOR UPDATE/FOR SHARE list available to addRangeTableEntry */
+       pstate->p_lockedRels = stmt->lockedRels;
 
        /* process the FROM clause */
        transformFromClause(pstate, stmt->fromClause);
@@ -1870,8 +1870,8 @@ transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
        if (pstate->p_hasAggs || qry->groupClause || qry->havingQual)
                parseCheckAggregates(pstate, qry);
 
-       if (stmt->forUpdate != NIL)
-               transformForUpdate(qry, stmt->forUpdate);
+       if (stmt->lockedRels != NIL)
+               transformLocking(qry, stmt->lockedRels, stmt->forUpdate);
 
        return qry;
 }
@@ -1899,7 +1899,8 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
        List       *sortClause;
        Node       *limitOffset;
        Node       *limitCount;
-       List       *forUpdate;
+       List       *lockedRels;
+       bool            forUpdate;
        Node       *node;
        ListCell   *left_tlist,
                           *dtlist;
@@ -1937,18 +1938,19 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
        sortClause = stmt->sortClause;
        limitOffset = stmt->limitOffset;
        limitCount = stmt->limitCount;
+       lockedRels = stmt->lockedRels;
        forUpdate = stmt->forUpdate;
 
        stmt->sortClause = NIL;
        stmt->limitOffset = NULL;
        stmt->limitCount = NULL;
-       stmt->forUpdate = NIL;
+       stmt->lockedRels = NIL;
 
-       /* We don't support forUpdate with set ops at the moment. */
-       if (forUpdate)
+       /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
+       if (lockedRels)
                ereport(ERROR,
                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                                errmsg("SELECT FOR UPDATE is not allowed with UNION/INTERSECT/EXCEPT")));
+                                errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
 
        /*
         * Recursively transform the components of the tree.
@@ -2083,8 +2085,8 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
        if (pstate->p_hasAggs || qry->groupClause || qry->havingQual)
                parseCheckAggregates(pstate, qry);
 
-       if (forUpdate != NIL)
-               transformForUpdate(qry, forUpdate);
+       if (lockedRels != NIL)
+               transformLocking(qry, lockedRels, forUpdate);
 
        return qry;
 }
@@ -2107,11 +2109,11 @@ transformSetOperationTree(ParseState *pstate, SelectStmt *stmt)
                ereport(ERROR,
                                (errcode(ERRCODE_SYNTAX_ERROR),
                                 errmsg("INTO is only allowed on first SELECT of UNION/INTERSECT/EXCEPT")));
-       /* We don't support forUpdate with set ops at the moment. */
-       if (stmt->forUpdate)
+       /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
+       if (stmt->lockedRels)
                ereport(ERROR,
                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                                errmsg("SELECT FOR UPDATE is not allowed with UNION/INTERSECT/EXCEPT")));
+                                errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
 
        /*
         * If an internal node of a set-op tree has ORDER BY, UPDATE, or LIMIT
@@ -2128,7 +2130,7 @@ transformSetOperationTree(ParseState *pstate, SelectStmt *stmt)
        {
                Assert(stmt->larg != NULL && stmt->rarg != NULL);
                if (stmt->sortClause || stmt->limitOffset || stmt->limitCount ||
-                       stmt->forUpdate)
+                       stmt->lockedRels)
                        isLeaf = true;
                else
                        isLeaf = false;
@@ -2711,47 +2713,67 @@ transformExecuteStmt(ParseState *pstate, ExecuteStmt *stmt)
 
 /* exported so planner can check again after rewriting, query pullup, etc */
 void
-CheckSelectForUpdate(Query *qry)
+CheckSelectLocking(Query *qry, bool forUpdate)
 {
+       const char *operation;
+
+       if (forUpdate)
+               operation = "SELECT FOR UPDATE";
+       else
+               operation = "SELECT FOR SHARE";
+
        if (qry->setOperations)
                ereport(ERROR,
                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                                errmsg("SELECT FOR UPDATE is not allowed with UNION/INTERSECT/EXCEPT")));
+                                /* translator: %s is a SQL command, like SELECT FOR UPDATE */
+                                errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT", operation)));
        if (qry->distinctClause != NIL)
                ereport(ERROR,
                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-               errmsg("SELECT FOR UPDATE is not allowed with DISTINCT clause")));
+                                /* translator: %s is a SQL command, like SELECT FOR UPDATE */
+                                errmsg("%s is not allowed with DISTINCT clause", operation)));
        if (qry->groupClause != NIL)
                ereport(ERROR,
                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-               errmsg("SELECT FOR UPDATE is not allowed with GROUP BY clause")));
+                                /* translator: %s is a SQL command, like SELECT FOR UPDATE */
+                                errmsg("%s is not allowed with GROUP BY clause", operation)));
        if (qry->havingQual != NULL)
                ereport(ERROR,
                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-               errmsg("SELECT FOR UPDATE is not allowed with HAVING clause")));
+                                /* translator: %s is a SQL command, like SELECT FOR UPDATE */
+                                errmsg("%s is not allowed with HAVING clause", operation)));
        if (qry->hasAggs)
                ereport(ERROR,
                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                                errmsg("SELECT FOR UPDATE is not allowed with aggregate functions")));
+                                /* translator: %s is a SQL command, like SELECT FOR UPDATE */
+                                errmsg("%s is not allowed with aggregate functions", operation)));
 }
 
 /*
- * Convert FOR UPDATE name list into rowMarks list of integer relids
+ * Convert FOR UPDATE/SHARE name list into rowMarks list of integer relids
  *
- * NB: if you need to change this, see also markQueryForUpdate()
+ * NB: if you need to change this, see also markQueryForLocking()
  * in rewriteHandler.c.
  */
 static void
-transformForUpdate(Query *qry, List *forUpdate)
+transformLocking(Query *qry, List *lockedRels, bool forUpdate)
 {
-       List       *rowMarks = qry->rowMarks;
+       List       *rowMarks;
        ListCell   *l;
        ListCell   *rt;
        Index           i;
 
-       CheckSelectForUpdate(qry);
+       if (qry->rowMarks && forUpdate != qry->forUpdate)
+               ereport(ERROR,
+                               (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                                errmsg("cannot use both FOR UPDATE and FOR SHARE in one query")));
+       qry->forUpdate = forUpdate;
+
+       CheckSelectLocking(qry, forUpdate);
+       
+       rowMarks = qry->rowMarks;
 
-       if (linitial(forUpdate) == NULL)
+       if (linitial(lockedRels) == NULL)
        {
                /* all regular tables used in query */
                i = 0;
@@ -2770,10 +2792,11 @@ transformForUpdate(Query *qry, List *forUpdate)
                                case RTE_SUBQUERY:
 
                                        /*
-                                        * FOR UPDATE of subquery is propagated to subquery's
-                                        * rels
+                                        * FOR UPDATE/SHARE of subquery is propagated to all
+                                        * of subquery's rels
                                         */
-                                       transformForUpdate(rte->subquery, list_make1(NULL));
+                                       transformLocking(rte->subquery, list_make1(NULL),
+                                                                        forUpdate);
                                        break;
                                default:
                                        /* ignore JOIN, SPECIAL, FUNCTION RTEs */
@@ -2784,7 +2807,7 @@ transformForUpdate(Query *qry, List *forUpdate)
        else
        {
                /* just the named tables */
-               foreach(l, forUpdate)
+               foreach(l, lockedRels)
                {
                        char       *relname = strVal(lfirst(l));
 
@@ -2806,25 +2829,26 @@ transformForUpdate(Query *qry, List *forUpdate)
                                                case RTE_SUBQUERY:
 
                                                        /*
-                                                        * FOR UPDATE of subquery is propagated to
-                                                        * subquery's rels
+                                                        * FOR UPDATE/SHARE of subquery is propagated to
+                                                        * all of subquery's rels
                                                         */
-                                                       transformForUpdate(rte->subquery, list_make1(NULL));
+                                                       transformLocking(rte->subquery, list_make1(NULL),
+                                                                                        forUpdate);
                                                        break;
                                                case RTE_JOIN:
                                                        ereport(ERROR,
                                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                                                                 errmsg("SELECT FOR UPDATE cannot be applied to a join")));
+                                                                 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to a join")));
                                                        break;
                                                case RTE_SPECIAL:
                                                        ereport(ERROR,
                                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                                                                 errmsg("SELECT FOR UPDATE cannot be applied to NEW or OLD")));
+                                                                 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to NEW or OLD")));
                                                        break;
                                                case RTE_FUNCTION:
                                                        ereport(ERROR,
                                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                                                                 errmsg("SELECT FOR UPDATE cannot be applied to a function")));
+                                                                 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to a function")));
                                                        break;
                                                default:
                                                        elog(ERROR, "unrecognized RTE type: %d",
@@ -2837,7 +2861,7 @@ transformForUpdate(Query *qry, List *forUpdate)
                        if (rt == NULL)
                                ereport(ERROR,
                                                (errcode(ERRCODE_UNDEFINED_TABLE),
-                                                errmsg("relation \"%s\" in FOR UPDATE clause not found in FROM clause",
+                                                errmsg("relation \"%s\" in FOR UPDATE/SHARE clause not found in FROM clause",
                                                                relname)));
                }
        }
index 260a2e1b455efb3edd5f728bd10c9b3a50ed55b0..c51c10c7a89abaffdc2d81fb73aacd2986dd369b 100644 (file)
@@ -11,7 +11,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.488 2005/04/23 17:22:16 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.489 2005/04/28 21:47:14 tgl Exp $
  *
  * HISTORY
  *       AUTHOR                        DATE                    MAJOR EVENT
@@ -87,7 +87,7 @@ static List *check_func_name(List *names);
 static List *extractArgTypes(List *parameters);
 static SelectStmt *findLeftmostSelect(SelectStmt *node);
 static void insertSelectOptions(SelectStmt *stmt,
-                                                               List *sortClause, List *forUpdate,
+                                                               List *sortClause, List *lockingClause,
                                                                Node *limitOffset, Node *limitCount);
 static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg);
 static Node *doNegate(Node *n);
@@ -242,7 +242,8 @@ static void doNegateFloat(Value *v);
 %type <oncommit> OnCommitOption
 %type <withoids> OptWithOids WithOidsAs
 
-%type <list>   for_update_clause opt_for_update_clause update_list
+%type <list>   for_locking_clause opt_for_locking_clause
+                               update_list
 %type <boolean>        opt_all
 
 %type <node>   join_outer join_qual
@@ -4886,9 +4887,9 @@ select_with_parens:
                ;
 
 /*
- *     FOR UPDATE may be before or after LIMIT/OFFSET.
+ *     FOR UPDATE/SHARE may be before or after LIMIT/OFFSET.
  *     In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
- *     We now support both orderings, but prefer LIMIT/OFFSET before FOR UPDATE
+ *     We now support both orderings, but prefer LIMIT/OFFSET before FOR UPDATE/SHARE
  *     2002-08-28 bjm
  */
 select_no_parens:
@@ -4899,13 +4900,13 @@ select_no_parens:
                                                                                NULL, NULL);
                                        $$ = $1;
                                }
-                       | select_clause opt_sort_clause for_update_clause opt_select_limit
+                       | select_clause opt_sort_clause for_locking_clause opt_select_limit
                                {
                                        insertSelectOptions((SelectStmt *) $1, $2, $3,
                                                                                list_nth($4, 0), list_nth($4, 1));
                                        $$ = $1;
                                }
-                       | select_clause opt_sort_clause select_limit opt_for_update_clause
+                       | select_clause opt_sort_clause select_limit opt_for_locking_clause
                                {
                                        insertSelectOptions((SelectStmt *) $1, $2, $4,
                                                                                list_nth($3, 0), list_nth($3, 1));
@@ -5146,13 +5147,14 @@ having_clause:
                        | /*EMPTY*/                                                             { $$ = NULL; }
                ;
 
-for_update_clause:
-                       FOR UPDATE update_list                                  { $$ = $3; }
+for_locking_clause:
+                       FOR UPDATE update_list                                  { $$ = lcons(makeString("for_update"), $3); }
+                       | FOR SHARE update_list                                 { $$ = lcons(makeString("for_share"), $3); }
                        | FOR READ ONLY                                                 { $$ = NULL; }
                ;
 
-opt_for_update_clause:
-                       for_update_clause                                               { $$ = $1; }
+opt_for_locking_clause:
+                       for_locking_clause                                              { $$ = $1; }
                        | /* EMPTY */                                                   { $$ = NULL; }
                ;
 
@@ -8379,7 +8381,7 @@ findLeftmostSelect(SelectStmt *node)
  */
 static void
 insertSelectOptions(SelectStmt *stmt,
-                                       List *sortClause, List *forUpdate,
+                                       List *sortClause, List *lockingClause,
                                        Node *limitOffset, Node *limitCount)
 {
        /*
@@ -8394,13 +8396,27 @@ insertSelectOptions(SelectStmt *stmt,
                                         errmsg("multiple ORDER BY clauses not allowed")));
                stmt->sortClause = sortClause;
        }
-       if (forUpdate)
+       if (lockingClause)
        {
-               if (stmt->forUpdate)
+               Value      *type;
+
+               if (stmt->lockedRels)
                        ereport(ERROR,
                                        (errcode(ERRCODE_SYNTAX_ERROR),
-                                        errmsg("multiple FOR UPDATE clauses not allowed")));
-               stmt->forUpdate = forUpdate;
+                                        errmsg("multiple FOR UPDATE/FOR SHARE clauses not allowed")));
+
+               Assert(list_length(lockingClause) > 1);
+               /* 1st is Value node containing "for_update" or "for_share" */
+               type = (Value *) linitial(lockingClause);
+               Assert(IsA(type, String));
+               if (strcmp(strVal(type), "for_update") == 0)
+                       stmt->forUpdate = true;
+               else if (strcmp(strVal(type), "for_share") == 0)
+                       stmt->forUpdate = false;
+               else
+                       elog(ERROR, "invalid first node in locking clause");
+
+               stmt->lockedRels = list_delete_first(lockingClause);
        }
        if (limitOffset)
        {
index b68ae005150490472c6d8202a7456cc321fba49b..d1e5fca2aae9453cd7be5f87a786f4510c38dd53 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/parser/parse_relation.c,v 1.106 2005/04/13 16:50:55 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/parser/parse_relation.c,v 1.107 2005/04/28 21:47:14 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -40,7 +40,7 @@ static Node *scanNameSpaceForRelid(ParseState *pstate, Node *nsnode,
                                          Oid relid);
 static void scanNameSpaceForConflict(ParseState *pstate, Node *nsnode,
                                                 RangeTblEntry *rte1, const char *aliasname1);
-static bool isForUpdate(ParseState *pstate, char *refname);
+static bool isLockedRel(ParseState *pstate, char *refname);
 static void expandRelation(Oid relid, Alias *eref,
                           int rtindex, int sublevels_up,
                           bool include_dropped,
@@ -759,9 +759,9 @@ addRangeTableEntry(ParseState *pstate,
         * Get the rel's OID.  This access also ensures that we have an
         * up-to-date relcache entry for the rel.  Since this is typically the
         * first access to a rel in a statement, be careful to get the right
-        * access level depending on whether we're doing SELECT FOR UPDATE.
+        * access level depending on whether we're doing SELECT FOR UPDATE/SHARE.
         */
-       lockmode = isForUpdate(pstate, refname) ? RowShareLock : AccessShareLock;
+       lockmode = isLockedRel(pstate, refname) ? RowShareLock : AccessShareLock;
        rel = heap_openrv(relation, lockmode);
        rte->relid = RelationGetRelid(rel);
 
@@ -1121,17 +1121,17 @@ addRangeTableEntryForJoin(ParseState *pstate,
 }
 
 /*
- * Has the specified refname been selected FOR UPDATE?
+ * Has the specified refname been selected FOR UPDATE/FOR SHARE?
  */
 static bool
-isForUpdate(ParseState *pstate, char *refname)
+isLockedRel(ParseState *pstate, char *refname)
 {
        /* Outer loop to check parent query levels as well as this one */
        while (pstate != NULL)
        {
-               if (pstate->p_forUpdate != NIL)
+               if (pstate->p_lockedRels != NIL)
                {
-                       if (linitial(pstate->p_forUpdate) == NULL)
+                       if (linitial(pstate->p_lockedRels) == NULL)
                        {
                                /* all tables used in query */
                                return true;
@@ -1141,7 +1141,7 @@ isForUpdate(ParseState *pstate, char *refname)
                                /* just the named tables */
                                ListCell   *l;
 
-                               foreach(l, pstate->p_forUpdate)
+                               foreach(l, pstate->p_lockedRels)
                                {
                                        char       *rname = strVal(lfirst(l));
 
index aae7908f50782cabe1806f2852d62bbb6fdfc111..21ce20c5ecfcbafd553f6f384918fa3568bcff15 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/parser/parse_type.c,v 1.73 2004/12/31 22:00:27 pgsql Exp $
+ *       $PostgreSQL: pgsql/src/backend/parser/parse_type.c,v 1.74 2005/04/28 21:47:14 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -432,7 +432,7 @@ parseTypeString(const char *str, Oid *type_id, int32 *typmod)
                stmt->sortClause != NIL ||
                stmt->limitOffset != NULL ||
                stmt->limitCount != NULL ||
-               stmt->forUpdate != NIL ||
+               stmt->lockedRels != NIL ||
                stmt->op != SETOP_NONE)
                goto fail;
        if (list_length(stmt->targetList) != 1)
index c190b634b2735d17d442091e50a63ce441cede87..a317764c43c2bfdb2315412e3e8321b1d91a11be 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/rewrite/rewriteHandler.c,v 1.150 2005/04/06 16:34:06 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/rewrite/rewriteHandler.c,v 1.151 2005/04/28 21:47:14 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -51,7 +51,7 @@ static TargetEntry *process_matched_tle(TargetEntry *src_tle,
                                        TargetEntry *prior_tle,
                                        const char *attrName);
 static Node *get_assignment_input(Node *node);
-static void markQueryForUpdate(Query *qry, bool skipOldNew);
+static void markQueryForLocking(Query *qry, bool forUpdate, bool skipOldNew);
 static List *matchLocks(CmdType event, RuleLock *rulelocks,
                   int varno, Query *parsetree);
 static Query *fireRIRrules(Query *parsetree, List *activeRIRs);
@@ -745,40 +745,46 @@ ApplyRetrieveRule(Query *parsetree,
        rte->checkAsUser = 0;
 
        /*
-        * FOR UPDATE of view?
+        * FOR UPDATE/SHARE of view?
         */
        if (list_member_int(parsetree->rowMarks, rt_index))
        {
                /*
                 * Remove the view from the list of rels that will actually be
-                * marked FOR UPDATE by the executor.  It will still be access-
+                * marked FOR UPDATE/SHARE by the executor.  It will still be access-
                 * checked for write access, though.
                 */
                parsetree->rowMarks = list_delete_int(parsetree->rowMarks, rt_index);
 
                /*
-                * Set up the view's referenced tables as if FOR UPDATE.
+                * Set up the view's referenced tables as if FOR UPDATE/SHARE.
                 */
-               markQueryForUpdate(rule_action, true);
+               markQueryForLocking(rule_action, parsetree->forUpdate, true);
        }
 
        return parsetree;
 }
 
 /*
- * Recursively mark all relations used by a view as FOR UPDATE.
+ * Recursively mark all relations used by a view as FOR UPDATE/SHARE.
  *
  * This may generate an invalid query, eg if some sub-query uses an
  * aggregate.  We leave it to the planner to detect that.
  *
- * NB: this must agree with the parser's transformForUpdate() routine.
+ * NB: this must agree with the parser's transformLocking() routine.
  */
 static void
-markQueryForUpdate(Query *qry, bool skipOldNew)
+markQueryForLocking(Query *qry, bool forUpdate, bool skipOldNew)
 {
        Index           rti = 0;
        ListCell   *l;
 
+       if (qry->rowMarks && forUpdate != qry->forUpdate)
+               ereport(ERROR,
+                               (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                                errmsg("cannot use both FOR UPDATE and FOR SHARE in one query")));
+       qry->forUpdate = forUpdate;
+
        foreach(l, qry->rtable)
        {
                RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
@@ -798,8 +804,8 @@ markQueryForUpdate(Query *qry, bool skipOldNew)
                }
                else if (rte->rtekind == RTE_SUBQUERY)
                {
-                       /* FOR UPDATE of subquery is propagated to subquery's rels */
-                       markQueryForUpdate(rte->subquery, false);
+                       /* FOR UPDATE/SHARE of subquery is propagated to subquery's rels */
+                       markQueryForLocking(rte->subquery, forUpdate, false);
                }
        }
 }
@@ -911,7 +917,7 @@ fireRIRrules(Query *parsetree, List *activeRIRs)
                 * If the relation is the query's result relation, then
                 * RewriteQuery() already got the right lock on it, so we need no
                 * additional lock. Otherwise, check to see if the relation is
-                * accessed FOR UPDATE or not.
+                * accessed FOR UPDATE/SHARE or not.
                 */
                if (rt_index == parsetree->resultRelation)
                        lockmode = NoLock;
index 5212bc1bbe3974bbbbffc9e301742bae86c09984..975d5f131d7d184e88dacabbb2b9908cd8dcd593 100644 (file)
@@ -8,13 +8,14 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/storage/ipc/ipci.c,v 1.74 2004/12/31 22:00:56 pgsql Exp $
+ *       $PostgreSQL: pgsql/src/backend/storage/ipc/ipci.c,v 1.75 2005/04/28 21:47:15 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
 #include "postgres.h"
 
 #include "access/clog.h"
+#include "access/multixact.h"
 #include "access/subtrans.h"
 #include "access/xlog.h"
 #include "miscadmin.h"
@@ -75,6 +76,7 @@ CreateSharedMemoryAndSemaphores(bool makePrivate,
                size += XLOGShmemSize();
                size += CLOGShmemSize();
                size += SUBTRANSShmemSize();
+               size += MultiXactShmemSize();
                size += LWLockShmemSize();
                size += SInvalShmemSize(maxBackends);
                size += FreeSpaceShmemSize();
@@ -140,6 +142,7 @@ CreateSharedMemoryAndSemaphores(bool makePrivate,
        XLOGShmemInit();
        CLOGShmemInit();
        SUBTRANSShmemInit();
+       MultiXactShmemInit();
        InitBufferPool();
 
        /*
index f75ccc869ffc4fee4e211318ac3df9f4bb1c6a1e..8b7e6f945164c2302a15136d4a23dfc33d02c336 100644 (file)
@@ -15,7 +15,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/storage/lmgr/lwlock.c,v 1.27 2005/04/08 14:18:35 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/storage/lmgr/lwlock.c,v 1.28 2005/04/28 21:47:15 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -114,6 +114,12 @@ NumLWLocks(void)
        /* subtrans.c needs one per SubTrans buffer */
        numLocks += NUM_SLRU_BUFFERS;
 
+       /*
+        * multixact.c needs one per MultiXact buffer, but there are
+        * two SLRU areas for MultiXact
+        */
+       numLocks += 2 * NUM_SLRU_BUFFERS;
+
        /* Perhaps create a few more for use by user-defined modules? */
 
        return numLocks;
index 3fc8a2d8882ec00d777fe8b8890ee4c34201741e..73be822a83e7f1b92e86fc362820c366992e3855 100644 (file)
@@ -10,7 +10,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/tcop/utility.c,v 1.235 2005/04/14 01:38:18 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/tcop/utility.c,v 1.236 2005/04/28 21:47:15 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -238,7 +238,7 @@ QueryIsReadOnly(Query *parsetree)
                        if (parsetree->into != NULL)
                                return false;                                   /* SELECT INTO */
                        else if (parsetree->rowMarks != NIL)
-                               return false;                                   /* SELECT FOR UPDATE */
+                               return false;                                   /* SELECT FOR UPDATE/SHARE */
                        else
                                return true;
                case CMD_UPDATE:
@@ -1663,7 +1663,12 @@ CreateQueryTag(Query *parsetree)
                        if (parsetree->into != NULL)
                                tag = "SELECT INTO";
                        else if (parsetree->rowMarks != NIL)
-                               tag = "SELECT FOR UPDATE";
+                       {
+                               if (parsetree->forUpdate)
+                                       tag = "SELECT FOR UPDATE";
+                               else
+                                       tag = "SELECT FOR SHARE";
+                       }
                        else
                                tag = "SELECT";
                        break;
index 72ae3b8ddea1ab0895c63fdbeec91115e2d56f90..78a85b7edccd794dc4678579eec61878b55d7ad8 100644 (file)
@@ -17,7 +17,7 @@
  *
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/backend/utils/adt/ri_triggers.c,v 1.76 2004/12/31 22:01:22 pgsql Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/ri_triggers.c,v 1.77 2005/04/28 21:47:15 tgl Exp $
  *
  * ----------
  */
@@ -206,7 +206,7 @@ RI_FKey_check(PG_FUNCTION_ARGS)
         * tuple.
         *
         * pk_rel is opened in RowShareLock mode since that's what our eventual
-        * SELECT FOR UPDATE will get on it.
+        * SELECT FOR SHARE will get on it.
         */
        pk_rel = heap_open(trigdata->tg_trigger->tgconstrrelid, RowShareLock);
        fk_rel = trigdata->tg_relation;
@@ -267,7 +267,7 @@ RI_FKey_check(PG_FUNCTION_ARGS)
                         * ----------
                         */
                        quoteRelationName(pkrelname, pk_rel);
-                       snprintf(querystr, sizeof(querystr), "SELECT 1 FROM ONLY %s x FOR UPDATE OF x",
+                       snprintf(querystr, sizeof(querystr), "SELECT 1 FROM ONLY %s x FOR SHARE OF x",
                                         pkrelname);
 
                        /* Prepare and save the plan */
@@ -428,7 +428,7 @@ RI_FKey_check(PG_FUNCTION_ARGS)
                        queryoids[i] = SPI_gettypeid(fk_rel->rd_att,
                                                                         qkey.keypair[i][RI_KEYPAIR_FK_IDX]);
                }
-               strcat(querystr, " FOR UPDATE OF x");
+               strcat(querystr, " FOR SHARE OF x");
 
                /* Prepare and save the plan */
                qplan = ri_PlanCheck(querystr, qkey.nkeypairs, queryoids,
@@ -590,7 +590,7 @@ ri_Check_Pk_Match(Relation pk_rel, Relation fk_rel,
                        queryoids[i] = SPI_gettypeid(pk_rel->rd_att,
                                                                         qkey.keypair[i][RI_KEYPAIR_PK_IDX]);
                }
-               strcat(querystr, " FOR UPDATE OF x");
+               strcat(querystr, " FOR SHARE OF x");
 
                /* Prepare and save the plan */
                qplan = ri_PlanCheck(querystr, qkey.nkeypairs, queryoids,
@@ -655,7 +655,7 @@ RI_FKey_noaction_del(PG_FUNCTION_ARGS)
         * tuple.
         *
         * fk_rel is opened in RowShareLock mode since that's what our eventual
-        * SELECT FOR UPDATE will get on it.
+        * SELECT FOR SHARE will get on it.
         */
        fk_rel = heap_open(trigdata->tg_trigger->tgconstrrelid, RowShareLock);
        pk_rel = trigdata->tg_relation;
@@ -748,7 +748,7 @@ RI_FKey_noaction_del(PG_FUNCTION_ARGS)
                                        queryoids[i] = SPI_gettypeid(pk_rel->rd_att,
                                                                         qkey.keypair[i][RI_KEYPAIR_PK_IDX]);
                                }
-                               strcat(querystr, " FOR UPDATE OF x");
+                               strcat(querystr, " FOR SHARE OF x");
 
                                /* Prepare and save the plan */
                                qplan = ri_PlanCheck(querystr, qkey.nkeypairs, queryoids,
@@ -834,7 +834,7 @@ RI_FKey_noaction_upd(PG_FUNCTION_ARGS)
         * and old tuple.
         *
         * fk_rel is opened in RowShareLock mode since that's what our eventual
-        * SELECT FOR UPDATE will get on it.
+        * SELECT FOR SHARE will get on it.
         */
        fk_rel = heap_open(trigdata->tg_trigger->tgconstrrelid, RowShareLock);
        pk_rel = trigdata->tg_relation;
@@ -939,7 +939,7 @@ RI_FKey_noaction_upd(PG_FUNCTION_ARGS)
                                        queryoids[i] = SPI_gettypeid(pk_rel->rd_att,
                                                                         qkey.keypair[i][RI_KEYPAIR_PK_IDX]);
                                }
-                               strcat(querystr, " FOR UPDATE OF x");
+                               strcat(querystr, " FOR SHARE OF x");
 
                                /* Prepare and save the plan */
                                qplan = ri_PlanCheck(querystr, qkey.nkeypairs, queryoids,
@@ -1373,7 +1373,7 @@ RI_FKey_restrict_del(PG_FUNCTION_ARGS)
         * tuple.
         *
         * fk_rel is opened in RowShareLock mode since that's what our eventual
-        * SELECT FOR UPDATE will get on it.
+        * SELECT FOR SHARE will get on it.
         */
        fk_rel = heap_open(trigdata->tg_trigger->tgconstrrelid, RowShareLock);
        pk_rel = trigdata->tg_relation;
@@ -1453,7 +1453,7 @@ RI_FKey_restrict_del(PG_FUNCTION_ARGS)
                                        queryoids[i] = SPI_gettypeid(pk_rel->rd_att,
                                                                         qkey.keypair[i][RI_KEYPAIR_PK_IDX]);
                                }
-                               strcat(querystr, " FOR UPDATE OF x");
+                               strcat(querystr, " FOR SHARE OF x");
 
                                /* Prepare and save the plan */
                                qplan = ri_PlanCheck(querystr, qkey.nkeypairs, queryoids,
@@ -1543,7 +1543,7 @@ RI_FKey_restrict_upd(PG_FUNCTION_ARGS)
         * and old tuple.
         *
         * fk_rel is opened in RowShareLock mode since that's what our eventual
-        * SELECT FOR UPDATE will get on it.
+        * SELECT FOR SHARE will get on it.
         */
        fk_rel = heap_open(trigdata->tg_trigger->tgconstrrelid, RowShareLock);
        pk_rel = trigdata->tg_relation;
@@ -1634,7 +1634,7 @@ RI_FKey_restrict_upd(PG_FUNCTION_ARGS)
                                        queryoids[i] = SPI_gettypeid(pk_rel->rd_att,
                                                                         qkey.keypair[i][RI_KEYPAIR_PK_IDX]);
                                }
-                               strcat(querystr, " FOR UPDATE OF x");
+                               strcat(querystr, " FOR SHARE OF x");
 
                                /* Prepare and save the plan */
                                qplan = ri_PlanCheck(querystr, qkey.nkeypairs, queryoids,
index 43909008863f39faa19685d827288188d2e47baf..916d840ea34320e79c58359dea9b0cc19f9fb8a6 100644 (file)
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/utils/time/tqual.c,v 1.86 2005/03/20 23:40:27 neilc Exp $
+ *       $PostgreSQL: pgsql/src/backend/utils/time/tqual.c,v 1.87 2005/04/28 21:47:16 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
 
 #include "postgres.h"
 
+#include "access/multixact.h"
 #include "access/subtrans.h"
 #include "storage/sinval.h"
 #include "utils/tqual.h"
@@ -129,7 +130,12 @@ HeapTupleSatisfiesItself(HeapTupleHeader tuple, Buffer buffer)
                        if (tuple->t_infomask & HEAP_XMAX_INVALID)      /* xid invalid */
                                return true;
 
-                       /* deleting subtransaction aborted */
+                       if (tuple->t_infomask & HEAP_IS_LOCKED)         /* not deleter */
+                               return true;
+
+                       Assert(!(tuple->t_infomask & HEAP_XMAX_IS_MULTI));
+
+                       /* deleting subtransaction aborted? */
                        if (TransactionIdDidAbort(HeapTupleHeaderGetXmax(tuple)))
                        {
                                tuple->t_infomask |= HEAP_XMAX_INVALID;
@@ -139,9 +145,6 @@ HeapTupleSatisfiesItself(HeapTupleHeader tuple, Buffer buffer)
 
                        Assert(TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmax(tuple)));
 
-                       if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
-                               return true;
-
                        return false;
                }
                else if (!TransactionIdDidCommit(HeapTupleHeaderGetXmin(tuple)))
@@ -167,14 +170,21 @@ HeapTupleSatisfiesItself(HeapTupleHeader tuple, Buffer buffer)
 
        if (tuple->t_infomask & HEAP_XMAX_COMMITTED)
        {
-               if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
+               if (tuple->t_infomask & HEAP_IS_LOCKED)
                        return true;
                return false;                   /* updated by other */
        }
 
+       if (tuple->t_infomask & HEAP_XMAX_IS_MULTI)
+       {
+               /* MultiXacts are currently only allowed to lock tuples */
+               Assert(tuple->t_infomask & HEAP_IS_LOCKED);
+               return true;
+       }
+
        if (TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmax(tuple)))
        {
-               if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
+               if (tuple->t_infomask & HEAP_IS_LOCKED)
                        return true;
                return false;
        }
@@ -191,7 +201,7 @@ HeapTupleSatisfiesItself(HeapTupleHeader tuple, Buffer buffer)
 
        /* xmax transaction committed */
 
-       if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
+       if (tuple->t_infomask & HEAP_IS_LOCKED)
        {
                tuple->t_infomask |= HEAP_XMAX_INVALID;
                SetBufferCommitInfoNeedsSave(buffer);
@@ -300,7 +310,12 @@ HeapTupleSatisfiesNow(HeapTupleHeader tuple, Buffer buffer)
                        if (tuple->t_infomask & HEAP_XMAX_INVALID)      /* xid invalid */
                                return true;
 
-                       /* deleting subtransaction aborted */
+                       if (tuple->t_infomask & HEAP_IS_LOCKED)         /* not deleter */
+                               return true;
+
+                       Assert(!(tuple->t_infomask & HEAP_XMAX_IS_MULTI));
+
+                       /* deleting subtransaction aborted? */
                        if (TransactionIdDidAbort(HeapTupleHeaderGetXmax(tuple)))
                        {
                                tuple->t_infomask |= HEAP_XMAX_INVALID;
@@ -310,9 +325,6 @@ HeapTupleSatisfiesNow(HeapTupleHeader tuple, Buffer buffer)
 
                        Assert(TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmax(tuple)));
 
-                       if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
-                               return true;
-
                        if (HeapTupleHeaderGetCmax(tuple) >= GetCurrentCommandId())
                                return true;    /* deleted after scan started */
                        else
@@ -341,14 +353,21 @@ HeapTupleSatisfiesNow(HeapTupleHeader tuple, Buffer buffer)
 
        if (tuple->t_infomask & HEAP_XMAX_COMMITTED)
        {
-               if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
+               if (tuple->t_infomask & HEAP_IS_LOCKED)
                        return true;
                return false;
        }
 
+       if (tuple->t_infomask & HEAP_XMAX_IS_MULTI)
+       {
+               /* MultiXacts are currently only allowed to lock tuples */
+               Assert(tuple->t_infomask & HEAP_IS_LOCKED);
+               return true;
+       }
+
        if (TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmax(tuple)))
        {
-               if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
+               if (tuple->t_infomask & HEAP_IS_LOCKED)
                        return true;
                if (HeapTupleHeaderGetCmax(tuple) >= GetCurrentCommandId())
                        return true;            /* deleted after scan started */
@@ -368,7 +387,7 @@ HeapTupleSatisfiesNow(HeapTupleHeader tuple, Buffer buffer)
 
        /* xmax transaction committed */
 
-       if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
+       if (tuple->t_infomask & HEAP_IS_LOCKED)
        {
                tuple->t_infomask |= HEAP_XMAX_INVALID;
                SetBufferCommitInfoNeedsSave(buffer);
@@ -454,6 +473,22 @@ HeapTupleSatisfiesToast(HeapTupleHeader tuple, Buffer buffer)
  *     code, since UPDATE needs to know more than "is it visible?".  Also,
  *     tuples of my own xact are tested against the passed CommandId not
  *     CurrentCommandId.
+ *
+ *     The possible return codes are:
+ *
+ *     HeapTupleInvisible: the tuple didn't exist at all when the scan started,
+ *     e.g. it was created by a later CommandId.
+ *
+ *     HeapTupleMayBeUpdated: The tuple is valid and visible, so it may be
+ *     updated.
+ *
+ *     HeapTupleSelfUpdated: The tuple was updated by the current transaction,
+ *     after the current scan started.
+ *
+ *     HeapTupleUpdated: The tuple was updated by a committed transaction.
+ *
+ *     HeapTupleBeingUpdated: The tuple is being updated by an in-progress
+ *     transaction other than the current transaction.
  */
 HTSU_Result
 HeapTupleSatisfiesUpdate(HeapTupleHeader tuple, CommandId curcid,
@@ -512,7 +547,12 @@ HeapTupleSatisfiesUpdate(HeapTupleHeader tuple, CommandId curcid,
                        if (tuple->t_infomask & HEAP_XMAX_INVALID)      /* xid invalid */
                                return HeapTupleMayBeUpdated;
 
-                       /* deleting subtransaction aborted */
+                       if (tuple->t_infomask & HEAP_IS_LOCKED)         /* not deleter */
+                               return HeapTupleMayBeUpdated;
+
+                       Assert(!(tuple->t_infomask & HEAP_XMAX_IS_MULTI));
+
+                       /* deleting subtransaction aborted? */
                        if (TransactionIdDidAbort(HeapTupleHeaderGetXmax(tuple)))
                        {
                                tuple->t_infomask |= HEAP_XMAX_INVALID;
@@ -522,9 +562,6 @@ HeapTupleSatisfiesUpdate(HeapTupleHeader tuple, CommandId curcid,
 
                        Assert(TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmax(tuple)));
 
-                       if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
-                               return HeapTupleMayBeUpdated;
-
                        if (HeapTupleHeaderGetCmax(tuple) >= curcid)
                                return HeapTupleSelfUpdated;    /* updated after scan
                                                                                                 * started */
@@ -555,14 +592,26 @@ HeapTupleSatisfiesUpdate(HeapTupleHeader tuple, CommandId curcid,
 
        if (tuple->t_infomask & HEAP_XMAX_COMMITTED)
        {
-               if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
+               if (tuple->t_infomask & HEAP_IS_LOCKED)
                        return HeapTupleMayBeUpdated;
                return HeapTupleUpdated;        /* updated by other */
        }
 
+       if (tuple->t_infomask & HEAP_XMAX_IS_MULTI)
+       {
+               /* MultiXacts are currently only allowed to lock tuples */
+               Assert(tuple->t_infomask & HEAP_IS_LOCKED);
+
+               if (MultiXactIdIsRunning(HeapTupleHeaderGetXmax(tuple)))
+                       return HeapTupleBeingUpdated;
+               tuple->t_infomask |= HEAP_XMAX_INVALID;
+               SetBufferCommitInfoNeedsSave(buffer);
+               return HeapTupleMayBeUpdated;
+       }
+
        if (TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmax(tuple)))
        {
-               if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
+               if (tuple->t_infomask & HEAP_IS_LOCKED)
                        return HeapTupleMayBeUpdated;
                if (HeapTupleHeaderGetCmax(tuple) >= curcid)
                        return HeapTupleSelfUpdated;            /* updated after scan
@@ -585,7 +634,7 @@ HeapTupleSatisfiesUpdate(HeapTupleHeader tuple, CommandId curcid,
 
        /* xmax transaction committed */
 
-       if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
+       if (tuple->t_infomask & HEAP_IS_LOCKED)
        {
                tuple->t_infomask |= HEAP_XMAX_INVALID;
                SetBufferCommitInfoNeedsSave(buffer);
@@ -669,7 +718,12 @@ HeapTupleSatisfiesDirty(HeapTupleHeader tuple, Buffer buffer)
                        if (tuple->t_infomask & HEAP_XMAX_INVALID)      /* xid invalid */
                                return true;
 
-                       /* deleting subtransaction aborted */
+                       if (tuple->t_infomask & HEAP_IS_LOCKED)         /* not deleter */
+                               return true;
+
+                       Assert(!(tuple->t_infomask & HEAP_XMAX_IS_MULTI));
+
+                       /* deleting subtransaction aborted? */
                        if (TransactionIdDidAbort(HeapTupleHeaderGetXmax(tuple)))
                        {
                                tuple->t_infomask |= HEAP_XMAX_INVALID;
@@ -679,9 +733,6 @@ HeapTupleSatisfiesDirty(HeapTupleHeader tuple, Buffer buffer)
 
                        Assert(TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmax(tuple)));
 
-                       if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
-                               return true;
-
                        return false;
                }
                else if (!TransactionIdDidCommit(HeapTupleHeaderGetXmin(tuple)))
@@ -710,15 +761,22 @@ HeapTupleSatisfiesDirty(HeapTupleHeader tuple, Buffer buffer)
 
        if (tuple->t_infomask & HEAP_XMAX_COMMITTED)
        {
-               if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
+               if (tuple->t_infomask & HEAP_IS_LOCKED)
                        return true;
                SnapshotDirty->tid = tuple->t_ctid;
                return false;                   /* updated by other */
        }
 
+       if (tuple->t_infomask & HEAP_XMAX_IS_MULTI)
+       {
+               /* MultiXacts are currently only allowed to lock tuples */
+               Assert(tuple->t_infomask & HEAP_IS_LOCKED);
+               return true;
+       }
+
        if (TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmax(tuple)))
        {
-               if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
+               if (tuple->t_infomask & HEAP_IS_LOCKED)
                        return true;
                return false;
        }
@@ -738,7 +796,7 @@ HeapTupleSatisfiesDirty(HeapTupleHeader tuple, Buffer buffer)
 
        /* xmax transaction committed */
 
-       if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
+       if (tuple->t_infomask & HEAP_IS_LOCKED)
        {
                tuple->t_infomask |= HEAP_XMAX_INVALID;
                SetBufferCommitInfoNeedsSave(buffer);
@@ -828,7 +886,12 @@ HeapTupleSatisfiesSnapshot(HeapTupleHeader tuple, Snapshot snapshot,
                        if (tuple->t_infomask & HEAP_XMAX_INVALID)      /* xid invalid */
                                return true;
 
-                       /* deleting subtransaction aborted */
+                       if (tuple->t_infomask & HEAP_IS_LOCKED)         /* not deleter */
+                               return true;
+
+                       Assert(!(tuple->t_infomask & HEAP_XMAX_IS_MULTI));
+
+                       /* deleting subtransaction aborted? */
                        /* FIXME -- is this correct w.r.t. the cmax of the tuple? */
                        if (TransactionIdDidAbort(HeapTupleHeaderGetXmax(tuple)))
                        {
@@ -839,9 +902,6 @@ HeapTupleSatisfiesSnapshot(HeapTupleHeader tuple, Snapshot snapshot,
 
                        Assert(TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmax(tuple)));
 
-                       if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
-                               return true;
-
                        if (HeapTupleHeaderGetCmax(tuple) >= snapshot->curcid)
                                return true;    /* deleted after scan started */
                        else
@@ -902,8 +962,15 @@ HeapTupleSatisfiesSnapshot(HeapTupleHeader tuple, Snapshot snapshot,
        if (tuple->t_infomask & HEAP_XMAX_INVALID)      /* xid invalid or aborted */
                return true;
 
-       if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
+       if (tuple->t_infomask & HEAP_IS_LOCKED)
+               return true;
+
+       if (tuple->t_infomask & HEAP_XMAX_IS_MULTI)
+       {
+               /* MultiXacts are currently only allowed to lock tuples */
+               Assert(tuple->t_infomask & HEAP_IS_LOCKED);
                return true;
+       }
 
        if (!(tuple->t_infomask & HEAP_XMAX_COMMITTED))
        {
@@ -1043,7 +1110,7 @@ HeapTupleSatisfiesVacuum(HeapTupleHeader tuple, TransactionId OldestXmin,
                {
                        if (tuple->t_infomask & HEAP_XMAX_INVALID)      /* xid invalid */
                                return HEAPTUPLE_INSERT_IN_PROGRESS;
-                       if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
+                       if (tuple->t_infomask & HEAP_IS_LOCKED)
                                return HEAPTUPLE_INSERT_IN_PROGRESS;
                        /* inserted and then deleted by same xact */
                        return HEAPTUPLE_DELETE_IN_PROGRESS;
@@ -1074,22 +1141,30 @@ HeapTupleSatisfiesVacuum(HeapTupleHeader tuple, TransactionId OldestXmin,
        if (tuple->t_infomask & HEAP_XMAX_INVALID)
                return HEAPTUPLE_LIVE;
 
-       if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
+       if (tuple->t_infomask & HEAP_IS_LOCKED)
        {
                /*
-                * "Deleting" xact really only marked it for update, so the tuple
+                * "Deleting" xact really only locked it, so the tuple
                 * is live in any case.  However, we must make sure that either
                 * XMAX_COMMITTED or XMAX_INVALID gets set once the xact is gone;
                 * otherwise it is unsafe to recycle CLOG status after vacuuming.
                 */
                if (!(tuple->t_infomask & HEAP_XMAX_COMMITTED))
                {
-                       if (TransactionIdIsInProgress(HeapTupleHeaderGetXmax(tuple)))
-                               return HEAPTUPLE_LIVE;
+                       if (tuple->t_infomask & HEAP_XMAX_IS_MULTI)
+                       {
+                               if (MultiXactIdIsRunning(HeapTupleHeaderGetXmax(tuple)))
+                                       return HEAPTUPLE_LIVE;
+                       }
+                       else
+                       {
+                               if (TransactionIdIsInProgress(HeapTupleHeaderGetXmax(tuple)))
+                                       return HEAPTUPLE_LIVE;
+                       }
 
                        /*
                         * We don't really care whether xmax did commit, abort or
-                        * crash. We know that xmax did mark the tuple for update, but
+                        * crash. We know that xmax did lock the tuple, but
                         * it did not and will never actually update it.
                         */
                        tuple->t_infomask |= HEAP_XMAX_INVALID;
@@ -1098,6 +1173,13 @@ HeapTupleSatisfiesVacuum(HeapTupleHeader tuple, TransactionId OldestXmin,
                return HEAPTUPLE_LIVE;
        }
 
+       if (tuple->t_infomask & HEAP_XMAX_IS_MULTI)
+       {
+               /* MultiXacts are currently only allowed to lock tuples */
+               Assert(tuple->t_infomask & HEAP_IS_LOCKED);
+               return HEAPTUPLE_LIVE;
+       }
+
        if (!(tuple->t_infomask & HEAP_XMAX_COMMITTED))
        {
                if (TransactionIdIsInProgress(HeapTupleHeaderGetXmax(tuple)))
index ff524d579090b63dfbf256bbc4c876eaa0161b03..7d424749d624e6e4e6f0c4e3c2685283c40de582 100644 (file)
@@ -39,7 +39,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  * Portions taken from FreeBSD.
  *
- * $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.81 2005/04/12 19:29:24 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.82 2005/04/28 21:47:16 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -527,7 +527,7 @@ mkdir_p(char *path, mode_t omode)
                {
                        /*
                         * POSIX 1003.2: For each dir operand that does not name an
-                        * existing directory, effects equivalent to those cased by
+                        * existing directory, effects equivalent to those caused by
                         * the following command shall occcur:
                         *
                         * mkdir -p -m $(umask -S),u+wx $(dirname dir) && mkdir [-m mode]
@@ -2124,6 +2124,8 @@ main(int argc, char *argv[])
                "pg_xlog/archive_status",
                "pg_clog",
                "pg_subtrans",
+               "pg_multixact/members",
+               "pg_multixact/offsets",
                "base",
                "base/1",
                "pg_tblspc"
index bfe7da2ad5053292a682d24438edbf4aa215d640..d89a934dfc2abf2798a82d6eb5d3d2ec5d745e92 100644 (file)
@@ -6,7 +6,7 @@
  * copyright (c) Oliver Elphick <olly@lfix.co.uk>, 2001;
  * licence: BSD
  *
- * $PostgreSQL: pgsql/src/bin/pg_controldata/pg_controldata.c,v 1.22 2005/03/29 03:01:32 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_controldata/pg_controldata.c,v 1.23 2005/04/28 21:47:16 tgl Exp $
  */
 #include "postgres.h"
 
@@ -165,6 +165,7 @@ main(int argc, char *argv[])
        printf(_("Latest checkpoint's TimeLineID:       %u\n"), ControlFile.checkPointCopy.ThisTimeLineID);
        printf(_("Latest checkpoint's NextXID:          %u\n"), ControlFile.checkPointCopy.nextXid);
        printf(_("Latest checkpoint's NextOID:          %u\n"), ControlFile.checkPointCopy.nextOid);
+       printf(_("Latest checkpoint's NextMultiXactId:  %u\n"), ControlFile.checkPointCopy.nextMulti);
        printf(_("Time of latest checkpoint:            %s\n"), ckpttime_str);
        printf(_("Database block size:                  %u\n"), ControlFile.blcksz);
        printf(_("Blocks per segment of large relation: %u\n"), ControlFile.relseg_size);
index c47a35340dc0ea3c10a58825ba4de4d002e5e20e..cabc5c00124cf4e9cf070e3171285f9507cf2427 100644 (file)
@@ -23,7 +23,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/bin/pg_resetxlog/pg_resetxlog.c,v 1.31 2005/04/13 18:54:56 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_resetxlog/pg_resetxlog.c,v 1.32 2005/04/28 21:47:16 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -40,6 +40,7 @@
 #include <getopt.h>
 #endif
 
+#include "access/multixact.h"
 #include "access/xlog.h"
 #include "access/xlog_internal.h"
 #include "catalog/catversion.h"
@@ -75,6 +76,7 @@ main(int argc, char *argv[])
        bool            noupdate = false;
        TransactionId set_xid = 0;
        Oid                     set_oid = 0;
+       MultiXactId     set_mxid = 0;
        uint32          minXlogTli = 0,
                                minXlogId = 0,
                                minXlogSeg = 0;
@@ -104,7 +106,7 @@ main(int argc, char *argv[])
        }
 
 
-       while ((c = getopt(argc, argv, "fl:no:x:")) != -1)
+       while ((c = getopt(argc, argv, "fl:m:no:x:")) != -1)
        {
                switch (c)
                {
@@ -146,6 +148,21 @@ main(int argc, char *argv[])
                                }
                                break;
 
+                       case 'm':
+                               set_mxid = strtoul(optarg, &endptr, 0);
+                               if (endptr == optarg || *endptr != '\0')
+                               {
+                                       fprintf(stderr, _("%s: invalid argument for option -m\n"), progname);
+                                       fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
+                                       exit(1);
+                               }
+                               if (set_mxid == 0)
+                               {
+                                       fprintf(stderr, _("%s: multi transaction ID (-m) must not be 0\n"), progname);
+                                       exit(1);
+                               }
+                               break;
+
                        case 'l':
                                minXlogTli = strtoul(optarg, &endptr, 0);
                                if (endptr == optarg || *endptr != ',')
@@ -245,6 +262,9 @@ main(int argc, char *argv[])
        if (set_oid != 0)
                ControlFile.checkPointCopy.nextOid = set_oid;
 
+       if (set_mxid != 0)
+               ControlFile.checkPointCopy.nextMulti = set_mxid;
+
        if (minXlogTli > ControlFile.checkPointCopy.ThisTimeLineID)
                ControlFile.checkPointCopy.ThisTimeLineID = minXlogTli;
 
@@ -405,6 +425,7 @@ GuessControlValues(void)
        ControlFile.checkPointCopy.ThisTimeLineID = 1;
        ControlFile.checkPointCopy.nextXid = (TransactionId) 514;       /* XXX */
        ControlFile.checkPointCopy.nextOid = FirstBootstrapObjectId;
+       ControlFile.checkPointCopy.nextMulti = FirstMultiXactId;
        ControlFile.checkPointCopy.time = time(NULL);
 
        ControlFile.state = DB_SHUTDOWNED;
@@ -478,6 +499,7 @@ PrintControlValues(bool guessed)
        printf(_("Latest checkpoint's TimeLineID:       %u\n"), ControlFile.checkPointCopy.ThisTimeLineID);
        printf(_("Latest checkpoint's NextXID:          %u\n"), ControlFile.checkPointCopy.nextXid);
        printf(_("Latest checkpoint's NextOID:          %u\n"), ControlFile.checkPointCopy.nextOid);
+       printf(_("Latest checkpoint's NextMultiXactId:  %u\n"), ControlFile.checkPointCopy.nextMulti);
        printf(_("Database block size:                  %u\n"), ControlFile.blcksz);
        printf(_("Blocks per segment of large relation: %u\n"), ControlFile.relseg_size);
        printf(_("Maximum length of identifiers:        %u\n"), ControlFile.nameDataLen);
@@ -753,6 +775,7 @@ usage(void)
        printf(_("  -n              no update, just show extracted control values (for testing)\n"));
        printf(_("  -o OID          set next OID\n"));
        printf(_("  -x XID          set next transaction ID\n"));
+       printf(_("  -m multiXID     set next multi transaction ID\n"));
        printf(_("  --help          show this help, then exit\n"));
        printf(_("  --version       output version information, then exit\n"));
        printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
index 8b18cc422481e8d0c79ffbd77063a503d4708c9b..9138186ba63c71ec827d85f2eaa0730204f36dac 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/access/heapam.h,v 1.99 2005/04/14 20:03:27 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/access/heapam.h,v 1.100 2005/04/28 21:47:16 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -123,6 +123,12 @@ extern Datum fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc,
 
 /* heapam.c */
 
+typedef enum
+{
+       LockTupleShared,
+       LockTupleExclusive
+} LockTupleMode;
+
 extern Relation relation_open(Oid relationId, LOCKMODE lockmode);
 extern Relation conditional_relation_open(Oid relationId, LOCKMODE lockmode, bool nowait);
 extern Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode);
@@ -155,8 +161,8 @@ extern HTSU_Result heap_delete(Relation relation, ItemPointer tid, ItemPointer c
                        CommandId cid, Snapshot crosscheck, bool wait);
 extern HTSU_Result heap_update(Relation relation, ItemPointer otid, HeapTuple tup,
                ItemPointer ctid, CommandId cid, Snapshot crosscheck, bool wait);
-extern HTSU_Result heap_mark4update(Relation relation, HeapTuple tup,
-                                Buffer *userbuf, CommandId cid);
+extern HTSU_Result heap_lock_tuple(Relation relation, HeapTuple tup,
+                                Buffer *userbuf, CommandId cid, LockTupleMode mode);
 
 extern Oid     simple_heap_insert(Relation relation, HeapTuple tup);
 extern void simple_heap_delete(Relation relation, ItemPointer tid);
index e57ce8a3cbefcbcf9a13a72e33e3079b6f579f79..adeb05fd560dae2e49ab77c3b299cbcb696f399c 100644 (file)
@@ -1,4 +1,4 @@
- /*-------------------------------------------------------------------------
+/*-------------------------------------------------------------------------
  *
  * htup.h
  *       POSTGRES heap tuple definitions.
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/access/htup.h,v 1.73 2005/03/28 01:50:34 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/access/htup.h,v 1.74 2005/04/28 21:47:17 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -93,11 +93,11 @@ typedef struct HeapTupleFields
 {
        TransactionId t_xmin;           /* inserting xact ID */
        CommandId       t_cmin;                 /* inserting command ID */
-       TransactionId t_xmax;           /* deleting xact ID */
+       TransactionId t_xmax;           /* deleting or locking xact ID */
 
        union
        {
-               CommandId       t_cmax;         /* deleting command ID */
+               CommandId       t_cmax;         /* deleting or locking command ID */
                TransactionId t_xvac;   /* VACUUM FULL xact ID */
        }                       t_field4;
 } HeapTupleFields;
@@ -152,12 +152,16 @@ typedef HeapTupleHeaderData *HeapTupleHeader;
                                                                                 * attribute(s) */
 #define HEAP_HASEXTENDED               0x000C  /* the two above combined */
 #define HEAP_HASOID                            0x0010  /* has an object-id field */
-/* 0x0020, 0x0040 and 0x0080 are unused */
+/* 0x0020 is presently unused */
+#define HEAP_XMAX_EXCL_LOCK            0x0040  /* xmax is exclusive locker */
+#define HEAP_XMAX_SHARED_LOCK  0x0080  /* xmax is shared locker */
+/* if either LOCK bit is set, xmax hasn't deleted the tuple, only locked it */
+#define HEAP_IS_LOCKED (HEAP_XMAX_EXCL_LOCK | HEAP_XMAX_SHARED_LOCK)
 #define HEAP_XMIN_COMMITTED            0x0100  /* t_xmin committed */
 #define HEAP_XMIN_INVALID              0x0200  /* t_xmin invalid/aborted */
 #define HEAP_XMAX_COMMITTED            0x0400  /* t_xmax committed */
 #define HEAP_XMAX_INVALID              0x0800  /* t_xmax invalid/aborted */
-#define HEAP_MARKED_FOR_UPDATE 0x1000  /* marked for UPDATE */
+#define HEAP_XMAX_IS_MULTI             0x1000  /* t_xmax is a MultiXactId */
 #define HEAP_UPDATED                   0x2000  /* this is UPDATEd version of row */
 #define HEAP_MOVED_OFF                 0x4000  /* moved to another place by
                                                                                 * VACUUM FULL */
@@ -406,7 +410,8 @@ typedef HeapTupleData *HeapTuple;
 #define XLOG_HEAP_MOVE         0x30
 #define XLOG_HEAP_CLEAN                0x40
 #define XLOG_HEAP_NEWPAGE      0x50
-/* opcodes 0x60, 0x70 still free */
+#define XLOG_HEAP_LOCK         0x60
+/* opcode 0x70 still free */
 #define XLOG_HEAP_OPMASK       0x70
 /*
  * When we insert 1st item on new page in INSERT/UPDATE
@@ -496,4 +501,13 @@ typedef struct xl_heap_newpage
 
 #define SizeOfHeapNewpage      (offsetof(xl_heap_newpage, blkno) + sizeof(BlockNumber))
 
+/* This is what we need to know about lock */
+typedef struct xl_heap_lock
+{
+       xl_heaptid      target;                 /* locked tuple id */
+       bool            shared_lock;    /* shared or exclusive row lock? */
+} xl_heap_lock;
+
+#define SizeOfHeapLock (offsetof(xl_heap_lock, shared_lock) + sizeof(bool))
+
 #endif   /* HTUP_H */
diff --git a/src/include/access/multixact.h b/src/include/access/multixact.h
new file mode 100644 (file)
index 0000000..1eafddb
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * multixact.h
+ *
+ * PostgreSQL multi-transaction-log manager
+ *
+ * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * $PostgreSQL: pgsql/src/include/access/multixact.h,v 1.1 2005/04/28 21:47:17 tgl Exp $
+ */
+#ifndef MULTIXACT_H
+#define MULTIXACT_H
+
+#define InvalidMultiXactId     ((MultiXactId) 0)
+#define FirstMultiXactId       ((MultiXactId) 1)
+
+#define MultiXactIdIsValid(multi) ((multi) != InvalidMultiXactId)
+
+extern void MultiXactIdWait(MultiXactId multi);
+extern MultiXactId MultiXactIdExpand(MultiXactId multi, bool isMulti,
+                                                                        TransactionId xid);
+extern bool MultiXactIdIsRunning(MultiXactId multi);
+extern void MultiXactIdSetOldestMember(void);
+
+extern void AtEOXact_MultiXact(void);
+
+extern int     MultiXactShmemSize(void);
+extern void MultiXactShmemInit(void);
+extern void BootStrapMultiXact(void);
+extern void StartupMultiXact(void);
+extern void ShutdownMultiXact(void);
+extern MultiXactId MultiXactGetCheckptMulti(bool is_shutdown);
+extern void CheckPointMultiXact(void);
+extern void MultiXactSetNextMXact(MultiXactId nextMulti);
+extern void MultiXactAdvanceNextMXact(MultiXactId minMulti);
+
+#endif   /* MULTIXACT_H */
index 41b8bde0118bdf90b8ae4d569e949e30af69b817..27a076c7e0d02ae7df57e1b72fd418df265b59be 100644 (file)
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/access/xlog.h,v 1.59 2004/12/31 22:03:21 pgsql Exp $
+ * $PostgreSQL: pgsql/src/include/access/xlog.h,v 1.60 2005/04/28 21:47:17 tgl Exp $
  */
 #ifndef XLOG_H
 #define XLOG_H
@@ -133,6 +133,7 @@ extern void ShutdownXLOG(int code, Datum arg);
 extern void InitXLOGAccess(void);
 extern void CreateCheckPoint(bool shutdown, bool force);
 extern void XLogPutNextOid(Oid nextOid);
+extern void XLogPutNextMultiXactId(MultiXactId multi);
 extern XLogRecPtr GetRedoRecPtr(void);
 
 #endif   /* XLOG_H */
index ebed0ab94548972f45d2e1bce5383c240a30d617..31d843fde753611532a5090a81e9d4e4ea2565ff 100644 (file)
@@ -12,7 +12,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/c.h,v 1.181 2005/03/29 00:17:16 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/c.h,v 1.182 2005/04/28 21:47:17 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -365,7 +365,8 @@ typedef float float4;
 typedef double float8;
 
 /*
- * Oid, RegProcedure, TransactionId, SubTransactionId, CommandId, AclId
+ * Oid, RegProcedure, TransactionId, SubTransactionId, MultiXactId,
+ * CommandId, AclId
  */
 
 /* typedef Oid is in postgres_ext.h */
@@ -384,6 +385,9 @@ typedef uint32 SubTransactionId;
 #define InvalidSubTransactionId                ((SubTransactionId) 0)
 #define TopSubTransactionId                    ((SubTransactionId) 1)
 
+/* MultiXactId must be equivalent to TransactionId, to fit in t_xmax */
+typedef TransactionId MultiXactId;
+
 typedef uint32 CommandId;
 
 #define FirstCommandId ((CommandId) 0)
index 83573334fc58c3e78cec61d499f8d23eff9b7268..e60a879424a3462ba0873c9ef8115958d6fadd61 100644 (file)
@@ -8,7 +8,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/catalog/pg_control.h,v 1.20 2005/03/29 03:01:32 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/catalog/pg_control.h,v 1.21 2005/04/28 21:47:17 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -22,7 +22,7 @@
 
 
 /* Version identifier for this pg_control format */
-#define PG_CONTROL_VERSION     74
+#define PG_CONTROL_VERSION     81
 
 /*
  * Body of CheckPoint XLOG records.  This is declared here because we keep
@@ -39,12 +39,14 @@ typedef struct CheckPoint
        TimeLineID      ThisTimeLineID; /* current TLI */
        TransactionId nextXid;          /* next free XID */
        Oid                     nextOid;                /* next free OID */
+       MultiXactId     nextMulti;              /* next free MultiXactId */
        time_t          time;                   /* time stamp of checkpoint */
 } CheckPoint;
 
 /* XLOG info values for XLOG rmgr */
 #define XLOG_CHECKPOINT_SHUTDOWN               0x00
 #define XLOG_CHECKPOINT_ONLINE                 0x10
+#define XLOG_NEXTMULTI                                 0x20
 #define XLOG_NEXTOID                                   0x30
 
 
index ba665025df6379bb38667a3507121fa70678983d..1280aff0ca091d6bace5818291ee8dec03d311f7 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/nodes/execnodes.h,v 1.129 2005/04/25 01:30:14 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/nodes/execnodes.h,v 1.130 2005/04/28 21:47:17 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -318,6 +318,7 @@ typedef struct EState
        uint32          es_processed;   /* # of tuples processed */
        Oid                     es_lastoid;             /* last oid processed (by INSERT) */
        List       *es_rowMark;         /* not good place, but there is no other */
+       bool            es_forUpdate;   /* was it FOR UPDATE or FOR SHARE */
 
        bool            es_instrument;  /* true requests runtime instrumentation */
        bool            es_select_into; /* true if doing SELECT INTO */
index e8a44c34b2f18241edba6aee3735dc446acb5370..97d5df935ac13cd4052a6d581e6faf870c63aadd 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.277 2005/04/07 01:51:40 neilc Exp $
+ * $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.278 2005/04/28 21:47:17 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -50,7 +50,7 @@ typedef uint32 AclMode;                       /* a bitmask of privilege bits */
 #define N_ACL_RIGHTS   11              /* 1 plus the last 1<<x */
 #define ACL_ALL_RIGHTS (-1)    /* all-privileges marker in GRANT list */
 #define ACL_NO_RIGHTS  0
-/* Currently, SELECT ... FOR UPDATE requires UPDATE privileges */
+/* Currently, SELECT ... FOR UPDATE/FOR SHARE requires UPDATE privileges */
 #define ACL_SELECT_FOR_UPDATE  ACL_UPDATE
 
 
@@ -91,7 +91,10 @@ typedef struct Query
                                                                 * clauses) */
 
        List       *rowMarks;           /* integer list of RT indexes of relations
-                                                                * that are selected FOR UPDATE */
+                                                                * that are selected FOR UPDATE/SHARE */
+
+       bool            forUpdate;              /* true if rowMarks are FOR UPDATE,
+                                                                * false if they are FOR SHARE */
 
        List       *targetList;         /* target list (of TargetEntry) */
 
@@ -688,7 +691,8 @@ typedef struct SelectStmt
        List       *sortClause;         /* sort clause (a list of SortBy's) */
        Node       *limitOffset;        /* # of result tuples to skip */
        Node       *limitCount;         /* # of result tuples to return */
-       List       *forUpdate;          /* FOR UPDATE clause */
+       List       *lockedRels;         /* FOR UPDATE or FOR SHARE relations */
+       bool            forUpdate;              /* true = FOR UPDATE, false = FOR SHARE */
 
        /*
         * These fields are used only in upper-level SelectStmts.
index bec10e8bb09b8c83bb324d560cb1a218f2561f4b..73cbb2fc797eb880e037df097032d2c454f1aee4 100644 (file)
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/parser/analyze.h,v 1.29 2004/12/31 22:03:38 pgsql Exp $
+ * $PostgreSQL: pgsql/src/include/parser/analyze.h,v 1.30 2005/04/28 21:47:18 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -21,6 +21,6 @@ extern List *parse_analyze_varparams(Node *parseTree, Oid **paramTypes,
                                                int *numParams);
 extern List *parse_sub_analyze(Node *parseTree, ParseState *parentParseState);
 extern List *analyzeCreateSchemaStmt(CreateSchemaStmt *stmt);
-extern void CheckSelectForUpdate(Query *qry);
+extern void CheckSelectLocking(Query *qry, bool forUpdate);
 
 #endif   /* ANALYZE_H */
index 3999918f248647ed05cf6e4a9f2fff1d555b7005..bfcf00fd47d7ea922f6e983cbbe7ed5486d74069 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/parser/parse_node.h,v 1.42 2004/12/31 22:03:38 pgsql Exp $
+ * $PostgreSQL: pgsql/src/include/parser/parse_node.h,v 1.43 2005/04/28 21:47:18 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -53,7 +53,7 @@ typedef struct ParseState
        Oid                *p_paramtypes;       /* OIDs of types for $n parameter symbols */
        int                     p_numparams;    /* allocated size of p_paramtypes[] */
        int                     p_next_resno;   /* next targetlist resno to assign */
-       List       *p_forUpdate;        /* FOR UPDATE clause, if any (see gram.y) */
+       List       *p_lockedRels;       /* FOR UPDATE/SHARE, if any (see gram.y) */
        Node       *p_value_substitute;         /* what to replace VALUE with, if
                                                                                 * any */
        bool            p_variableparams;
index 8b195132cb9e94e8b6f3a538f7193ca171650009..c02bb4dc8758f839ceeafc280ba00421487324ce 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/storage/bufpage.h,v 1.64 2005/03/22 06:17:03 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/storage/bufpage.h,v 1.65 2005/04/28 21:47:18 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -135,8 +135,9 @@ typedef PageHeaderData *PageHeader;
  * Page layout version number 0 is for pre-7.3 Postgres releases.
  * Releases 7.3 and 7.4 use 1, denoting a new HeapTupleHeader layout.
  * Release 8.0 changed the HeapTupleHeader layout again.
+ * Release 8.1 redefined HeapTupleHeader infomask bits.
  */
-#define PG_PAGE_LAYOUT_VERSION         2
+#define PG_PAGE_LAYOUT_VERSION         3
 
 
 /* ----------------------------------------------------------------
index 6aec7bf19c2a3a44283ba71ba6a246c31ccfc1ab..8d63294f5cdeb9a82094a68777461efbc90fd040 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/storage/lmgr.h,v 1.45 2004/12/31 22:03:42 pgsql Exp $
+ * $PostgreSQL: pgsql/src/include/storage/lmgr.h,v 1.46 2005/04/28 21:47:18 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -23,7 +23,7 @@
 #define NoLock                                 0
 
 #define AccessShareLock                        1               /* SELECT */
-#define RowShareLock                   2               /* SELECT FOR UPDATE */
+#define RowShareLock                   2               /* SELECT FOR UPDATE/FOR SHARE */
 #define RowExclusiveLock               3               /* INSERT, UPDATE, DELETE */
 #define ShareUpdateExclusiveLock 4             /* VACUUM (non-FULL) */
 #define ShareLock                              5               /* CREATE INDEX */
index 50c029cc8b005321387f2f7bc006319eda492765..18215f48381f8a3c0e2d8702353c61a8b56c6498 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/storage/lwlock.h,v 1.17 2005/03/04 20:21:07 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/storage/lwlock.h,v 1.18 2005/04/28 21:47:18 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -40,6 +40,9 @@ typedef enum LWLockId
        CheckpointStartLock,
        CLogControlLock,
        SubtransControlLock,
+       MultiXactGenLock,
+       MultiXactOffsetControlLock,
+       MultiXactMemberControlLock,
        RelCacheInitLock,
        BgWriterCommLock,