]> granicus.if.org Git - postgresql/commitdiff
Add new contrib function pgrowlocks. See README.pgrowlocks for more details.
authorTatsuo Ishii <ishii@postgresql.org>
Sun, 23 Apr 2006 01:12:58 +0000 (01:12 +0000)
committerTatsuo Ishii <ishii@postgresql.org>
Sun, 23 Apr 2006 01:12:58 +0000 (01:12 +0000)
contrib/pgrowlocks/Makefile [new file with mode: 0644]
contrib/pgrowlocks/README.pgrowlocks [new file with mode: 0644]
contrib/pgrowlocks/README.pgrowlocks.euc_jp [new file with mode: 0644]
contrib/pgrowlocks/pgrowlocks.c [new file with mode: 0644]
contrib/pgrowlocks/pgrowlocks.sql.in [new file with mode: 0644]

diff --git a/contrib/pgrowlocks/Makefile b/contrib/pgrowlocks/Makefile
new file mode 100644 (file)
index 0000000..2bff839
--- /dev/null
@@ -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
+
diff --git a/contrib/pgrowlocks/README.pgrowlocks b/contrib/pgrowlocks/README.pgrowlocks
new file mode 100644 (file)
index 0000000..ebaea4c
--- /dev/null
@@ -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
diff --git a/contrib/pgrowlocks/README.pgrowlocks.euc_jp b/contrib/pgrowlocks/README.pgrowlocks.euc_jp
new file mode 100644 (file)
index 0000000..de7a7c5
--- /dev/null
@@ -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¥ê¥ê¡¼¥¹
diff --git a/contrib/pgrowlocks/pgrowlocks.c b/contrib/pgrowlocks/pgrowlocks.c
new file mode 100644 (file)
index 0000000..51035e4
--- /dev/null
@@ -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);
+}
diff --git a/contrib/pgrowlocks/pgrowlocks.sql.in b/contrib/pgrowlocks/pgrowlocks.sql.in
new file mode 100644 (file)
index 0000000..0607c44
--- /dev/null
@@ -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;