Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1bab464

Browse files
committed
Code review for pg_locks feature. Make shmemoffset of PROCLOCK structs
available (else there's no way to interpret the list links). Changepg_locks view to show transaction ID locks separately from ordinaryrelation locks. Avoid showing N duplicate rows when the same lock isheld multiple times (seems unlikely that users care about exact holdcount). Improve documentation.
1 parent642fd38 commit1bab464

File tree

7 files changed

+212
-163
lines changed

7 files changed

+212
-163
lines changed

‎doc/src/sgml/monitoring.sgml

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/monitoring.sgml,v 1.12 2002/08/20 04:47:52 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/monitoring.sgml,v 1.13 2002/08/31 17:14:27 tgl Exp $
33
-->
44

55
<chapter id="monitoring">
@@ -593,22 +593,25 @@ FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS S;
593593

594594
<note>
595595
<para>
596-
When the <literal>pg_locks</literal> view is accessed,an
597-
exclusive lock on aninternal lock manager datastructure must be
598-
acquired to ensure thatthedata produced by the view is
599-
consistent. The lock held on this structure conflicts with normal
600-
database operations, and can therefore have an effect on overall
601-
database performance. Nevertheless, theperformanceimpact of
602-
accessing this view should be minimal in most situations.
596+
When the <literal>pg_locks</literal> view is accessed,the
597+
internal lock manager datastructures are momentarily locked,
598+
and a copy is made fortheview to display. This ensures that
599+
the view produces a consistent set of results, while not blocking
600+
normal lock manager operations longer than necessary. Nonetheless
601+
there could be some impact on databaseperformanceif this view is
602+
examined often.
603603
</para>
604604
</note>
605605

606606
<para>
607-
The <literal>pg_locks</literal> view contains one row per
608-
lock. This means that if there are multiple locks on a single
609-
relation (which may or may not conflict with one another), a
610-
single relation may show up many times. Furthermore, only
611-
table-level locks are displayed (not row-level ones).
607+
The <literal>pg_locks</literal> view contains one row per lockable
608+
object and requested lock mode. Thus, the same lockable object
609+
may appear many times, if multiple transactions are holding or
610+
waiting for locks on it. A lockable object is either a relation
611+
or a transaction ID. (Note that this view includes only table-level
612+
locks, not row-level ones. If a transaction is waiting for a
613+
row-level lock, it will appear in the view as waiting for the
614+
transaction ID of the current holder of that row lock.)
612615
</para>
613616

614617
<table>
@@ -627,53 +630,74 @@ FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS S;
627630
<row>
628631
<entry><structfield>relation</structfield></entry>
629632
<entry><type>oid</type></entry>
630-
<entry>The OID of the locked relation. When querying
631-
<literal>pg_locks</literal>, this column can be joined with the
632-
<literal>pg_class</literal> system catalog to get more
633-
information on the locked relation.</entry>
633+
<entry>The OID of the locked relation, or NULL if the lockable
634+
object is a transaction ID. This column can be joined
635+
with the <literal>pg_class</literal> system catalog to get more
636+
information on the locked relation. Note however that this will
637+
only work for relations in the current database (those for which
638+
the <structfield>database</structfield> column is either the
639+
current database's OID or zero).
640+
</entry>
634641
</row>
635642

636643
<row>
637644
<entry><structfield>database</structfield></entry>
638645
<entry><type>oid</type></entry>
639646
<entry>The OID of the database in which the locked relation
640-
exists. If the lock is on a globally-shared object, this value
641-
will be 0. When querying <literal>pg_locks</literal>, this
642-
column can be joined with the <literal>pg_database</literal>
643-
system catalog to get more information on the locked object's
644-
database.</entry>
647+
exists, or NULL if the lockable object is a transaction ID.
648+
If the lock is on a globally-shared table, this field will be
649+
zero. This
650+
column can be joined with the <literal>pg_database</literal>
651+
system catalog to get more information on the locked object's
652+
database.
653+
</entry>
645654
</row>
646655

647656
<row>
648-
<entry><structfield>backendpid</structfield></entry>
657+
<entry><structfield>transaction</structfield></entry>
658+
<entry><type>xid</type></entry>
659+
<entry>The ID of a transaction, or NULL if the lockable object
660+
is a relation. Every transaction holds ExclusiveLock on its
661+
transaction ID for its entire duration. If one transaction finds
662+
it necessary to wait specifically for another transaction, it
663+
does so by attempting to acquire ShareLock on the other transaction
664+
ID. That will succeed only when the other transaction terminates
665+
and releases its locks.
666+
</entry>
667+
</row>
668+
669+
<row>
670+
<entry><structfield>pid</structfield></entry>
649671
<entry><type>int4</type></entry>
650672
<entry>The process ID of the
651673
<productname>PostgreSQL</productname> backend that has
652674
acquired or is attempting to acquire the lock. If you have
653675
enabled the statistics collector, this column can be joined
654-
with the <literal>pg_stat_activity</literal> view toaccess
676+
with the <literal>pg_stat_activity</literal> view toget
655677
more information on the backend holding or waiting to hold the
656678
lock.</entry>
657679
</row>
658680

659681
<row>
660682
<entry><structfield>mode</structfield></entry>
661683
<entry><type>text</type></entry>
662-
<entry>The mode of the lock. For more information on the
684+
<entry>The mode of the requested or held lock on the lockable
685+
object. For more information on the
663686
different lock modes available in
664687
<productname>PostgreSQL</productname>, refer to the
665688
<citetitle>User's Guide</citetitle>.</entry>
666689
</row>
667690

668691
<row>
669692
<entry><structfield>isgranted</structfield></entry>
670-
<entry><type>text</type></entry>
671-
<entry>A boolean column indicating whether or not this
672-
particular lock has been granted. If the lock has not been
673-
granted, the backend atempting to acquire it will sleep until
674-
the lock is released (or a deadlock situation is detected). A
675-
single backend can be waiting to acquire at most one lock at
676-
any given time.</entry>
693+
<entry><type>bool</type></entry>
694+
<entry>True if this lock has been granted (is held by this
695+
backend). False indicates that this backend is currently
696+
waiting to acquire this lock, which implies that some other
697+
backend is holding a conflicting lock mode on the same lockable
698+
object. This backend will sleep until the other lock is released
699+
(or a deadlock situation is detected). A single backend can be
700+
waiting to acquire at most one lock at a time.</entry>
677701
</row>
678702
</tbody>
679703
</tgroup>

‎src/backend/storage/lmgr/lock.c

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lock.c,v 1.112 2002/08/17 13:04:14 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lock.c,v 1.113 2002/08/31 17:14:27 tgl Exp $
1212
*
1313
* NOTES
1414
* Outside modules can create a lock table and acquire/release
@@ -1361,59 +1361,68 @@ LockShmemSize(int maxBackends)
13611361

13621362
/*
13631363
* GetLockStatusData - Return a summary of the lock manager's internal
1364-
* status, for use in a user-levelstatisticalreporting function.
1364+
* status, for use in a user-level reporting function.
13651365
*
1366-
* This function should be passed a pointer to a LockData struct. It fills
1367-
* the structure with the appropriate information and returns. The goal
1368-
* is to hold the LockMgrLock for as short a time as possible; thus, the
1369-
* function simply makes a copy of the necessary data and releases the
1370-
* lock, allowing the caller to contemplate and format the data for
1371-
* as long as it pleases.
1366+
* The return data consists of an array of PROCLOCK objects, with the
1367+
* associated PGPROC and LOCK objects for each. Note that multiple
1368+
* copies of the same PGPROC and/or LOCK objects are likely to appear.
1369+
* It is the caller's responsibility to match up duplicates if wanted.
1370+
*
1371+
* The design goal is to hold the LockMgrLock for as short a time as possible;
1372+
* thus, this function simply makes a copy of the necessary data and releases
1373+
* the lock, allowing the caller to contemplate and format the data for as
1374+
* long as it pleases.
13721375
*/
1373-
void
1374-
GetLockStatusData(LockData*data)
1376+
LockData*
1377+
GetLockStatusData(void)
13751378
{
1379+
LockData*data;
13761380
HTAB*holderTable;
13771381
PROCLOCK*holder;
13781382
HASH_SEQ_STATUSseqstat;
1379-
inti=0;
1383+
inti;
13801384

1381-
data->currIdx=0;
1385+
data=(LockData*)palloc(sizeof(LockData));
13821386

13831387
LWLockAcquire(LockMgrLock,LW_EXCLUSIVE);
13841388

13851389
holderTable=LockMethodTable[DEFAULT_LOCKMETHOD]->holderHash;
13861390

1387-
data->nelements=holderTable->hctl->nentries;
1391+
data->nelements=i=holderTable->hctl->nentries;
1392+
1393+
if (i==0)
1394+
i=1;/* avoid palloc(0) if empty table */
13881395

1389-
data->procs= (PGPROC*)palloc(sizeof(PGPROC)*data->nelements);
1390-
data->locks= (LOCK*)palloc(sizeof(LOCK)*data->nelements);
1391-
data->holders= (PROCLOCK*)palloc(sizeof(PROCLOCK)*data->nelements);
1396+
data->holderaddrs= (SHMEM_OFFSET*)palloc(sizeof(SHMEM_OFFSET)*i);
1397+
data->holders= (PROCLOCK*)palloc(sizeof(PROCLOCK)*i);
1398+
data->procs= (PGPROC*)palloc(sizeof(PGPROC)*i);
1399+
data->locks= (LOCK*)palloc(sizeof(LOCK)*i);
13921400

13931401
hash_seq_init(&seqstat,holderTable);
13941402

1403+
i=0;
13951404
while ( (holder=hash_seq_search(&seqstat)) )
13961405
{
1397-
PGPROC*proc;
1398-
LOCK*lock;
1399-
1400-
/* Only do a shallow copy */
1401-
proc= (PGPROC*)MAKE_PTR(holder->tag.proc);
1402-
lock= (LOCK*)MAKE_PTR(holder->tag.lock);
1406+
PGPROC*proc= (PGPROC*)MAKE_PTR(holder->tag.proc);
1407+
LOCK*lock= (LOCK*)MAKE_PTR(holder->tag.lock);
14031408

1409+
data->holderaddrs[i]=MAKE_OFFSET(holder);
1410+
memcpy(&(data->holders[i]),holder,sizeof(PROCLOCK));
14041411
memcpy(&(data->procs[i]),proc,sizeof(PGPROC));
14051412
memcpy(&(data->locks[i]),lock,sizeof(LOCK));
1406-
memcpy(&(data->holders[i]),holder,sizeof(PROCLOCK));
14071413

14081414
i++;
14091415
}
14101416

1417+
LWLockRelease(LockMgrLock);
1418+
14111419
Assert(i==data->nelements);
14121420

1413-
LWLockRelease(LockMgrLock);
1421+
returndata;
14141422
}
14151423

1416-
char*
1424+
/* Provide the textual name of any lock mode */
1425+
constchar*
14171426
GetLockmodeName(LOCKMODEmode)
14181427
{
14191428
Assert(mode <=MAX_LOCKMODES);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp