2010-09-20 22:08:53 +02:00
|
|
|
<!-- doc/src/sgml/pgrowlocks.sgml -->
|
2007-11-11 00:30:46 +01:00
|
|
|
|
2011-05-08 04:29:20 +02:00
|
|
|
<sect1 id="pgrowlocks" xreflabel="pgrowlocks">
|
2007-11-11 00:30:46 +01:00
|
|
|
<title>pgrowlocks</title>
|
2007-12-10 06:32:51 +01:00
|
|
|
|
2007-11-11 00:30:46 +01:00
|
|
|
<indexterm zone="pgrowlocks">
|
|
|
|
<primary>pgrowlocks</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
|
|
|
<para>
|
2007-12-10 06:32:51 +01:00
|
|
|
The <filename>pgrowlocks</filename> module provides a function to show row
|
2007-11-11 00:30:46 +01:00
|
|
|
locking information for a specified table.
|
|
|
|
</para>
|
|
|
|
|
2017-03-30 20:18:53 +02:00
|
|
|
<para>
|
|
|
|
By default use is restricted to superusers, members of the
|
|
|
|
<literal>pg_stat_scan_tables</literal> role, and users with
|
|
|
|
<literal>SELECT</literal> permissions on the table.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
2007-11-11 00:30:46 +01:00
|
|
|
<sect2>
|
|
|
|
<title>Overview</title>
|
2007-12-10 06:32:51 +01:00
|
|
|
|
2013-07-04 17:33:08 +02:00
|
|
|
<indexterm>
|
|
|
|
<primary>pgrowlocks</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
2009-05-18 13:08:24 +02:00
|
|
|
<synopsis>
|
2007-12-10 06:32:51 +01:00
|
|
|
pgrowlocks(text) returns setof record
|
2009-05-18 13:08:24 +02:00
|
|
|
</synopsis>
|
2007-12-10 06:32:51 +01:00
|
|
|
|
2007-11-11 00:30:46 +01:00
|
|
|
<para>
|
2007-12-10 06:32:51 +01:00
|
|
|
The parameter is the name of a table. The result is a set of records,
|
|
|
|
with one row for each locked row within the table. The output columns
|
2017-11-23 15:39:47 +01:00
|
|
|
are shown in <xref linkend="pgrowlocks-columns"/>.
|
2007-11-11 00:30:46 +01:00
|
|
|
</para>
|
|
|
|
|
2009-05-18 13:08:24 +02:00
|
|
|
<table id="pgrowlocks-columns">
|
2017-10-09 03:44:17 +02:00
|
|
|
<title><function>pgrowlocks</function> Output Columns</title>
|
2007-12-10 06:32:51 +01:00
|
|
|
|
|
|
|
<tgroup cols="3">
|
|
|
|
<thead>
|
|
|
|
<row>
|
|
|
|
<entry>Name</entry>
|
|
|
|
<entry>Type</entry>
|
|
|
|
<entry>Description</entry>
|
|
|
|
</row>
|
|
|
|
</thead>
|
2007-11-11 00:30:46 +01:00
|
|
|
<tbody>
|
2007-12-10 06:32:51 +01:00
|
|
|
|
2007-11-11 00:30:46 +01:00
|
|
|
<row>
|
2007-12-10 06:32:51 +01:00
|
|
|
<entry><structfield>locked_row</structfield></entry>
|
|
|
|
<entry><type>tid</type></entry>
|
|
|
|
<entry>Tuple ID (TID) of locked row</entry>
|
2007-11-11 00:30:46 +01:00
|
|
|
</row>
|
|
|
|
<row>
|
2007-12-10 06:32:51 +01:00
|
|
|
<entry><structfield>locker</structfield></entry>
|
|
|
|
<entry><type>xid</type></entry>
|
2012-06-07 23:06:20 +02:00
|
|
|
<entry>Transaction ID of locker, or multixact ID if multitransaction</entry>
|
2007-11-11 00:30:46 +01:00
|
|
|
</row>
|
|
|
|
<row>
|
2007-12-10 06:32:51 +01:00
|
|
|
<entry><structfield>multi</structfield></entry>
|
|
|
|
<entry><type>boolean</type></entry>
|
2012-06-07 23:06:20 +02:00
|
|
|
<entry>True if locker is a multitransaction</entry>
|
2007-11-11 00:30:46 +01:00
|
|
|
</row>
|
|
|
|
<row>
|
2007-12-10 06:32:51 +01:00
|
|
|
<entry><structfield>xids</structfield></entry>
|
|
|
|
<entry><type>xid[]</type></entry>
|
2012-06-07 23:06:20 +02:00
|
|
|
<entry>Transaction IDs of lockers (more than one if multitransaction)</entry>
|
2007-11-11 00:30:46 +01:00
|
|
|
</row>
|
Improve concurrency of foreign key locking
This patch introduces two additional lock modes for tuples: "SELECT FOR
KEY SHARE" and "SELECT FOR NO KEY UPDATE". These don't block each
other, in contrast with already existing "SELECT FOR SHARE" and "SELECT
FOR UPDATE". UPDATE commands that do not modify the values stored in
the columns that are part of the key of the tuple now grab a SELECT FOR
NO KEY UPDATE lock on the tuple, allowing them to proceed concurrently
with tuple locks of the FOR KEY SHARE variety.
Foreign key triggers now use FOR KEY SHARE instead of FOR SHARE; this
means the concurrency improvement applies to them, which is the whole
point of this patch.
The added tuple lock semantics require some rejiggering of the multixact
module, so that the locking level that each transaction is holding can
be stored alongside its Xid. Also, multixacts now need to persist
across server restarts and crashes, because they can now represent not
only tuple locks, but also tuple updates. This means we need more
careful tracking of lifetime of pg_multixact SLRU files; since they now
persist longer, we require more infrastructure to figure out when they
can be removed. pg_upgrade also needs to be careful to copy
pg_multixact files over from the old server to the new, or at least part
of multixact.c state, depending on the versions of the old and new
servers.
Tuple time qualification rules (HeapTupleSatisfies routines) need to be
careful not to consider tuples with the "is multi" infomask bit set as
being only locked; they might need to look up MultiXact values (i.e.
possibly do pg_multixact I/O) to find out the Xid that updated a tuple,
whereas they previously were assured to only use information readily
available from the tuple header. This is considered acceptable, because
the extra I/O would involve cases that would previously cause some
commands to block waiting for concurrent transactions to finish.
Another important change is the fact that locking tuples that have
previously been updated causes the future versions to be marked as
locked, too; this is essential for correctness of foreign key checks.
This causes additional WAL-logging, also (there was previously a single
WAL record for a locked tuple; now there are as many as updated copies
of the tuple there exist.)
With all this in place, contention related to tuples being checked by
foreign key rules should be much reduced.
As a bonus, the old behavior that a subtransaction grabbing a stronger
tuple lock than the parent (sub)transaction held on a given tuple and
later aborting caused the weaker lock to be lost, has been fixed.
Many new spec files were added for isolation tester framework, to ensure
overall behavior is sane. There's probably room for several more tests.
There were several reviewers of this patch; in particular, Noah Misch
and Andres Freund spent considerable time in it. Original idea for the
patch came from Simon Riggs, after a problem report by Joel Jacobson.
Most code is from me, with contributions from Marti Raudsepp, Alexander
Shulgin, Noah Misch and Andres Freund.
This patch was discussed in several pgsql-hackers threads; the most
important start at the following message-ids:
AANLkTimo9XVcEzfiBR-ut3KVNDkjm2Vxh+t8kAmWjPuv@mail.gmail.com
1290721684-sup-3951@alvh.no-ip.org
1294953201-sup-2099@alvh.no-ip.org
1320343602-sup-2290@alvh.no-ip.org
1339690386-sup-8927@alvh.no-ip.org
4FE5FF020200002500048A3D@gw.wicourts.gov
4FEAB90A0200002500048B7D@gw.wicourts.gov
2013-01-23 16:04:59 +01:00
|
|
|
<row>
|
2018-10-02 09:34:41 +02:00
|
|
|
<entry><structfield>modes</structfield></entry>
|
Improve concurrency of foreign key locking
This patch introduces two additional lock modes for tuples: "SELECT FOR
KEY SHARE" and "SELECT FOR NO KEY UPDATE". These don't block each
other, in contrast with already existing "SELECT FOR SHARE" and "SELECT
FOR UPDATE". UPDATE commands that do not modify the values stored in
the columns that are part of the key of the tuple now grab a SELECT FOR
NO KEY UPDATE lock on the tuple, allowing them to proceed concurrently
with tuple locks of the FOR KEY SHARE variety.
Foreign key triggers now use FOR KEY SHARE instead of FOR SHARE; this
means the concurrency improvement applies to them, which is the whole
point of this patch.
The added tuple lock semantics require some rejiggering of the multixact
module, so that the locking level that each transaction is holding can
be stored alongside its Xid. Also, multixacts now need to persist
across server restarts and crashes, because they can now represent not
only tuple locks, but also tuple updates. This means we need more
careful tracking of lifetime of pg_multixact SLRU files; since they now
persist longer, we require more infrastructure to figure out when they
can be removed. pg_upgrade also needs to be careful to copy
pg_multixact files over from the old server to the new, or at least part
of multixact.c state, depending on the versions of the old and new
servers.
Tuple time qualification rules (HeapTupleSatisfies routines) need to be
careful not to consider tuples with the "is multi" infomask bit set as
being only locked; they might need to look up MultiXact values (i.e.
possibly do pg_multixact I/O) to find out the Xid that updated a tuple,
whereas they previously were assured to only use information readily
available from the tuple header. This is considered acceptable, because
the extra I/O would involve cases that would previously cause some
commands to block waiting for concurrent transactions to finish.
Another important change is the fact that locking tuples that have
previously been updated causes the future versions to be marked as
locked, too; this is essential for correctness of foreign key checks.
This causes additional WAL-logging, also (there was previously a single
WAL record for a locked tuple; now there are as many as updated copies
of the tuple there exist.)
With all this in place, contention related to tuples being checked by
foreign key rules should be much reduced.
As a bonus, the old behavior that a subtransaction grabbing a stronger
tuple lock than the parent (sub)transaction held on a given tuple and
later aborting caused the weaker lock to be lost, has been fixed.
Many new spec files were added for isolation tester framework, to ensure
overall behavior is sane. There's probably room for several more tests.
There were several reviewers of this patch; in particular, Noah Misch
and Andres Freund spent considerable time in it. Original idea for the
patch came from Simon Riggs, after a problem report by Joel Jacobson.
Most code is from me, with contributions from Marti Raudsepp, Alexander
Shulgin, Noah Misch and Andres Freund.
This patch was discussed in several pgsql-hackers threads; the most
important start at the following message-ids:
AANLkTimo9XVcEzfiBR-ut3KVNDkjm2Vxh+t8kAmWjPuv@mail.gmail.com
1290721684-sup-3951@alvh.no-ip.org
1294953201-sup-2099@alvh.no-ip.org
1320343602-sup-2290@alvh.no-ip.org
1339690386-sup-8927@alvh.no-ip.org
4FE5FF020200002500048A3D@gw.wicourts.gov
4FEAB90A0200002500048B7D@gw.wicourts.gov
2013-01-23 16:04:59 +01:00
|
|
|
<entry><type>text[]</type></entry>
|
|
|
|
<entry>Lock mode of lockers (more than one if multitransaction),
|
2017-10-09 03:44:17 +02:00
|
|
|
an array of <literal>Key Share</literal>, <literal>Share</literal>,
|
|
|
|
<literal>For No Key Update</literal>, <literal>No Key Update</literal>,
|
|
|
|
<literal>For Update</literal>, <literal>Update</literal>.</entry>
|
Improve concurrency of foreign key locking
This patch introduces two additional lock modes for tuples: "SELECT FOR
KEY SHARE" and "SELECT FOR NO KEY UPDATE". These don't block each
other, in contrast with already existing "SELECT FOR SHARE" and "SELECT
FOR UPDATE". UPDATE commands that do not modify the values stored in
the columns that are part of the key of the tuple now grab a SELECT FOR
NO KEY UPDATE lock on the tuple, allowing them to proceed concurrently
with tuple locks of the FOR KEY SHARE variety.
Foreign key triggers now use FOR KEY SHARE instead of FOR SHARE; this
means the concurrency improvement applies to them, which is the whole
point of this patch.
The added tuple lock semantics require some rejiggering of the multixact
module, so that the locking level that each transaction is holding can
be stored alongside its Xid. Also, multixacts now need to persist
across server restarts and crashes, because they can now represent not
only tuple locks, but also tuple updates. This means we need more
careful tracking of lifetime of pg_multixact SLRU files; since they now
persist longer, we require more infrastructure to figure out when they
can be removed. pg_upgrade also needs to be careful to copy
pg_multixact files over from the old server to the new, or at least part
of multixact.c state, depending on the versions of the old and new
servers.
Tuple time qualification rules (HeapTupleSatisfies routines) need to be
careful not to consider tuples with the "is multi" infomask bit set as
being only locked; they might need to look up MultiXact values (i.e.
possibly do pg_multixact I/O) to find out the Xid that updated a tuple,
whereas they previously were assured to only use information readily
available from the tuple header. This is considered acceptable, because
the extra I/O would involve cases that would previously cause some
commands to block waiting for concurrent transactions to finish.
Another important change is the fact that locking tuples that have
previously been updated causes the future versions to be marked as
locked, too; this is essential for correctness of foreign key checks.
This causes additional WAL-logging, also (there was previously a single
WAL record for a locked tuple; now there are as many as updated copies
of the tuple there exist.)
With all this in place, contention related to tuples being checked by
foreign key rules should be much reduced.
As a bonus, the old behavior that a subtransaction grabbing a stronger
tuple lock than the parent (sub)transaction held on a given tuple and
later aborting caused the weaker lock to be lost, has been fixed.
Many new spec files were added for isolation tester framework, to ensure
overall behavior is sane. There's probably room for several more tests.
There were several reviewers of this patch; in particular, Noah Misch
and Andres Freund spent considerable time in it. Original idea for the
patch came from Simon Riggs, after a problem report by Joel Jacobson.
Most code is from me, with contributions from Marti Raudsepp, Alexander
Shulgin, Noah Misch and Andres Freund.
This patch was discussed in several pgsql-hackers threads; the most
important start at the following message-ids:
AANLkTimo9XVcEzfiBR-ut3KVNDkjm2Vxh+t8kAmWjPuv@mail.gmail.com
1290721684-sup-3951@alvh.no-ip.org
1294953201-sup-2099@alvh.no-ip.org
1320343602-sup-2290@alvh.no-ip.org
1339690386-sup-8927@alvh.no-ip.org
4FE5FF020200002500048A3D@gw.wicourts.gov
4FEAB90A0200002500048B7D@gw.wicourts.gov
2013-01-23 16:04:59 +01:00
|
|
|
</row>
|
|
|
|
|
2007-11-11 00:30:46 +01:00
|
|
|
<row>
|
2007-12-10 06:32:51 +01:00
|
|
|
<entry><structfield>pids</structfield></entry>
|
|
|
|
<entry><type>integer[]</type></entry>
|
2012-06-07 23:06:20 +02:00
|
|
|
<entry>Process IDs of locking backends (more than one if multitransaction)</entry>
|
2007-11-11 00:30:46 +01:00
|
|
|
</row>
|
2007-12-10 06:32:51 +01:00
|
|
|
|
2007-11-11 00:30:46 +01:00
|
|
|
</tbody>
|
|
|
|
</tgroup>
|
|
|
|
</table>
|
|
|
|
|
|
|
|
<para>
|
2017-10-09 03:44:17 +02:00
|
|
|
<function>pgrowlocks</function> takes <literal>AccessShareLock</literal> for the
|
2007-12-10 06:32:51 +01:00
|
|
|
target table and reads each row one by one to collect the row locking
|
|
|
|
information. This is not very speedy for a large table. Note that:
|
2007-11-11 00:30:46 +01:00
|
|
|
</para>
|
2007-12-10 06:32:51 +01:00
|
|
|
|
2007-11-11 00:30:46 +01:00
|
|
|
<orderedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2007-12-10 06:32:51 +01:00
|
|
|
If the table as a whole is exclusive-locked by someone else,
|
|
|
|
<function>pgrowlocks</function> will be blocked.
|
2007-11-11 00:30:46 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2007-12-10 06:32:51 +01:00
|
|
|
<function>pgrowlocks</function> is not guaranteed to produce a
|
|
|
|
self-consistent snapshot. It is possible that a new row lock is taken,
|
|
|
|
or an old lock is freed, during its execution.
|
2007-11-11 00:30:46 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</orderedlist>
|
2007-12-10 06:32:51 +01:00
|
|
|
|
2007-11-11 00:30:46 +01:00
|
|
|
<para>
|
2007-12-10 06:32:51 +01:00
|
|
|
<function>pgrowlocks</function> does not show the contents of locked
|
|
|
|
rows. If you want to take a look at the row contents at the same time, you
|
|
|
|
could do something like this:
|
|
|
|
|
2010-07-29 21:34:41 +02:00
|
|
|
<programlisting>
|
2007-12-10 06:32:51 +01:00
|
|
|
SELECT * FROM accounts AS a, pgrowlocks('accounts') AS p
|
|
|
|
WHERE p.locked_row = a.ctid;
|
2010-07-29 21:34:41 +02:00
|
|
|
</programlisting>
|
2007-12-10 06:32:51 +01:00
|
|
|
|
2014-02-24 18:56:37 +01:00
|
|
|
Be aware however that such a query will be very inefficient.
|
2007-12-10 06:32:51 +01:00
|
|
|
</para>
|
2007-11-11 00:30:46 +01:00
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2>
|
2011-01-29 19:00:18 +01:00
|
|
|
<title>Sample Output</title>
|
2007-12-10 06:32:51 +01:00
|
|
|
|
2018-10-02 09:34:41 +02:00
|
|
|
<screen>
|
|
|
|
=# SELECT * FROM pgrowlocks('t1');
|
|
|
|
locked_row | locker | multi | xids | modes | pids
|
|
|
|
------------+--------+-------+-------+----------------+--------
|
|
|
|
(0,1) | 609 | f | {609} | {"For Share"} | {3161}
|
|
|
|
(0,2) | 609 | f | {609} | {"For Share"} | {3161}
|
|
|
|
(0,3) | 607 | f | {607} | {"For Update"} | {3107}
|
|
|
|
(0,4) | 607 | f | {607} | {"For Update"} | {3107}
|
2007-11-11 00:30:46 +01:00
|
|
|
(4 rows)
|
2010-07-29 21:34:41 +02:00
|
|
|
</screen>
|
2007-12-10 06:32:51 +01:00
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2>
|
|
|
|
<title>Author</title>
|
2007-11-11 00:30:46 +01:00
|
|
|
|
2007-12-10 06:32:51 +01:00
|
|
|
<para>
|
|
|
|
Tatsuo Ishii
|
|
|
|
</para>
|
2007-11-11 00:30:46 +01:00
|
|
|
</sect2>
|
|
|
|
|
2007-12-10 06:32:51 +01:00
|
|
|
</sect1>
|