2006-04-23 09:12:58 +08:00
|
|
|
/*
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pgrowlocks/pgrowlocks.c
|
2006-04-23 09:12:58 +08:00
|
|
|
*
|
|
|
|
* 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 "access/heapam.h"
|
2007-08-27 08:13:51 +08:00
|
|
|
#include "access/multixact.h"
|
2008-06-19 08:46:06 +08:00
|
|
|
#include "access/relscan.h"
|
2006-07-14 00:57:31 +08:00
|
|
|
#include "access/xact.h"
|
2006-04-23 09:12:58 +08:00
|
|
|
#include "catalog/namespace.h"
|
2007-08-27 08:13:51 +08:00
|
|
|
#include "funcapi.h"
|
|
|
|
#include "miscadmin.h"
|
2008-05-12 08:00:54 +08:00
|
|
|
#include "storage/bufmgr.h"
|
2007-08-27 08:13:51 +08:00
|
|
|
#include "storage/procarray.h"
|
2007-08-29 06:59:30 +08:00
|
|
|
#include "utils/acl.h"
|
2006-04-23 09:12:58 +08:00
|
|
|
#include "utils/builtins.h"
|
2011-02-24 01:18:09 +08:00
|
|
|
#include "utils/rel.h"
|
2008-03-27 05:10:39 +08:00
|
|
|
#include "utils/tqual.h"
|
2006-04-23 09:12:58 +08:00
|
|
|
|
|
|
|
|
2006-05-31 06:12:16 +08:00
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
2006-04-23 09:12:58 +08:00
|
|
|
PG_FUNCTION_INFO_V1(pgrowlocks);
|
|
|
|
|
|
|
|
extern Datum pgrowlocks(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
/* ----------
|
|
|
|
* pgrowlocks:
|
|
|
|
* returns tids of rows being locked
|
|
|
|
* ----------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define NCHARS 32
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
2006-04-25 08:40:54 +08:00
|
|
|
Relation rel;
|
2006-04-23 09:12:58 +08:00
|
|
|
HeapScanDesc scan;
|
2006-10-04 08:30:14 +08:00
|
|
|
int ncolumns;
|
2009-06-11 22:49:15 +08:00
|
|
|
} MyData;
|
2006-04-23 09:12:58 +08:00
|
|
|
|
|
|
|
Datum
|
|
|
|
pgrowlocks(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
FuncCallContext *funcctx;
|
|
|
|
HeapScanDesc scan;
|
|
|
|
HeapTuple tuple;
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
AttInMetadata *attinmeta;
|
|
|
|
Datum result;
|
2006-10-04 08:30:14 +08:00
|
|
|
MyData *mydata;
|
2006-04-23 09:12:58 +08:00
|
|
|
Relation rel;
|
|
|
|
|
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
|
|
|
text *relname;
|
|
|
|
RangeVar *relrv;
|
|
|
|
MemoryContext oldcontext;
|
2007-08-29 06:59:30 +08:00
|
|
|
AclResult aclresult;
|
2006-04-23 09:12:58 +08:00
|
|
|
|
|
|
|
funcctx = SRF_FIRSTCALL_INIT();
|
|
|
|
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
|
|
|
|
2007-08-27 08:13:51 +08:00
|
|
|
/* Build a tuple descriptor for our result type */
|
|
|
|
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
|
|
|
|
elog(ERROR, "return type must be a row type");
|
|
|
|
|
2006-04-23 09:12:58 +08:00
|
|
|
attinmeta = TupleDescGetAttInMetadata(tupdesc);
|
|
|
|
funcctx->attinmeta = attinmeta;
|
|
|
|
|
|
|
|
relname = PG_GETARG_TEXT_P(0);
|
|
|
|
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
|
|
|
|
rel = heap_openrv(relrv, AccessShareLock);
|
2007-08-27 08:13:51 +08:00
|
|
|
|
2007-08-29 06:59:30 +08:00
|
|
|
/* check permissions: must have SELECT on table */
|
|
|
|
aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
|
|
|
|
ACL_SELECT);
|
|
|
|
if (aclresult != ACLCHECK_OK)
|
|
|
|
aclcheck_error(aclresult, ACL_KIND_CLASS,
|
|
|
|
RelationGetRelationName(rel));
|
|
|
|
|
2006-04-23 09:12:58 +08:00
|
|
|
scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
|
|
|
|
mydata = palloc(sizeof(*mydata));
|
2006-04-25 08:40:54 +08:00
|
|
|
mydata->rel = rel;
|
2006-04-23 09:12:58 +08:00
|
|
|
mydata->scan = scan;
|
|
|
|
mydata->ncolumns = tupdesc->natts;
|
|
|
|
funcctx->user_fctx = mydata;
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
}
|
|
|
|
|
|
|
|
funcctx = SRF_PERCALL_SETUP();
|
|
|
|
attinmeta = funcctx->attinmeta;
|
2006-10-04 08:30:14 +08:00
|
|
|
mydata = (MyData *) funcctx->user_fctx;
|
2006-04-23 09:12:58 +08:00
|
|
|
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);
|
|
|
|
|
2007-12-01 05:22:54 +08:00
|
|
|
if (HeapTupleSatisfiesUpdate(tuple->t_data,
|
|
|
|
GetCurrentCommandId(false),
|
|
|
|
scan->rs_cbuf) == HeapTupleBeingUpdated)
|
2006-04-23 09:12:58 +08:00
|
|
|
{
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
char **values;
|
|
|
|
int i;
|
2006-04-23 09:12:58 +08:00
|
|
|
|
|
|
|
values = (char **) palloc(mydata->ncolumns * sizeof(char *));
|
|
|
|
|
|
|
|
i = 0;
|
2006-10-04 08:30:14 +08:00
|
|
|
values[i++] = (char *) DirectFunctionCall1(tidout, PointerGetDatum(&tuple->t_self));
|
2006-04-23 09:12:58 +08:00
|
|
|
|
|
|
|
if (tuple->t_data->t_infomask & HEAP_XMAX_SHARED_LOCK)
|
|
|
|
values[i++] = pstrdup("Shared");
|
|
|
|
else
|
|
|
|
values[i++] = pstrdup("Exclusive");
|
2006-10-04 08:30:14 +08:00
|
|
|
values[i] = palloc(NCHARS * sizeof(char));
|
2006-04-23 09:12:58 +08:00
|
|
|
snprintf(values[i++], NCHARS, "%d", HeapTupleHeaderGetXmax(tuple->t_data));
|
|
|
|
if (tuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI)
|
|
|
|
{
|
|
|
|
TransactionId *xids;
|
2006-10-04 08:30:14 +08:00
|
|
|
int nxids;
|
|
|
|
int j;
|
|
|
|
int isValidXid = 0; /* any valid xid ever exists? */
|
2006-04-23 09:12:58 +08:00
|
|
|
|
|
|
|
values[i++] = pstrdup("true");
|
|
|
|
nxids = GetMultiXactIdMembers(HeapTupleHeaderGetXmax(tuple->t_data), &xids);
|
|
|
|
if (nxids == -1)
|
|
|
|
{
|
|
|
|
elog(ERROR, "GetMultiXactIdMembers returns error");
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
values[i] = palloc(NCHARS * nxids);
|
|
|
|
values[i + 1] = palloc(NCHARS * nxids);
|
2006-04-23 09:12:58 +08:00
|
|
|
strcpy(values[i], "{");
|
2006-10-04 08:30:14 +08:00
|
|
|
strcpy(values[i + 1], "{");
|
2006-04-23 09:12:58 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (j = 0; j < nxids; j++)
|
2006-04-23 09:12:58 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
char buf[NCHARS];
|
2006-04-23 09:12:58 +08:00
|
|
|
|
|
|
|
if (TransactionIdIsInProgress(xids[j]))
|
|
|
|
{
|
|
|
|
if (isValidXid)
|
|
|
|
{
|
|
|
|
strcat(values[i], ",");
|
2006-10-04 08:30:14 +08:00
|
|
|
strcat(values[i + 1], ",");
|
2006-04-23 09:12:58 +08:00
|
|
|
}
|
|
|
|
snprintf(buf, NCHARS, "%d", xids[j]);
|
|
|
|
strcat(values[i], buf);
|
|
|
|
snprintf(buf, NCHARS, "%d", BackendXidGetPid(xids[j]));
|
2006-10-04 08:30:14 +08:00
|
|
|
strcat(values[i + 1], buf);
|
2006-04-23 09:12:58 +08:00
|
|
|
|
|
|
|
isValidXid = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
strcat(values[i], "}");
|
2006-10-04 08:30:14 +08:00
|
|
|
strcat(values[i + 1], "}");
|
2006-04-23 09:12:58 +08:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
values[i++] = pstrdup("false");
|
2006-10-04 08:30:14 +08:00
|
|
|
values[i] = palloc(NCHARS * sizeof(char));
|
2006-04-23 09:12:58 +08:00
|
|
|
snprintf(values[i++], NCHARS, "{%d}", HeapTupleHeaderGetXmax(tuple->t_data));
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
values[i] = palloc(NCHARS * sizeof(char));
|
2006-04-23 09:12:58 +08:00
|
|
|
snprintf(values[i++], NCHARS, "{%d}", BackendXidGetPid(HeapTupleHeaderGetXmax(tuple->t_data)));
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2006-04-25 08:40:54 +08:00
|
|
|
heap_close(mydata->rel, AccessShareLock);
|
2006-04-23 09:12:58 +08:00
|
|
|
|
|
|
|
SRF_RETURN_DONE(funcctx);
|
|
|
|
}
|