2002-08-31 19:14:28 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
2002-08-17 15:11:43 +02:00
|
|
|
* lockfuncs.c
|
|
|
|
* Set-returning functions to view the state of locks within the DB.
|
2002-09-04 22:31:48 +02:00
|
|
|
*
|
2006-03-05 16:59:11 +01:00
|
|
|
* Copyright (c) 2002-2006, PostgreSQL Global Development Group
|
2002-08-17 15:11:43 +02:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2006-07-24 01:08:46 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/utils/adt/lockfuncs.c,v 1.24 2006/07/23 23:08:46 tgl Exp $
|
2002-08-31 19:14:28 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
2002-08-17 15:11:43 +02:00
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
2002-08-29 02:17:06 +02:00
|
|
|
|
2002-08-17 15:11:43 +02:00
|
|
|
#include "funcapi.h"
|
2002-08-31 19:14:28 +02:00
|
|
|
#include "access/heapam.h"
|
2002-08-27 06:00:28 +02:00
|
|
|
#include "catalog/pg_type.h"
|
2002-08-17 15:11:43 +02:00
|
|
|
#include "storage/proc.h"
|
2002-08-29 02:17:06 +02:00
|
|
|
#include "utils/builtins.h"
|
2002-08-17 15:11:43 +02:00
|
|
|
|
|
|
|
|
2005-05-17 23:46:11 +02:00
|
|
|
/* This must match enum LockTagType! */
|
2005-10-15 04:49:52 +02:00
|
|
|
static const char *const LockTagTypeNames[] = {
|
2005-05-17 23:46:11 +02:00
|
|
|
"relation",
|
|
|
|
"extend",
|
|
|
|
"page",
|
|
|
|
"tuple",
|
2005-06-18 21:33:42 +02:00
|
|
|
"transactionid",
|
2005-05-17 23:46:11 +02:00
|
|
|
"object",
|
|
|
|
"userlock"
|
|
|
|
};
|
|
|
|
|
2002-08-31 19:14:28 +02:00
|
|
|
/* Working status for pg_lock_status */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
LockData *lockData; /* state data from lmgr */
|
|
|
|
int currIdx; /* current PROCLOCK index */
|
|
|
|
} PG_Lock_Status;
|
2002-08-29 02:17:06 +02:00
|
|
|
|
2002-08-31 19:14:28 +02:00
|
|
|
/*
|
|
|
|
* pg_lock_status - produce a view with one row per held or awaited lock mode
|
|
|
|
*/
|
2002-08-17 15:11:43 +02:00
|
|
|
Datum
|
2002-08-27 06:00:28 +02:00
|
|
|
pg_lock_status(PG_FUNCTION_ARGS)
|
2002-08-17 15:11:43 +02:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
FuncCallContext *funcctx;
|
|
|
|
PG_Lock_Status *mystatus;
|
2002-08-31 19:14:28 +02:00
|
|
|
LockData *lockData;
|
2002-08-17 15:11:43 +02:00
|
|
|
|
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
TupleDesc tupdesc;
|
|
|
|
MemoryContext oldcontext;
|
2002-08-17 15:11:43 +02:00
|
|
|
|
2002-08-29 19:14:33 +02:00
|
|
|
/* create a function context for cross-call persistence */
|
|
|
|
funcctx = SRF_FIRSTCALL_INIT();
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* switch to memory context appropriate for multiple function calls
|
2002-09-04 22:31:48 +02:00
|
|
|
*/
|
2002-08-29 19:14:33 +02:00
|
|
|
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
|
|
|
|
2002-08-31 19:14:28 +02:00
|
|
|
/* build tupdesc for result tuples */
|
2004-04-01 23:28:47 +02:00
|
|
|
/* this had better match pg_locks view in system_views.sql */
|
2005-06-18 21:33:42 +02:00
|
|
|
tupdesc = CreateTemplateTupleDesc(13, false);
|
2005-05-17 23:46:11 +02:00
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "locktype",
|
|
|
|
TEXTOID, -1, 0);
|
2003-02-20 00:41:15 +01:00
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "database",
|
2004-04-01 23:28:47 +02:00
|
|
|
OIDOID, -1, 0);
|
2005-05-17 23:46:11 +02:00
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "relation",
|
|
|
|
OIDOID, -1, 0);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 4, "page",
|
|
|
|
INT4OID, -1, 0);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 5, "tuple",
|
|
|
|
INT2OID, -1, 0);
|
2005-06-18 21:33:42 +02:00
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 6, "transactionid",
|
2004-04-01 23:28:47 +02:00
|
|
|
XIDOID, -1, 0);
|
2005-05-17 23:46:11 +02:00
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 7, "classid",
|
|
|
|
OIDOID, -1, 0);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 8, "objid",
|
|
|
|
OIDOID, -1, 0);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 9, "objsubid",
|
|
|
|
INT2OID, -1, 0);
|
2005-06-18 21:33:42 +02:00
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 10, "transaction",
|
|
|
|
XIDOID, -1, 0);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 11, "pid",
|
2004-04-01 23:28:47 +02:00
|
|
|
INT4OID, -1, 0);
|
2005-06-18 21:33:42 +02:00
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 12, "mode",
|
2004-04-01 23:28:47 +02:00
|
|
|
TEXTOID, -1, 0);
|
2005-06-18 21:33:42 +02:00
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 13, "granted",
|
2004-04-01 23:28:47 +02:00
|
|
|
BOOLOID, -1, 0);
|
2002-08-27 06:00:28 +02:00
|
|
|
|
2004-04-01 23:28:47 +02:00
|
|
|
funcctx->tuple_desc = BlessTupleDesc(tupdesc);
|
2002-08-17 15:11:43 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Collect all the locking information that we will format and send
|
|
|
|
* out as a result set.
|
2002-08-17 15:11:43 +02:00
|
|
|
*/
|
2002-08-31 19:14:28 +02:00
|
|
|
mystatus = (PG_Lock_Status *) palloc(sizeof(PG_Lock_Status));
|
|
|
|
funcctx->user_fctx = (void *) mystatus;
|
2002-08-17 15:11:43 +02:00
|
|
|
|
2002-08-31 19:14:28 +02:00
|
|
|
mystatus->lockData = GetLockStatusData();
|
|
|
|
mystatus->currIdx = 0;
|
2002-08-17 15:11:43 +02:00
|
|
|
|
2002-08-29 19:14:33 +02:00
|
|
|
MemoryContextSwitchTo(oldcontext);
|
2002-08-17 15:11:43 +02:00
|
|
|
}
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
funcctx = SRF_PERCALL_SETUP();
|
2002-08-31 19:14:28 +02:00
|
|
|
mystatus = (PG_Lock_Status *) funcctx->user_fctx;
|
|
|
|
lockData = mystatus->lockData;
|
2002-08-17 15:11:43 +02:00
|
|
|
|
2002-08-31 19:14:28 +02:00
|
|
|
while (mystatus->currIdx < lockData->nelements)
|
2002-08-17 15:11:43 +02:00
|
|
|
{
|
2003-02-18 03:13:24 +01:00
|
|
|
PROCLOCK *proclock;
|
2002-09-04 22:31:48 +02:00
|
|
|
LOCK *lock;
|
|
|
|
PGPROC *proc;
|
|
|
|
bool granted;
|
2004-08-27 19:07:42 +02:00
|
|
|
LOCKMODE mode = 0;
|
2005-05-17 23:46:11 +02:00
|
|
|
const char *locktypename;
|
|
|
|
char tnbuf[32];
|
2005-06-18 21:33:42 +02:00
|
|
|
Datum values[13];
|
|
|
|
char nulls[13];
|
2002-09-04 22:31:48 +02:00
|
|
|
HeapTuple tuple;
|
|
|
|
Datum result;
|
|
|
|
|
2003-02-18 03:13:24 +01:00
|
|
|
proclock = &(lockData->proclocks[mystatus->currIdx]);
|
2002-09-04 22:31:48 +02:00
|
|
|
lock = &(lockData->locks[mystatus->currIdx]);
|
|
|
|
proc = &(lockData->procs[mystatus->currIdx]);
|
2002-08-17 15:11:43 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Look to see if there are any held lock modes in this PROCLOCK. If
|
|
|
|
* so, report, and destructively modify lockData so we don't report
|
|
|
|
* again.
|
2002-08-17 15:11:43 +02:00
|
|
|
*/
|
2002-08-31 19:14:28 +02:00
|
|
|
granted = false;
|
2004-08-27 19:07:42 +02:00
|
|
|
if (proclock->holdMask)
|
2002-08-17 15:11:43 +02:00
|
|
|
{
|
2004-08-27 19:07:42 +02:00
|
|
|
for (mode = 0; mode < MAX_LOCKMODES; mode++)
|
2002-08-31 19:14:28 +02:00
|
|
|
{
|
2004-08-27 19:07:42 +02:00
|
|
|
if (proclock->holdMask & LOCKBIT_ON(mode))
|
|
|
|
{
|
|
|
|
granted = true;
|
|
|
|
proclock->holdMask &= LOCKBIT_OFF(mode);
|
|
|
|
break;
|
|
|
|
}
|
2002-08-31 19:14:28 +02:00
|
|
|
}
|
2002-08-17 15:11:43 +02:00
|
|
|
}
|
2002-08-31 19:14:28 +02:00
|
|
|
|
|
|
|
/*
|
2002-09-04 22:31:48 +02:00
|
|
|
* If no (more) held modes to report, see if PROC is waiting for a
|
|
|
|
* lock on this lock.
|
2002-08-31 19:14:28 +02:00
|
|
|
*/
|
|
|
|
if (!granted)
|
2002-08-17 15:11:43 +02:00
|
|
|
{
|
2006-07-24 01:08:46 +02:00
|
|
|
if (proc->waitLock == proclock->tag.myLock)
|
2002-08-31 19:14:28 +02:00
|
|
|
{
|
|
|
|
/* Yes, so report it with proper mode */
|
|
|
|
mode = proc->waitLockMode;
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-08-31 19:14:28 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We are now done with this PROCLOCK, so advance pointer to
|
|
|
|
* continue with next one on next call.
|
2002-08-31 19:14:28 +02:00
|
|
|
*/
|
|
|
|
mystatus->currIdx++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Okay, we've displayed all the locks associated with this
|
|
|
|
* PROCLOCK, proceed to the next one.
|
2002-08-31 19:14:28 +02:00
|
|
|
*/
|
|
|
|
mystatus->currIdx++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2002-08-17 15:11:43 +02:00
|
|
|
|
2002-08-31 19:14:28 +02:00
|
|
|
/*
|
|
|
|
* Form tuple with appropriate data.
|
|
|
|
*/
|
|
|
|
MemSet(values, 0, sizeof(values));
|
|
|
|
MemSet(nulls, ' ', sizeof(nulls));
|
|
|
|
|
2005-05-17 23:46:11 +02:00
|
|
|
if (lock->tag.locktag_type <= LOCKTAG_USERLOCK)
|
|
|
|
locktypename = LockTagTypeNames[lock->tag.locktag_type];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snprintf(tnbuf, sizeof(tnbuf), "unknown %d",
|
|
|
|
(int) lock->tag.locktag_type);
|
|
|
|
locktypename = tnbuf;
|
|
|
|
}
|
|
|
|
values[0] = DirectFunctionCall1(textin,
|
2005-10-15 04:49:52 +02:00
|
|
|
CStringGetDatum(locktypename));
|
2005-05-17 23:46:11 +02:00
|
|
|
|
|
|
|
|
2005-04-30 00:28:24 +02:00
|
|
|
switch (lock->tag.locktag_type)
|
2002-08-31 19:14:28 +02:00
|
|
|
{
|
2005-04-30 00:28:24 +02:00
|
|
|
case LOCKTAG_RELATION:
|
|
|
|
case LOCKTAG_RELATION_EXTEND:
|
2005-05-17 23:46:11 +02:00
|
|
|
values[1] = ObjectIdGetDatum(lock->tag.locktag_field1);
|
|
|
|
values[2] = ObjectIdGetDatum(lock->tag.locktag_field2);
|
|
|
|
nulls[3] = 'n';
|
|
|
|
nulls[4] = 'n';
|
|
|
|
nulls[5] = 'n';
|
|
|
|
nulls[6] = 'n';
|
|
|
|
nulls[7] = 'n';
|
|
|
|
nulls[8] = 'n';
|
|
|
|
break;
|
2005-04-30 00:28:24 +02:00
|
|
|
case LOCKTAG_PAGE:
|
2005-05-17 23:46:11 +02:00
|
|
|
values[1] = ObjectIdGetDatum(lock->tag.locktag_field1);
|
|
|
|
values[2] = ObjectIdGetDatum(lock->tag.locktag_field2);
|
|
|
|
values[3] = UInt32GetDatum(lock->tag.locktag_field3);
|
|
|
|
nulls[4] = 'n';
|
|
|
|
nulls[5] = 'n';
|
|
|
|
nulls[6] = 'n';
|
|
|
|
nulls[7] = 'n';
|
|
|
|
nulls[8] = 'n';
|
|
|
|
break;
|
2005-04-30 00:28:24 +02:00
|
|
|
case LOCKTAG_TUPLE:
|
|
|
|
values[1] = ObjectIdGetDatum(lock->tag.locktag_field1);
|
2005-05-17 23:46:11 +02:00
|
|
|
values[2] = ObjectIdGetDatum(lock->tag.locktag_field2);
|
|
|
|
values[3] = UInt32GetDatum(lock->tag.locktag_field3);
|
|
|
|
values[4] = UInt16GetDatum(lock->tag.locktag_field4);
|
|
|
|
nulls[5] = 'n';
|
|
|
|
nulls[6] = 'n';
|
|
|
|
nulls[7] = 'n';
|
|
|
|
nulls[8] = 'n';
|
2005-04-30 00:28:24 +02:00
|
|
|
break;
|
|
|
|
case LOCKTAG_TRANSACTION:
|
2005-05-17 23:46:11 +02:00
|
|
|
values[5] = TransactionIdGetDatum(lock->tag.locktag_field1);
|
2005-04-30 00:28:24 +02:00
|
|
|
nulls[1] = 'n';
|
2005-05-17 23:46:11 +02:00
|
|
|
nulls[2] = 'n';
|
|
|
|
nulls[3] = 'n';
|
|
|
|
nulls[4] = 'n';
|
|
|
|
nulls[6] = 'n';
|
|
|
|
nulls[7] = 'n';
|
|
|
|
nulls[8] = 'n';
|
|
|
|
break;
|
|
|
|
case LOCKTAG_OBJECT:
|
|
|
|
case LOCKTAG_USERLOCK:
|
|
|
|
default: /* treat unknown locktags like OBJECT */
|
|
|
|
values[1] = ObjectIdGetDatum(lock->tag.locktag_field1);
|
|
|
|
values[6] = ObjectIdGetDatum(lock->tag.locktag_field2);
|
|
|
|
values[7] = ObjectIdGetDatum(lock->tag.locktag_field3);
|
|
|
|
values[8] = Int16GetDatum(lock->tag.locktag_field4);
|
|
|
|
nulls[2] = 'n';
|
|
|
|
nulls[3] = 'n';
|
|
|
|
nulls[4] = 'n';
|
|
|
|
nulls[5] = 'n';
|
2005-04-30 00:28:24 +02:00
|
|
|
break;
|
2002-08-17 15:11:43 +02:00
|
|
|
}
|
|
|
|
|
2005-06-18 21:33:42 +02:00
|
|
|
values[9] = TransactionIdGetDatum(proc->xid);
|
|
|
|
if (proc->pid != 0)
|
|
|
|
values[10] = Int32GetDatum(proc->pid);
|
|
|
|
else
|
|
|
|
nulls[10] = 'n';
|
|
|
|
values[11] = DirectFunctionCall1(textin,
|
2005-12-09 02:22:04 +01:00
|
|
|
CStringGetDatum(GetLockmodeName(LOCK_LOCKMETHOD(*lock),
|
|
|
|
mode)));
|
2005-06-18 21:33:42 +02:00
|
|
|
values[12] = BoolGetDatum(granted);
|
2002-08-17 15:11:43 +02:00
|
|
|
|
2004-04-01 23:28:47 +02:00
|
|
|
tuple = heap_formtuple(funcctx->tuple_desc, values, nulls);
|
|
|
|
result = HeapTupleGetDatum(tuple);
|
2002-08-29 19:14:33 +02:00
|
|
|
SRF_RETURN_NEXT(funcctx, result);
|
2002-08-17 15:11:43 +02:00
|
|
|
}
|
|
|
|
|
2002-08-29 19:14:33 +02:00
|
|
|
SRF_RETURN_DONE(funcctx);
|
2002-08-17 15:11:43 +02:00
|
|
|
}
|