Add new contrib function pgrowlocks. See README.pgrowlocks for more details.
This commit is contained in:
parent
044c3b4615
commit
66126f9687
25
contrib/pgrowlocks/Makefile
Normal file
25
contrib/pgrowlocks/Makefile
Normal file
@ -0,0 +1,25 @@
|
||||
#-------------------------------------------------------------------------
|
||||
#
|
||||
# pgrowlocks Makefile
|
||||
#
|
||||
# $PostgreSQL: pgsql/contrib/pgrowlocks/Makefile,v 1.1 2006/04/23 01:12:58 ishii Exp $
|
||||
#
|
||||
#-------------------------------------------------------------------------
|
||||
|
||||
SRCS = pgrowlocks.c
|
||||
|
||||
MODULE_big = pgrowlocks
|
||||
OBJS = $(SRCS:.c=.o)
|
||||
DOCS = README.pgrowlocks README.pgrowlocks.euc_jp
|
||||
DATA_built = pgrowlocks.sql
|
||||
|
||||
ifdef USE_PGXS
|
||||
PGXS = $(shell pg_config --pgxs)
|
||||
include $(PGXS)
|
||||
else
|
||||
subdir = contrib/pgrowlocks
|
||||
top_builddir = ../..
|
||||
include $(top_builddir)/src/Makefile.global
|
||||
include $(top_srcdir)/contrib/contrib-global.mk
|
||||
endif
|
||||
|
98
contrib/pgrowlocks/README.pgrowlocks
Normal file
98
contrib/pgrowlocks/README.pgrowlocks
Normal file
@ -0,0 +1,98 @@
|
||||
$PostgreSQL: pgsql/contrib/pgrowlocks/README.pgrowlocks,v 1.1 2006/04/23 01:12:58 ishii Exp $
|
||||
|
||||
pgrowlocks README Tatsuo Ishii
|
||||
|
||||
1. What is pgrowlocks?
|
||||
|
||||
pgrowlocks shows row locking information for specified table.
|
||||
|
||||
pgrowlocks returns following data type:
|
||||
|
||||
CREATE TYPE pgrowlocks_type AS (
|
||||
locked_row TID, -- row TID
|
||||
lock_type TEXT, -- lock type
|
||||
locker XID, -- locking XID
|
||||
multi bool, -- multi XID?
|
||||
xids xid[], -- multi XIDs
|
||||
pids INTEGER[] -- locker's process id
|
||||
);
|
||||
|
||||
Here is a sample execution of pgrowlocks:
|
||||
|
||||
test=# SELECT * FROM pgrowlocks('t1');
|
||||
locked_row | lock_type | locker | multi | xids | pids
|
||||
------------+-----------+--------+-------+-----------+---------------
|
||||
(0,1) | Shared | 19 | t | {804,805} | {29066,29068}
|
||||
(0,2) | Shared | 19 | t | {804,805} | {29066,29068}
|
||||
(0,3) | Exclusive | 804 | f | {804} | {29066}
|
||||
(0,4) | Exclusive | 804 | f | {804} | {29066}
|
||||
(4 rows)
|
||||
|
||||
locked_row -- tuple ID(TID) of each locked rows
|
||||
lock_type -- "Shared" for shared lock, "Exclusive" for exclusive lock
|
||||
locker -- transaction ID of locker (note 1)
|
||||
multi -- "t" if locker is a multi transaction, otherwise "f"
|
||||
xids -- XIDs of lockers (note 2)
|
||||
pids -- process ids of locking backends
|
||||
|
||||
note1: if the locker is multi transaction, it represents the multi ID
|
||||
|
||||
note2: if the locker is multi, multiple data are shown
|
||||
|
||||
2. Installing pgrowlocks
|
||||
|
||||
Installing pgrowlocks requires PostgreSQL 8.0 or later source tree.
|
||||
|
||||
$ cd /usr/local/src/postgresql-8.1/contrib
|
||||
$ tar xfz /tmp/pgrowlocks-1.0.tar.gz
|
||||
|
||||
If you are using PostgreSQL 8.0, you need to modify pgrowlocks source code.
|
||||
Around line 61, you will see:
|
||||
|
||||
#undef MAKERANGEVARFROMNAMELIST_HAS_TWO_ARGS
|
||||
|
||||
change this to:
|
||||
|
||||
#define MAKERANGEVARFROMNAMELIST_HAS_TWO_ARGS
|
||||
|
||||
$ make
|
||||
$ make install
|
||||
|
||||
$ psql -e -f pgrowlocks.sql test
|
||||
|
||||
3. How to use pgrowlocks
|
||||
|
||||
The calling sequence for pgrowlocks is as follows:
|
||||
|
||||
CREATE OR REPLACE FUNCTION pgrowlocks(text) RETURNS pgrowlocks_type
|
||||
AS 'MODULE_PATHNAME', 'pgrowlocks'
|
||||
LANGUAGE 'c' WITH (isstrict);
|
||||
|
||||
The parameter is a name of table. pgrowlocks returns type pgrowlocks_type.
|
||||
|
||||
pgrowlocks grab AccessShareLock for the target table and read each
|
||||
row one by one to get the row locking information. You should
|
||||
notice that:
|
||||
|
||||
1) if the table is exclusive locked by someone else, pgrowlocks
|
||||
will be blocked.
|
||||
|
||||
2) pgrowlocks may show incorrect information if there's a new
|
||||
lock or a lock is freeed while its execution.
|
||||
|
||||
pgrowlocks 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:
|
||||
|
||||
SELECT * FROM accounts AS a, pgrowlocks('accounts') AS p WHERE p.locked_ row = a.ctid;
|
||||
|
||||
|
||||
4. License
|
||||
|
||||
pgrowlocks is distribute under (modified) BSD license described in
|
||||
the source file.
|
||||
|
||||
5. History
|
||||
|
||||
2006/03/21 pgrowlocks version 1.1 released (tested on 8.2 current)
|
||||
2005/08/22 pgrowlocks version 1.0 released
|
121
contrib/pgrowlocks/README.pgrowlocks.euc_jp
Normal file
121
contrib/pgrowlocks/README.pgrowlocks.euc_jp
Normal file
@ -0,0 +1,121 @@
|
||||
$PostgreSQL: pgsql/contrib/pgrowlocks/README.pgrowlocks.euc_jp,v 1.1 2006/04/23 01:12:58 ishii Exp $
|
||||
|
||||
pgrowlocks README 石井達夫
|
||||
|
||||
1. pgrowlocksとは
|
||||
|
||||
pgrowlocksは,指定されたテーブルの行ロックに関する情報を表示します.
|
||||
|
||||
pgrowlocksの返す型は,以下のようになります.
|
||||
|
||||
CREATE TYPE pgrowlocks_type AS (
|
||||
locked_row TID, -- row TID
|
||||
lock_type TEXT, -- lock type
|
||||
locker XID, -- locking XID
|
||||
multi bool, -- multi XID?
|
||||
xids xid[], -- multi XIDs
|
||||
pids INTEGER[] -- locker's process id
|
||||
);
|
||||
|
||||
実行例を示します.
|
||||
|
||||
test=# SELECT * FROM pgrowlocks('t1');
|
||||
locked_row | lock_type | locker | multi | xids | pids
|
||||
------------+-----------+--------+-------+-----------+---------------
|
||||
(0,1) | Shared | 19 | t | {804,805} | {29066,29068}
|
||||
(0,2) | Shared | 19 | t | {804,805} | {29066,29068}
|
||||
(0,3) | Exclusive | 804 | f | {804} | {29066}
|
||||
(0,4) | Exclusive | 804 | f | {804} | {29066}
|
||||
(4 rows)
|
||||
|
||||
各項目の説明です.
|
||||
|
||||
locked_row -- ロックされた行のタプルID(TID)
|
||||
lock_type -- 共有ロックなら"Shared",排他ロックなら"Exclusive"
|
||||
locker -- ロックをかけているトランザクションID[注1]
|
||||
multi -- lockerがマルチトランザクションならtそうでなければf
|
||||
xids -- ロックをかけているトランザクションID[注2]
|
||||
pids -- ロックをかけているバックエンドプロセスのプロセスID[注2]
|
||||
|
||||
[注1: lockerがマルチなら,トランザクションIDではなくてマルチIDになり
|
||||
ます.]
|
||||
|
||||
[注2: lockerがマルチの場合,複数のデータが表示されます.]
|
||||
|
||||
2. pgrowlocksのインストール
|
||||
|
||||
pgrowlocksのインストールには,PostgreSQLをインストールしたときのソー
|
||||
スツリーが必要です.今のところ対応しているバージョンはPostgreSQL
|
||||
8.0以降です.
|
||||
|
||||
ここではPostgreSQLのソースは/usr/local/src/postgresql-8.1/に展開さ
|
||||
れているものとします.
|
||||
|
||||
1) pgrowlocksのソースを展開します.ソースファイルは
|
||||
/tmp/pgrowlocks-1.0.tar.gzに置いてあるものとします.
|
||||
|
||||
$ cd /usr/local/src/postgresql-8.1/contrib
|
||||
$ tar xfz pgrowlocks-1.0.tar.gz
|
||||
|
||||
2) PostgreSQL 8.0の場合は,ソースの一部に修正が必要です.
|
||||
pgrowlocks.cの61行目辺りに
|
||||
|
||||
#undef MAKERANGEVARFROMNAMELIST_HAS_TWO_ARGS
|
||||
|
||||
というのがあるので,これを
|
||||
|
||||
#define MAKERANGEVARFROMNAMELIST_HAS_TWO_ARGS
|
||||
|
||||
に書き換えます(undef->defineにします).
|
||||
|
||||
3) コンパイルして関数の共有ライブラリをインストールします.
|
||||
|
||||
$ make
|
||||
$ make install
|
||||
|
||||
4) ユーザ定義関数を登録します.
|
||||
|
||||
$ psql -e -f /usr/local/pgsql/share/contrib/pgrowlocks.sql test
|
||||
|
||||
この例では"test"というデータベースに登録していますが,もし他のデー
|
||||
タベースに登録する場合はここを読み替えてください.
|
||||
|
||||
3. pgrowlocksの使い方
|
||||
|
||||
pgrowlocksの呼び出し形式は以下です.
|
||||
|
||||
CREATE OR REPLACE FUNCTION pgrowlocks(text) RETURNS pgrowlocks_type
|
||||
AS 'MODULE_PATHNAME', 'pgrowlocks'
|
||||
LANGUAGE 'c' WITH (isstrict);
|
||||
|
||||
第一引数: テーブル名
|
||||
|
||||
関数の戻りはpgrowlocks_type型です.
|
||||
|
||||
pgrowlocksはテーブルにAccessShareLockロックをかけ,1行ずつ読み出し
|
||||
ては行ロックがかかっているかどうかチェックします.以下の点に注意し
|
||||
てください.
|
||||
|
||||
1) 該当テーブルに排他ロックがかかっていると,pgrowlocksの実行はブロッ
|
||||
クされます.
|
||||
|
||||
2) pgrowlocksの実行中に新たにかかったり,解除された行ロックに関する
|
||||
情報はpgrowlocksの実行結果に反映されていない可能性があります.
|
||||
|
||||
|
||||
pgrowlocksはロックされた行の内容は表示しません.行内容を見たい場合
|
||||
は,テーブルをpgrowlocksのlocked_rows列で結合します.例を示します.
|
||||
|
||||
SELECT * FROM accounts AS a, pgrowlocks('accounts') AS p WHERE p.locked_ row = a.ctid;
|
||||
|
||||
|
||||
4. pgrowlocksのライセンス条件について
|
||||
|
||||
pgrowlocks.cの冒頭に書いてある通りです(修正BSDライセンスに準じてい
|
||||
ます).また,pgrowlocks は完全に無保証です.pgrowlocks を使用したこ
|
||||
とによって生じるいかなる結果に関しても責任を負いません.
|
||||
|
||||
5. 改訂履歴
|
||||
|
||||
2006/03/21 pgrowlocks バージョン 1.1リリース(8.2 currentでテスト)
|
||||
2005/08/22 pgrowlocks バージョン 1.0リリース
|
228
contrib/pgrowlocks/pgrowlocks.c
Normal file
228
contrib/pgrowlocks/pgrowlocks.c
Normal file
@ -0,0 +1,228 @@
|
||||
/*
|
||||
* $PostgreSQL: pgsql/contrib/pgrowlocks/pgrowlocks.c,v 1.1 2006/04/23 01:12:58 ishii Exp $
|
||||
*
|
||||
* Copyright (c) 2005-2006 Tatsuo Ishii
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software and
|
||||
* its documentation for any purpose, without fee, and without a
|
||||
* written agreement is hereby granted, provided that the above
|
||||
* copyright notice and this paragraph and the following two
|
||||
* paragraphs appear in all copies.
|
||||
*
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
|
||||
* INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
|
||||
* LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
|
||||
* DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS
|
||||
* IS" BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,
|
||||
* SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
||||
*/
|
||||
|
||||
#include "postgres.h"
|
||||
|
||||
#include "funcapi.h"
|
||||
#include "access/heapam.h"
|
||||
#include "access/transam.h"
|
||||
#include "catalog/namespace.h"
|
||||
#include "catalog/pg_type.h"
|
||||
#include "storage/proc.h"
|
||||
#include "utils/builtins.h"
|
||||
|
||||
#ifdef HEAP_XMAX_SHARED_LOCK
|
||||
#include "access/multixact.h"
|
||||
#include "storage/procarray.h"
|
||||
#endif
|
||||
|
||||
PG_FUNCTION_INFO_V1(pgrowlocks);
|
||||
|
||||
extern Datum pgrowlocks(PG_FUNCTION_ARGS);
|
||||
|
||||
/* ----------
|
||||
* pgrowlocks:
|
||||
* returns tids of rows being locked
|
||||
*
|
||||
* C FUNCTION definition
|
||||
* pgrowlocks(text) returns set of pgrowlocks_type
|
||||
* see pgrowlocks.sql for pgrowlocks_type
|
||||
* ----------
|
||||
*/
|
||||
|
||||
#define DUMMY_TUPLE "public.pgrowlocks_type"
|
||||
#define NCHARS 32
|
||||
|
||||
/*
|
||||
* define this if makeRangeVarFromNameList() has two arguments. As far
|
||||
* as I know, this only happens in 8.0.x.
|
||||
*/
|
||||
#undef MAKERANGEVARFROMNAMELIST_HAS_TWO_ARGS
|
||||
|
||||
typedef struct {
|
||||
HeapScanDesc scan;
|
||||
int ncolumns;
|
||||
} MyData;
|
||||
|
||||
Datum
|
||||
pgrowlocks(PG_FUNCTION_ARGS)
|
||||
{
|
||||
FuncCallContext *funcctx;
|
||||
HeapScanDesc scan;
|
||||
HeapTuple tuple;
|
||||
TupleDesc tupdesc;
|
||||
AttInMetadata *attinmeta;
|
||||
Datum result;
|
||||
MyData *mydata;
|
||||
Relation rel;
|
||||
|
||||
if (SRF_IS_FIRSTCALL())
|
||||
{
|
||||
text *relname;
|
||||
RangeVar *relrv;
|
||||
MemoryContext oldcontext;
|
||||
|
||||
funcctx = SRF_FIRSTCALL_INIT();
|
||||
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
||||
|
||||
tupdesc = RelationNameGetTupleDesc(DUMMY_TUPLE);
|
||||
attinmeta = TupleDescGetAttInMetadata(tupdesc);
|
||||
funcctx->attinmeta = attinmeta;
|
||||
|
||||
relname = PG_GETARG_TEXT_P(0);
|
||||
#ifdef MAKERANGEVARFROMNAMELIST_HAS_TWO_ARGS
|
||||
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname, "pgrowlocks"));
|
||||
|
||||
#else
|
||||
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
|
||||
#endif
|
||||
rel = heap_openrv(relrv, AccessShareLock);
|
||||
scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
|
||||
mydata = palloc(sizeof(*mydata));
|
||||
mydata->scan = scan;
|
||||
mydata->ncolumns = tupdesc->natts;
|
||||
funcctx->user_fctx = mydata;
|
||||
|
||||
MemoryContextSwitchTo(oldcontext);
|
||||
}
|
||||
|
||||
funcctx = SRF_PERCALL_SETUP();
|
||||
attinmeta = funcctx->attinmeta;
|
||||
mydata = (MyData *)funcctx->user_fctx;
|
||||
scan = mydata->scan;
|
||||
|
||||
/* scan the relation */
|
||||
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
|
||||
{
|
||||
/* must hold a buffer lock to call HeapTupleSatisfiesUpdate */
|
||||
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE);
|
||||
|
||||
if (HeapTupleSatisfiesUpdate(tuple->t_data, GetCurrentCommandId(), scan->rs_cbuf)
|
||||
== HeapTupleBeingUpdated)
|
||||
{
|
||||
|
||||
char **values;
|
||||
int i;
|
||||
|
||||
values = (char **) palloc(mydata->ncolumns * sizeof(char *));
|
||||
|
||||
i = 0;
|
||||
values[i++] = (char *)DirectFunctionCall1(tidout, PointerGetDatum(&tuple->t_self));
|
||||
|
||||
#ifdef HEAP_XMAX_SHARED_LOCK
|
||||
if (tuple->t_data->t_infomask & HEAP_XMAX_SHARED_LOCK)
|
||||
values[i++] = pstrdup("Shared");
|
||||
else
|
||||
values[i++] = pstrdup("Exclusive");
|
||||
#else
|
||||
values[i++] = pstrdup("Exclusive");
|
||||
#endif
|
||||
values[i] = palloc(NCHARS*sizeof(char));
|
||||
snprintf(values[i++], NCHARS, "%d", HeapTupleHeaderGetXmax(tuple->t_data));
|
||||
#ifdef HEAP_XMAX_SHARED_LOCK
|
||||
if (tuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI)
|
||||
{
|
||||
TransactionId *xids;
|
||||
int nxids;
|
||||
int j;
|
||||
int isValidXid = 0; /* any valid xid ever exists? */
|
||||
|
||||
values[i++] = pstrdup("true");
|
||||
nxids = GetMultiXactIdMembers(HeapTupleHeaderGetXmax(tuple->t_data), &xids);
|
||||
if (nxids == -1)
|
||||
{
|
||||
elog(ERROR, "GetMultiXactIdMembers returns error");
|
||||
}
|
||||
|
||||
values[i] = palloc(NCHARS*nxids);
|
||||
values[i+1] = palloc(NCHARS*nxids);
|
||||
strcpy(values[i], "{");
|
||||
strcpy(values[i+1], "{");
|
||||
|
||||
for (j=0;j<nxids;j++)
|
||||
{
|
||||
char buf[NCHARS];
|
||||
|
||||
if (TransactionIdIsInProgress(xids[j]))
|
||||
{
|
||||
if (isValidXid)
|
||||
{
|
||||
strcat(values[i], ",");
|
||||
strcat(values[i+1], ",");
|
||||
}
|
||||
snprintf(buf, NCHARS, "%d", xids[j]);
|
||||
strcat(values[i], buf);
|
||||
snprintf(buf, NCHARS, "%d", BackendXidGetPid(xids[j]));
|
||||
strcat(values[i+1], buf);
|
||||
|
||||
isValidXid = 1;
|
||||
}
|
||||
}
|
||||
|
||||
strcat(values[i], "}");
|
||||
strcat(values[i+1], "}");
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
values[i++] = pstrdup("false");
|
||||
values[i] = palloc(NCHARS*sizeof(char));
|
||||
snprintf(values[i++], NCHARS, "{%d}", HeapTupleHeaderGetXmax(tuple->t_data));
|
||||
|
||||
values[i] = palloc(NCHARS*sizeof(char));
|
||||
snprintf(values[i++], NCHARS, "{%d}", BackendXidGetPid(HeapTupleHeaderGetXmax(tuple->t_data)));
|
||||
}
|
||||
|
||||
#else
|
||||
values[i++] = pstrdup("false");
|
||||
values[i++] = pstrdup("{}");
|
||||
values[i++] = pstrdup("{}");
|
||||
#endif
|
||||
|
||||
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK);
|
||||
|
||||
/* build a tuple */
|
||||
tuple = BuildTupleFromCStrings(attinmeta, values);
|
||||
|
||||
/* make the tuple into a datum */
|
||||
result = HeapTupleGetDatum(tuple);
|
||||
|
||||
/* Clean up */
|
||||
for (i = 0; i < mydata->ncolumns; i++)
|
||||
pfree(values[i]);
|
||||
pfree(values);
|
||||
|
||||
SRF_RETURN_NEXT(funcctx, result);
|
||||
}
|
||||
else
|
||||
{
|
||||
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK);
|
||||
}
|
||||
}
|
||||
|
||||
heap_endscan(scan);
|
||||
heap_close(scan->rs_rd, AccessShareLock);
|
||||
|
||||
SRF_RETURN_DONE(funcctx);
|
||||
}
|
16
contrib/pgrowlocks/pgrowlocks.sql.in
Normal file
16
contrib/pgrowlocks/pgrowlocks.sql.in
Normal file
@ -0,0 +1,16 @@
|
||||
-- Adjust this setting to control where the objects get created.
|
||||
SET search_path = public;
|
||||
|
||||
CREATE TYPE pgrowlocks_type AS (
|
||||
locked_row TID, -- row TID
|
||||
lock_type TEXT, -- lock type
|
||||
locker XID, -- locking XID
|
||||
multi bool, -- multi XID?
|
||||
xids xid[], -- multi XIDs
|
||||
pids INTEGER[] -- locker's process id
|
||||
);
|
||||
|
||||
CREATE OR REPLACE FUNCTION pgrowlocks(text)
|
||||
RETURNS setof pgrowlocks_type
|
||||
AS 'MODULE_PATHNAME', 'pgrowlocks'
|
||||
LANGUAGE 'C' STRICT;
|
Loading…
x
Reference in New Issue
Block a user