diff options
author | Tatsuo Ishii | 2006-04-23 01:12:58 +0000 |
---|---|---|
committer | Tatsuo Ishii | 2006-04-23 01:12:58 +0000 |
commit | dca25654118f85ea533fe77a9a277dce199b4258 (patch) | |
tree | a097a6e8f0af3f5ecd77f0a7dbc74c850d040048 | |
parent | a89701f4d17f874ec2044df6d6909088ab101707 (diff) |
Add new contrib function pgrowlocks. See README.pgrowlocks for more details.
-rw-r--r-- | contrib/pgrowlocks/Makefile | 25 | ||||
-rw-r--r-- | contrib/pgrowlocks/README.pgrowlocks | 98 | ||||
-rw-r--r-- | contrib/pgrowlocks/README.pgrowlocks.euc_jp | 121 | ||||
-rw-r--r-- | contrib/pgrowlocks/pgrowlocks.c | 228 | ||||
-rw-r--r-- | contrib/pgrowlocks/pgrowlocks.sql.in | 16 |
5 files changed, 488 insertions, 0 deletions
diff --git a/contrib/pgrowlocks/Makefile b/contrib/pgrowlocks/Makefile new file mode 100644 index 0000000000..1b7128d8ee --- /dev/null +++ b/contrib/pgrowlocks/Makefile @@ -0,0 +1,25 @@ +#------------------------------------------------------------------------- +# +# pgrowlocks Makefile +# +# $PostgreSQL$ +# +#------------------------------------------------------------------------- + +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 + diff --git a/contrib/pgrowlocks/README.pgrowlocks b/contrib/pgrowlocks/README.pgrowlocks new file mode 100644 index 0000000000..ed238bc575 --- /dev/null +++ b/contrib/pgrowlocks/README.pgrowlocks @@ -0,0 +1,98 @@ +$PostgreSQL$ + +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 diff --git a/contrib/pgrowlocks/README.pgrowlocks.euc_jp b/contrib/pgrowlocks/README.pgrowlocks.euc_jp new file mode 100644 index 0000000000..2d11d81742 --- /dev/null +++ b/contrib/pgrowlocks/README.pgrowlocks.euc_jp @@ -0,0 +1,121 @@ +$PostgreSQL$ + +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���� diff --git a/contrib/pgrowlocks/pgrowlocks.c b/contrib/pgrowlocks/pgrowlocks.c new file mode 100644 index 0000000000..d135a87b5d --- /dev/null +++ b/contrib/pgrowlocks/pgrowlocks.c @@ -0,0 +1,228 @@ +/* + * $PostgreSQL$ + * + * 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); +} diff --git a/contrib/pgrowlocks/pgrowlocks.sql.in b/contrib/pgrowlocks/pgrowlocks.sql.in new file mode 100644 index 0000000000..0607c44349 --- /dev/null +++ b/contrib/pgrowlocks/pgrowlocks.sql.in @@ -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; |