<!--
-$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">
</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,
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>
</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>
</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>
<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>
<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>
<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
<!--
-$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
-->
<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.
<!--
-$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
-->
<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>
<!--
-$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
-->
<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>
</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>
<!--
-$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
-->
[ 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:
<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>
<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>
</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
</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>
</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>
</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>
</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>
</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>
</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:
</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
</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>
<!--
-$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
-->
[ 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>
<!--
-$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">
[ 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>
<!--
-$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">
<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>
*
*
* 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"
}
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;
/* 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);
}
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;
{
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);
{
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);
}
/*
- * 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);
{
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);
}
/*
- * 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);
/* ----------------
* 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
/* ----------------
* 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
{
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);
{
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);
{
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);
}
+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)
{
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);
}
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);
}
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");
}
# 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 $
#
#-------------------------------------------------------------------------
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
--- /dev/null
+/*-------------------------------------------------------------------------
+ *
+ * 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);
+}
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
#include <time.h>
#include <unistd.h>
+#include "access/multixact.h"
#include "access/subtrans.h"
#include "access/xact.h"
#include "catalog/heap.h"
*/
smgrDoPendingDeletes(true);
+ AtEOXact_MultiXact();
+
ResourceOwnerRelease(TopTransactionResourceOwner,
RESOURCE_RELEASE_LOCKS,
true, true);
AtEOXact_Buffers(false);
AtEOXact_Inval(false);
smgrDoPendingDeletes(false);
+ AtEOXact_MultiXact();
ResourceOwnerRelease(TopTransactionResourceOwner,
RESOURCE_RELEASE_LOCKS,
false, true);
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);
}
}
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),
* 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 $
*
*-------------------------------------------------------------------------
*/
#include <sys/time.h>
#include "access/clog.h"
+#include "access/multixact.h"
#include "access/subtrans.h"
#include "access/xact.h"
#include "access/xlog.h"
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;
/* Bootstrap the commit log, too */
BootStrapCLOG();
BootStrapSUBTRANS();
+ BootStrapMultiXact();
}
static char *
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")));
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
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")));
CreateCheckPoint(true, true);
ShutdownCLOG();
ShutdownSUBTRANS();
+ ShutdownMultiXact();
CritSectionCount--;
ereport(LOG,
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.
CheckPointCLOG();
CheckPointSUBTRANS();
+ CheckPointMultiXact();
FlushBufferPool();
START_CRIT_SECTION();
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.
+ */
}
/*
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;
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
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,
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)
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");
}
*
*
* 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
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);
* 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))
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
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);
* 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 $
*
*-------------------------------------------------------------------------
*/
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:
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 */
}
}
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
!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)))))
{
* 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))))
{
* 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);
-$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
---------------------
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
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.
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
}
/*
- * 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;
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)
{
* ctid!! */
tupleid = &tuple_ctid;
}
+ /*
+ * Process any FOR UPDATE or FOR SHARE locking requested.
+ */
else if (estate->es_rowMark != NIL)
{
ListCell *l;
Buffer buffer;
HeapTupleData tuple;
TupleTableSlot *newSlot;
+ LockTupleMode lockmode;
HTSU_Result test;
if (!ExecGetJunkAttribute(junkfilter,
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)
{
goto lnext;
default:
- elog(ERROR, "unrecognized heap_mark4update status: %u",
+ elog(ERROR, "unrecognized heap_lock_tuple status: %u",
test);
return (NULL);
}
* 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)
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;
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
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;
* 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 $
*
*-------------------------------------------------------------------------
*/
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);
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);
* 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 $
*
*-------------------------------------------------------------------------
*/
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);
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);
*
*
* 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*
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);
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);
*
*
* 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
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);
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
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
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
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.
*/
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;
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
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
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
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
* 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 $
*
*-------------------------------------------------------------------------
*/
}
/*
- * 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.
*/
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)
{
* 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 $
*
*-------------------------------------------------------------------------
*/
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);
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);
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;
}
List *sortClause;
Node *limitOffset;
Node *limitCount;
- List *forUpdate;
+ List *lockedRels;
+ bool forUpdate;
Node *node;
ListCell *left_tlist,
*dtlist;
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.
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;
}
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
{
Assert(stmt->larg != NULL && stmt->rarg != NULL);
if (stmt->sortClause || stmt->limitOffset || stmt->limitCount ||
- stmt->forUpdate)
+ stmt->lockedRels)
isLeaf = true;
else
isLeaf = false;
/* 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;
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 */
else
{
/* just the named tables */
- foreach(l, forUpdate)
+ foreach(l, lockedRels)
{
char *relname = strVal(lfirst(l));
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",
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)));
}
}
*
*
* 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
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);
%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
;
/*
- * 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:
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));
| /*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; }
;
*/
static void
insertSelectOptions(SelectStmt *stmt,
- List *sortClause, List *forUpdate,
+ List *sortClause, List *lockingClause,
Node *limitOffset, Node *limitCount)
{
/*
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)
{
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
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,
* 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);
}
/*
- * 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;
/* just the named tables */
ListCell *l;
- foreach(l, pstate->p_forUpdate)
+ foreach(l, pstate->p_lockedRels)
{
char *rname = strVal(lfirst(l));
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
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)
* 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 $
*
*-------------------------------------------------------------------------
*/
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);
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);
}
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);
}
}
}
* 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;
*
*
* 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"
size += XLOGShmemSize();
size += CLOGShmemSize();
size += SUBTRANSShmemSize();
+ size += MultiXactShmemSize();
size += LWLockShmemSize();
size += SInvalShmemSize(maxBackends);
size += FreeSpaceShmemSize();
XLOGShmemInit();
CLOGShmemInit();
SUBTRANSShmemInit();
+ MultiXactShmemInit();
InitBufferPool();
/*
* 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 $
*
*-------------------------------------------------------------------------
*/
/* 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;
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
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:
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;
*
* 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 $
*
* ----------
*/
* 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;
* ----------
*/
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 */
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,
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,
* 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;
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,
* 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;
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,
* 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;
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,
* 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;
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,
* 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"
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;
Assert(TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmax(tuple)));
- if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
- return true;
-
return false;
}
else if (!TransactionIdDidCommit(HeapTupleHeaderGetXmin(tuple)))
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;
}
/* 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);
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;
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
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 */
/* 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);
* 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,
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;
Assert(TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmax(tuple)));
- if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
- return HeapTupleMayBeUpdated;
-
if (HeapTupleHeaderGetCmax(tuple) >= curcid)
return HeapTupleSelfUpdated; /* updated after scan
* started */
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
/* 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);
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;
Assert(TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmax(tuple)));
- if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)
- return true;
-
return false;
}
else if (!TransactionIdDidCommit(HeapTupleHeaderGetXmin(tuple)))
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;
}
/* 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);
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)))
{
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
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))
{
{
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;
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;
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)))
* 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 $
*
*-------------------------------------------------------------------------
*/
{
/*
* 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]
"pg_xlog/archive_status",
"pg_clog",
"pg_subtrans",
+ "pg_multixact/members",
+ "pg_multixact/offsets",
"base",
"base/1",
"pg_tblspc"
* 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"
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);
* 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 $
*
*-------------------------------------------------------------------------
*/
#include <getopt.h>
#endif
+#include "access/multixact.h"
#include "access/xlog.h"
#include "access/xlog_internal.h"
#include "catalog/catversion.h"
bool noupdate = false;
TransactionId set_xid = 0;
Oid set_oid = 0;
+ MultiXactId set_mxid = 0;
uint32 minXlogTli = 0,
minXlogId = 0,
minXlogSeg = 0;
}
- while ((c = getopt(argc, argv, "fl:no:x:")) != -1)
+ while ((c = getopt(argc, argv, "fl:m:no:x:")) != -1)
{
switch (c)
{
}
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 != ',')
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;
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;
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);
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"));
* 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 $
*
*-------------------------------------------------------------------------
*/
/* 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);
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);
- /*-------------------------------------------------------------------------
+/*-------------------------------------------------------------------------
*
* htup.h
* POSTGRES heap tuple definitions.
* 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 $
*
*-------------------------------------------------------------------------
*/
{
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;
* 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 */
#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
#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 */
--- /dev/null
+/*
+ * 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 */
* 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
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 */
* 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 $
*
*-------------------------------------------------------------------------
*/
typedef double float8;
/*
- * Oid, RegProcedure, TransactionId, SubTransactionId, CommandId, AclId
+ * Oid, RegProcedure, TransactionId, SubTransactionId, MultiXactId,
+ * CommandId, AclId
*/
/* typedef Oid is in postgres_ext.h */
#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)
* 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 $
*
*-------------------------------------------------------------------------
*/
/* 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
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
* 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 $
*
*-------------------------------------------------------------------------
*/
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 */
* 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 $
*
*-------------------------------------------------------------------------
*/
#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
* 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) */
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.
* 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 $
*
*-------------------------------------------------------------------------
*/
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 */
* 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 $
*
*-------------------------------------------------------------------------
*/
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;
* 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 $
*
*-------------------------------------------------------------------------
*/
* 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
/* ----------------------------------------------------------------
* 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 $
*
*-------------------------------------------------------------------------
*/
#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 */
* 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 $
*
*-------------------------------------------------------------------------
*/
CheckpointStartLock,
CLogControlLock,
SubtransControlLock,
+ MultiXactGenLock,
+ MultiXactOffsetControlLock,
+ MultiXactMemberControlLock,
RelCacheInitLock,
BgWriterCommLock,