1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* lock.c
|
2000-12-22 01:51:54 +01:00
|
|
|
* POSTGRES low-level lock mechanism
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2004-12-31 23:04:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2005-08-21 01:26:37 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/storage/lmgr/lock.c,v 1.157 2005/08/20 23:26:23 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* NOTES
|
1997-09-07 07:04:48 +02:00
|
|
|
* Outside modules can create a lock table and acquire/release
|
|
|
|
* locks. A lock table is a shared memory hash table. When
|
2000-12-22 01:51:54 +01:00
|
|
|
* a process tries to acquire a lock of a type that conflicts
|
1997-09-07 07:04:48 +02:00
|
|
|
* with existing locks, it is put to sleep using the routines
|
|
|
|
* in storage/lmgr/proc.c.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-12-22 01:51:54 +01:00
|
|
|
* For the most part, this code should be invoked via lmgr.c
|
|
|
|
* or another lock-management module, not directly.
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Interface:
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1998-08-25 23:20:32 +02:00
|
|
|
* LockAcquire(), LockRelease(), LockMethodTableInit(),
|
2004-07-01 02:52:04 +02:00
|
|
|
* LockMethodTableRename(), LockReleaseAll(),
|
2001-01-25 04:31:16 +01:00
|
|
|
* LockCheckConflicts(), GrantLock()
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2001-01-25 04:31:16 +01:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1998-08-25 23:20:32 +02:00
|
|
|
#include <signal.h>
|
2004-07-17 05:32:14 +02:00
|
|
|
#include <unistd.h>
|
1996-11-08 07:02:30 +01:00
|
|
|
|
2005-06-18 00:32:51 +02:00
|
|
|
#include "access/twophase.h"
|
|
|
|
#include "access/twophase_rmgr.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "access/xact.h"
|
1998-01-25 06:15:15 +01:00
|
|
|
#include "miscadmin.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "storage/proc.h"
|
2000-07-17 05:05:41 +02:00
|
|
|
#include "utils/memutils.h"
|
1998-08-25 23:20:32 +02:00
|
|
|
#include "utils/ps_status.h"
|
2004-07-17 05:32:14 +02:00
|
|
|
#include "utils/resowner.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2001-06-28 01:31:40 +02:00
|
|
|
|
|
|
|
/* This configuration variable is used to set the lock table size */
|
2001-10-25 07:50:21 +02:00
|
|
|
int max_locks_per_xact; /* set by guc.c */
|
2001-06-28 01:31:40 +02:00
|
|
|
|
2005-08-21 01:26:37 +02:00
|
|
|
#define NLOCKENTS() \
|
|
|
|
mul_size(max_locks_per_xact, add_size(MaxBackends, max_prepared_xacts))
|
2005-06-18 00:32:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* Record that's written to 2PC state file when a lock is persisted */
|
|
|
|
typedef struct TwoPhaseLockRecord
|
|
|
|
{
|
|
|
|
LOCKTAG locktag;
|
|
|
|
LOCKMODE lockmode;
|
|
|
|
} TwoPhaseLockRecord;
|
2001-06-28 01:31:40 +02:00
|
|
|
|
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
/*
|
|
|
|
* map from lock method id to the lock table data structures
|
|
|
|
*/
|
|
|
|
static LockMethod LockMethods[MAX_LOCK_METHODS];
|
2004-08-29 07:07:03 +02:00
|
|
|
static HTAB *LockMethodLockHash[MAX_LOCK_METHODS];
|
|
|
|
static HTAB *LockMethodProcLockHash[MAX_LOCK_METHODS];
|
|
|
|
static HTAB *LockMethodLocalHash[MAX_LOCK_METHODS];
|
2004-08-27 19:07:42 +02:00
|
|
|
|
|
|
|
/* exported so lmgr.c can initialize it */
|
2004-08-29 07:07:03 +02:00
|
|
|
int NumLockMethods;
|
2004-08-27 19:07:42 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
/* private state for GrantAwaitedLock */
|
|
|
|
static LOCALLOCK *awaitedLock;
|
|
|
|
static ResourceOwner awaitedOwner;
|
|
|
|
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
static const char *const lock_mode_names[] =
|
2000-05-31 02:28:42 +02:00
|
|
|
{
|
2001-10-28 07:26:15 +01:00
|
|
|
"INVALID",
|
2001-10-30 06:38:56 +01:00
|
|
|
"AccessShareLock",
|
|
|
|
"RowShareLock",
|
|
|
|
"RowExclusiveLock",
|
|
|
|
"ShareUpdateExclusiveLock",
|
|
|
|
"ShareLock",
|
|
|
|
"ShareRowExclusiveLock",
|
|
|
|
"ExclusiveLock",
|
|
|
|
"AccessExclusiveLock"
|
1997-03-15 02:23:58 +01:00
|
|
|
};
|
1997-02-12 06:25:13 +01:00
|
|
|
|
2000-05-31 02:28:42 +02:00
|
|
|
|
|
|
|
#ifdef LOCK_DEBUG
|
|
|
|
|
|
|
|
/*------
|
|
|
|
* The following configuration options are available for lock debugging:
|
|
|
|
*
|
2001-03-22 05:01:46 +01:00
|
|
|
* TRACE_LOCKS -- give a bunch of output what's going on in this file
|
|
|
|
* TRACE_USERLOCKS -- same but for user locks
|
|
|
|
* TRACE_LOCK_OIDMIN-- do not trace locks for tables below this oid
|
|
|
|
* (use to avoid output on system tables)
|
|
|
|
* TRACE_LOCK_TABLE -- trace locks on this table (oid) unconditionally
|
|
|
|
* DEBUG_DEADLOCKS -- currently dumps locks at untimely occasions ;)
|
2001-02-23 00:20:06 +01:00
|
|
|
*
|
2001-09-29 06:02:27 +02:00
|
|
|
* Furthermore, but in storage/lmgr/lwlock.c:
|
|
|
|
* TRACE_LWLOCKS -- trace lightweight locks (pretty useless)
|
2000-05-31 02:28:42 +02:00
|
|
|
*
|
2001-02-23 00:20:06 +01:00
|
|
|
* Define LOCK_DEBUG at compile time to get all these enabled.
|
|
|
|
* --------
|
2000-05-31 02:28:42 +02:00
|
|
|
*/
|
|
|
|
|
2005-04-13 20:54:57 +02:00
|
|
|
int Trace_lock_oidmin = FirstNormalObjectId;
|
2001-03-22 05:01:46 +01:00
|
|
|
bool Trace_locks = false;
|
|
|
|
bool Trace_userlocks = false;
|
|
|
|
int Trace_lock_table = 0;
|
|
|
|
bool Debug_deadlocks = false;
|
2000-05-31 02:28:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
inline static bool
|
2001-03-22 05:01:46 +01:00
|
|
|
LOCK_DEBUG_ENABLED(const LOCK *lock)
|
2000-05-31 02:28:42 +02:00
|
|
|
{
|
2001-02-23 00:02:33 +01:00
|
|
|
return
|
2005-04-30 00:28:24 +02:00
|
|
|
(((Trace_locks && LOCK_LOCKMETHOD(*lock) == DEFAULT_LOCKMETHOD)
|
|
|
|
|| (Trace_userlocks && LOCK_LOCKMETHOD(*lock) == USER_LOCKMETHOD))
|
|
|
|
&& ((Oid) lock->tag.locktag_field2 >= (Oid) Trace_lock_oidmin))
|
|
|
|
|| (Trace_lock_table
|
|
|
|
&& (lock->tag.locktag_field2 == Trace_lock_table));
|
2000-05-31 02:28:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline static void
|
2001-03-22 05:01:46 +01:00
|
|
|
LOCK_PRINT(const char *where, const LOCK *lock, LOCKMODE type)
|
2000-05-31 02:28:42 +02:00
|
|
|
{
|
|
|
|
if (LOCK_DEBUG_ENABLED(lock))
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
elog(LOG,
|
2005-04-30 00:28:24 +02:00
|
|
|
"%s: lock(%lx) id(%u,%u,%u,%u,%u,%u) grantMask(%x) "
|
2001-02-23 00:02:33 +01:00
|
|
|
"req(%d,%d,%d,%d,%d,%d,%d)=%d "
|
|
|
|
"grant(%d,%d,%d,%d,%d,%d,%d)=%d wait(%d) type(%s)",
|
|
|
|
where, MAKE_OFFSET(lock),
|
2005-04-30 00:28:24 +02:00
|
|
|
lock->tag.locktag_field1, lock->tag.locktag_field2,
|
|
|
|
lock->tag.locktag_field3, lock->tag.locktag_field4,
|
|
|
|
lock->tag.locktag_type, lock->tag.locktag_lockmethodid,
|
|
|
|
lock->grantMask,
|
2001-02-23 00:02:33 +01:00
|
|
|
lock->requested[1], lock->requested[2], lock->requested[3],
|
2001-01-16 07:11:34 +01:00
|
|
|
lock->requested[4], lock->requested[5], lock->requested[6],
|
|
|
|
lock->requested[7], lock->nRequested,
|
2001-02-23 00:02:33 +01:00
|
|
|
lock->granted[1], lock->granted[2], lock->granted[3],
|
|
|
|
lock->granted[4], lock->granted[5], lock->granted[6],
|
|
|
|
lock->granted[7], lock->nGranted,
|
2001-02-23 00:20:06 +01:00
|
|
|
lock->waitProcs.size, lock_mode_names[type]);
|
2000-05-31 02:28:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline static void
|
2003-02-18 03:13:24 +01:00
|
|
|
PROCLOCK_PRINT(const char *where, const PROCLOCK *proclockP)
|
2000-05-31 02:28:42 +02:00
|
|
|
{
|
2005-04-30 00:28:24 +02:00
|
|
|
if (LOCK_DEBUG_ENABLED((LOCK *) MAKE_PTR(proclockP->tag.lock)))
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
elog(LOG,
|
2005-06-15 00:15:33 +02:00
|
|
|
"%s: proclock(%lx) lock(%lx) method(%u) proc(%lx) hold(%x)",
|
2003-02-18 03:13:24 +01:00
|
|
|
where, MAKE_OFFSET(proclockP), proclockP->tag.lock,
|
|
|
|
PROCLOCK_LOCKMETHOD(*(proclockP)),
|
2005-06-15 00:15:33 +02:00
|
|
|
proclockP->tag.proc, (int) proclockP->holdMask);
|
2000-05-31 02:28:42 +02:00
|
|
|
}
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
#else /* not LOCK_DEBUG */
|
2000-05-31 02:28:42 +02:00
|
|
|
|
|
|
|
#define LOCK_PRINT(where, lock, type)
|
2003-02-18 03:13:24 +01:00
|
|
|
#define PROCLOCK_PRINT(where, proclockP)
|
2004-08-27 19:07:42 +02:00
|
|
|
#endif /* not LOCK_DEBUG */
|
2000-05-31 02:28:42 +02:00
|
|
|
|
2004-05-28 07:13:32 +02:00
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
static void RemoveLocalLock(LOCALLOCK *locallock);
|
|
|
|
static void GrantLockLocal(LOCALLOCK *locallock, ResourceOwner owner);
|
2005-05-30 00:45:02 +02:00
|
|
|
static void WaitOnLock(LOCKMETHODID lockmethodid, LOCALLOCK *locallock,
|
|
|
|
ResourceOwner owner);
|
2005-02-04 03:04:53 +01:00
|
|
|
static bool UnGrantLock(LOCK *lock, LOCKMODE lockmode,
|
|
|
|
PROCLOCK *proclock, LockMethod lockMethodTable);
|
2005-05-20 01:30:18 +02:00
|
|
|
static void CleanUpLock(LOCKMETHODID lockmethodid, LOCK *lock,
|
|
|
|
PROCLOCK *proclock, bool wakeupNeeded);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2003-12-20 18:31:21 +01:00
|
|
|
|
2001-02-23 00:02:33 +01:00
|
|
|
/*
|
2005-06-18 00:32:51 +02:00
|
|
|
* InitLocks -- Init the lock module. Nothing to do here at present.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
void
|
2000-12-22 01:51:54 +01:00
|
|
|
InitLocks(void)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
/* NOP */
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-12-22 01:51:54 +01:00
|
|
|
|
2001-01-25 04:31:16 +01:00
|
|
|
/*
|
|
|
|
* Fetch the lock method table associated with a given lock
|
|
|
|
*/
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
LockMethod
|
2001-01-25 04:31:16 +01:00
|
|
|
GetLocksMethodTable(LOCK *lock)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
LOCKMETHODID lockmethodid = LOCK_LOCKMETHOD(*lock);
|
2001-01-25 04:31:16 +01:00
|
|
|
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
Assert(0 < lockmethodid && lockmethodid < NumLockMethods);
|
|
|
|
return LockMethods[lockmethodid];
|
2001-01-25 04:31:16 +01:00
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/*
|
1998-06-30 04:33:34 +02:00
|
|
|
* LockMethodInit -- initialize the lock table's lock type
|
1997-09-07 07:04:48 +02:00
|
|
|
* structures
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Notes: just copying. Should only be called once.
|
|
|
|
*/
|
|
|
|
static void
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
LockMethodInit(LockMethod lockMethodTable,
|
2004-05-28 07:13:32 +02:00
|
|
|
const LOCKMASK *conflictsP,
|
1998-09-01 06:40:42 +02:00
|
|
|
int numModes)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-07-19 01:06:20 +02:00
|
|
|
lockMethodTable->numLockModes = numModes;
|
2003-08-18 00:41:12 +02:00
|
|
|
/* copies useless zero element as well as the N lockmodes */
|
2004-05-28 07:13:32 +02:00
|
|
|
for (i = 0; i <= numModes; i++)
|
|
|
|
lockMethodTable->conflictTab[i] = conflictsP[i];
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-06-30 04:33:34 +02:00
|
|
|
* LockMethodTableInit -- initialize a lock table structure
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-06-28 05:33:33 +02:00
|
|
|
* NOTE: data structures allocated here are allocated permanently, using
|
2001-03-22 05:01:46 +01:00
|
|
|
* TopMemoryContext and shared memory. We don't ever release them anyway,
|
2000-06-28 05:33:33 +02:00
|
|
|
* and in normal multi-backend operation the lock table structures set up
|
|
|
|
* by the postmaster are inherited by each backend, so they must be in
|
|
|
|
* TopMemoryContext.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
LOCKMETHODID
|
2004-05-28 07:13:32 +02:00
|
|
|
LockMethodTableInit(const char *tabName,
|
|
|
|
const LOCKMASK *conflictsP,
|
2005-06-18 00:32:51 +02:00
|
|
|
int numModes)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
LockMethod newLockMethod;
|
2004-08-27 19:07:42 +02:00
|
|
|
LOCKMETHODID lockmethodid;
|
1997-09-08 04:41:22 +02:00
|
|
|
char *shmemName;
|
|
|
|
HASHCTL info;
|
|
|
|
int hash_flags;
|
|
|
|
bool found;
|
2000-12-22 01:51:54 +01:00
|
|
|
long init_table_size,
|
|
|
|
max_table_size;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-06-22 02:04:59 +02:00
|
|
|
if (numModes >= MAX_LOCKMODES)
|
2003-08-18 00:41:12 +02:00
|
|
|
elog(ERROR, "too many lock types %d (limit is %d)",
|
2004-08-29 07:07:03 +02:00
|
|
|
numModes, MAX_LOCKMODES - 1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-12-22 01:51:54 +01:00
|
|
|
/* Compute init/max size to request for lock hashtables */
|
2005-06-18 00:32:51 +02:00
|
|
|
max_table_size = NLOCKENTS();
|
2004-09-28 22:46:37 +02:00
|
|
|
init_table_size = max_table_size / 2;
|
2000-12-22 01:51:54 +01:00
|
|
|
|
2000-06-28 05:33:33 +02:00
|
|
|
/* Allocate a string for the shmem index table lookups. */
|
|
|
|
/* This is just temp space in this routine, so palloc is OK. */
|
|
|
|
shmemName = (char *) palloc(strlen(tabName) + 32);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-11-01 01:40:23 +01:00
|
|
|
/* each lock table has a header in shared memory */
|
2002-09-26 07:18:30 +02:00
|
|
|
sprintf(shmemName, "%s (lock method table)", tabName);
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
newLockMethod = (LockMethod)
|
|
|
|
ShmemInitStruct(shmemName, sizeof(LockMethodData), &found);
|
2002-07-19 01:06:20 +02:00
|
|
|
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
if (!newLockMethod)
|
2003-07-25 00:04:15 +02:00
|
|
|
elog(FATAL, "could not initialize lock table \"%s\"", tabName);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-02-23 00:02:33 +01:00
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* we're first - initialize
|
|
|
|
*/
|
|
|
|
if (!found)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
MemSet(newLockMethod, 0, sizeof(LockMethodData));
|
|
|
|
newLockMethod->masterLock = LockMgrLock;
|
2004-05-28 07:13:32 +02:00
|
|
|
LockMethodInit(newLockMethod, conflictsP, numModes);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-02-23 00:02:33 +01:00
|
|
|
/*
|
2000-12-22 01:51:54 +01:00
|
|
|
* other modules refer to the lock table by a lockmethod ID
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2004-08-27 19:07:42 +02:00
|
|
|
Assert(NumLockMethods < MAX_LOCK_METHODS);
|
|
|
|
lockmethodid = NumLockMethods++;
|
|
|
|
LockMethods[lockmethodid] = newLockMethod;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-02-23 00:02:33 +01:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* allocate a hash table for LOCK structs. This is used to store
|
|
|
|
* per-locked-object information.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2004-08-27 19:07:42 +02:00
|
|
|
MemSet(&info, 0, sizeof(info));
|
2001-10-01 07:36:17 +02:00
|
|
|
info.keysize = sizeof(LOCKTAG);
|
|
|
|
info.entrysize = sizeof(LOCK);
|
1997-09-07 07:04:48 +02:00
|
|
|
info.hash = tag_hash;
|
|
|
|
hash_flags = (HASH_ELEM | HASH_FUNCTION);
|
|
|
|
|
|
|
|
sprintf(shmemName, "%s (lock hash)", tabName);
|
2004-08-27 19:07:42 +02:00
|
|
|
LockMethodLockHash[lockmethodid] = ShmemInitHash(shmemName,
|
2004-08-29 07:07:03 +02:00
|
|
|
init_table_size,
|
|
|
|
max_table_size,
|
|
|
|
&info,
|
|
|
|
hash_flags);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
if (!LockMethodLockHash[lockmethodid])
|
2003-07-25 00:04:15 +02:00
|
|
|
elog(FATAL, "could not initialize lock table \"%s\"", tabName);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-02-23 00:02:33 +01:00
|
|
|
/*
|
2002-09-04 22:31:48 +02:00
|
|
|
* allocate a hash table for PROCLOCK structs. This is used to store
|
2004-08-27 19:07:42 +02:00
|
|
|
* per-lock-holder information.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2002-07-19 02:17:40 +02:00
|
|
|
info.keysize = sizeof(PROCLOCKTAG);
|
|
|
|
info.entrysize = sizeof(PROCLOCK);
|
1997-09-07 07:04:48 +02:00
|
|
|
info.hash = tag_hash;
|
|
|
|
hash_flags = (HASH_ELEM | HASH_FUNCTION);
|
|
|
|
|
2003-02-18 03:13:24 +01:00
|
|
|
sprintf(shmemName, "%s (proclock hash)", tabName);
|
2004-08-27 19:07:42 +02:00
|
|
|
LockMethodProcLockHash[lockmethodid] = ShmemInitHash(shmemName,
|
2004-08-29 07:07:03 +02:00
|
|
|
init_table_size,
|
|
|
|
max_table_size,
|
|
|
|
&info,
|
|
|
|
hash_flags);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
if (!LockMethodProcLockHash[lockmethodid])
|
|
|
|
elog(FATAL, "could not initialize lock table \"%s\"", tabName);
|
|
|
|
|
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* allocate a non-shared hash table for LOCALLOCK structs. This is
|
|
|
|
* used to store lock counts and resource owner information.
|
2004-08-27 19:07:42 +02:00
|
|
|
*
|
|
|
|
* The non-shared table could already exist in this process (this occurs
|
2004-08-29 07:07:03 +02:00
|
|
|
* when the postmaster is recreating shared memory after a backend
|
|
|
|
* crash). If so, delete and recreate it. (We could simply leave it,
|
|
|
|
* since it ought to be empty in the postmaster, but for safety let's
|
|
|
|
* zap it.)
|
2004-08-27 19:07:42 +02:00
|
|
|
*/
|
|
|
|
if (LockMethodLocalHash[lockmethodid])
|
|
|
|
hash_destroy(LockMethodLocalHash[lockmethodid]);
|
|
|
|
|
|
|
|
info.keysize = sizeof(LOCALLOCKTAG);
|
|
|
|
info.entrysize = sizeof(LOCALLOCK);
|
|
|
|
info.hash = tag_hash;
|
|
|
|
hash_flags = (HASH_ELEM | HASH_FUNCTION);
|
|
|
|
|
|
|
|
sprintf(shmemName, "%s (locallock hash)", tabName);
|
|
|
|
LockMethodLocalHash[lockmethodid] = hash_create(shmemName,
|
2004-11-20 21:16:54 +01:00
|
|
|
128,
|
2004-08-27 19:07:42 +02:00
|
|
|
&info,
|
|
|
|
hash_flags);
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
pfree(shmemName);
|
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
return lockmethodid;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-12-22 01:51:54 +01:00
|
|
|
* LockMethodTableRename -- allocate another lockmethod ID to the same
|
1997-09-07 07:04:48 +02:00
|
|
|
* lock table.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2005-04-30 00:28:24 +02:00
|
|
|
* NOTES: This function makes it possible to have different lockmethodids,
|
|
|
|
* and hence different locking semantics, while still storing all
|
|
|
|
* the data in one shared-memory hashtable.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1998-08-25 23:20:32 +02:00
|
|
|
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
LOCKMETHODID
|
|
|
|
LockMethodTableRename(LOCKMETHODID lockmethodid)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
LOCKMETHODID newLockMethodId;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-06-30 04:33:34 +02:00
|
|
|
if (NumLockMethods >= MAX_LOCK_METHODS)
|
1998-09-01 05:29:17 +02:00
|
|
|
return INVALID_LOCKMETHOD;
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
if (LockMethods[lockmethodid] == INVALID_LOCKMETHOD)
|
1998-09-01 05:29:17 +02:00
|
|
|
return INVALID_LOCKMETHOD;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-12-22 01:51:54 +01:00
|
|
|
/* other modules refer to the lock table by a lockmethod ID */
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
newLockMethodId = NumLockMethods;
|
1998-06-30 04:33:34 +02:00
|
|
|
NumLockMethods++;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
LockMethods[newLockMethodId] = LockMethods[lockmethodid];
|
2004-08-26 19:23:30 +02:00
|
|
|
LockMethodLockHash[newLockMethodId] = LockMethodLockHash[lockmethodid];
|
|
|
|
LockMethodProcLockHash[newLockMethodId] = LockMethodProcLockHash[lockmethodid];
|
2004-08-27 19:07:42 +02:00
|
|
|
LockMethodLocalHash[newLockMethodId] = LockMethodLocalHash[lockmethodid];
|
2004-08-26 19:23:30 +02:00
|
|
|
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
return newLockMethodId;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* LockAcquire -- Check for lock conflicts, sleep if conflict found,
|
1997-09-07 07:04:48 +02:00
|
|
|
* set lock if/when no conflicts.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2005-06-15 00:15:33 +02:00
|
|
|
* Inputs:
|
|
|
|
* lockmethodid: identifies which lock table to use
|
|
|
|
* locktag: unique identifier for the lockable object
|
|
|
|
* isTempObject: is the lockable object a temporary object? (Under 2PC,
|
|
|
|
* such locks cannot be persisted)
|
|
|
|
* lockmode: lock mode to acquire
|
|
|
|
* sessionLock: if true, acquire lock for session not current transaction
|
|
|
|
* dontWait: if true, don't wait to acquire lock
|
|
|
|
*
|
2005-05-30 00:45:02 +02:00
|
|
|
* Returns one of:
|
|
|
|
* LOCKACQUIRE_NOT_AVAIL lock not available, and dontWait=true
|
|
|
|
* LOCKACQUIRE_OK lock successfully acquired
|
|
|
|
* LOCKACQUIRE_ALREADY_HELD incremented count for lock already held
|
|
|
|
*
|
|
|
|
* In the normal case where dontWait=false and the caller doesn't need to
|
|
|
|
* distinguish a freshly acquired lock from one already taken earlier in
|
|
|
|
* this same transaction, there is no need to examine the return value.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2001-06-22 02:04:59 +02:00
|
|
|
* Side Effects: The lock is acquired and recorded in lock tables.
|
|
|
|
*
|
|
|
|
* NOTE: if we wait for the lock, there is no way to abort the wait
|
|
|
|
* short of aborting the transaction.
|
1998-08-25 23:20:32 +02:00
|
|
|
*
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Note on User Locks:
|
1998-08-25 23:20:32 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* User locks are handled totally on the application side as
|
|
|
|
* long term cooperative locks which extend beyond the normal
|
|
|
|
* transaction boundaries. Their purpose is to indicate to an
|
|
|
|
* application that someone is `working' on an item. So it is
|
|
|
|
* possible to put an user lock on a tuple's oid, retrieve the
|
|
|
|
* tuple, work on it for an hour and then update it and remove
|
|
|
|
* the lock. While the lock is active other clients can still
|
|
|
|
* read and write the tuple but they can be aware that it has
|
|
|
|
* been locked at the application level by someone.
|
2005-04-30 00:28:24 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* User locks and normal locks are completely orthogonal and
|
2005-04-30 00:28:24 +02:00
|
|
|
* they don't interfere with each other.
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* User locks are always non blocking, therefore they are never
|
|
|
|
* acquired if already held by another process. They must be
|
|
|
|
* released explicitly by the application but they are released
|
|
|
|
* automatically when a backend terminates.
|
1998-08-25 23:20:32 +02:00
|
|
|
* They are indicated by a lockmethod 2 which is an alias for the
|
2005-04-30 00:28:24 +02:00
|
|
|
* normal lock table.
|
1996-10-11 05:22:59 +02:00
|
|
|
*
|
1998-06-30 04:33:34 +02:00
|
|
|
* The lockmode parameter can have the same values for normal locks
|
1997-09-07 07:04:48 +02:00
|
|
|
* although probably only WRITE_LOCK can have some practical use.
|
1996-10-11 05:22:59 +02:00
|
|
|
*
|
1998-08-25 23:20:32 +02:00
|
|
|
* DZ - 22 Nov 1997
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2005-05-30 00:45:02 +02:00
|
|
|
LockAcquireResult
|
2005-06-15 00:15:33 +02:00
|
|
|
LockAcquire(LOCKMETHODID lockmethodid,
|
|
|
|
LOCKTAG *locktag,
|
|
|
|
bool isTempObject,
|
|
|
|
LOCKMODE lockmode,
|
|
|
|
bool sessionLock,
|
|
|
|
bool dontWait)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-08-27 19:07:42 +02:00
|
|
|
LOCALLOCKTAG localtag;
|
|
|
|
LOCALLOCK *locallock;
|
|
|
|
LOCK *lock;
|
2003-02-18 03:13:24 +01:00
|
|
|
PROCLOCK *proclock;
|
|
|
|
PROCLOCKTAG proclocktag;
|
1997-09-08 04:41:22 +02:00
|
|
|
bool found;
|
2004-08-27 19:07:42 +02:00
|
|
|
ResourceOwner owner;
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockId masterLock;
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
LockMethod lockMethodTable;
|
1997-09-08 04:41:22 +02:00
|
|
|
int status;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-05-31 02:28:42 +02:00
|
|
|
#ifdef LOCK_DEBUG
|
2005-04-30 00:28:24 +02:00
|
|
|
if (Trace_userlocks && lockmethodid == USER_LOCKMETHOD)
|
|
|
|
elog(LOG, "LockAcquire: user lock [%u,%u] %s",
|
|
|
|
locktag->locktag_field1, locktag->locktag_field2,
|
|
|
|
lock_mode_names[lockmode]);
|
1996-10-11 05:22:59 +02:00
|
|
|
#endif
|
|
|
|
|
2005-04-30 00:28:24 +02:00
|
|
|
/* ugly */
|
|
|
|
locktag->locktag_lockmethodid = lockmethodid;
|
1998-08-25 23:20:32 +02:00
|
|
|
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
Assert(lockmethodid < NumLockMethods);
|
|
|
|
lockMethodTable = LockMethods[lockmethodid];
|
1998-06-30 04:33:34 +02:00
|
|
|
if (!lockMethodTable)
|
2005-05-30 00:45:02 +02:00
|
|
|
elog(ERROR, "unrecognized lock method: %d", lockmethodid);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
/* Session locks and user locks are not transactional */
|
2005-06-15 00:15:33 +02:00
|
|
|
if (!sessionLock && lockmethodid == DEFAULT_LOCKMETHOD)
|
2004-08-27 19:07:42 +02:00
|
|
|
owner = CurrentResourceOwner;
|
|
|
|
else
|
|
|
|
owner = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find or create a LOCALLOCK entry for this lock and lockmode
|
|
|
|
*/
|
2004-08-29 07:07:03 +02:00
|
|
|
MemSet(&localtag, 0, sizeof(localtag)); /* must clear padding */
|
2004-08-27 19:07:42 +02:00
|
|
|
localtag.lock = *locktag;
|
|
|
|
localtag.mode = lockmode;
|
|
|
|
|
|
|
|
locallock = (LOCALLOCK *) hash_search(LockMethodLocalHash[lockmethodid],
|
|
|
|
(void *) &localtag,
|
|
|
|
HASH_ENTER, &found);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if it's a new locallock object, initialize it
|
|
|
|
*/
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
locallock->lock = NULL;
|
|
|
|
locallock->proclock = NULL;
|
2005-06-15 00:15:33 +02:00
|
|
|
locallock->isTempObject = isTempObject;
|
2004-08-27 19:07:42 +02:00
|
|
|
locallock->nLocks = 0;
|
|
|
|
locallock->numLockOwners = 0;
|
|
|
|
locallock->maxLockOwners = 8;
|
|
|
|
locallock->lockOwners = NULL;
|
|
|
|
locallock->lockOwners = (LOCALLOCKOWNER *)
|
|
|
|
MemoryContextAlloc(TopMemoryContext,
|
2004-08-29 07:07:03 +02:00
|
|
|
locallock->maxLockOwners * sizeof(LOCALLOCKOWNER));
|
2004-08-27 19:07:42 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-06-15 00:15:33 +02:00
|
|
|
Assert(locallock->isTempObject == isTempObject);
|
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
/* Make sure there will be room to remember the lock */
|
|
|
|
if (locallock->numLockOwners >= locallock->maxLockOwners)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
int newsize = locallock->maxLockOwners * 2;
|
2004-08-27 19:07:42 +02:00
|
|
|
|
|
|
|
locallock->lockOwners = (LOCALLOCKOWNER *)
|
|
|
|
repalloc(locallock->lockOwners,
|
|
|
|
newsize * sizeof(LOCALLOCKOWNER));
|
|
|
|
locallock->maxLockOwners = newsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* If we already hold the lock, we can just increase the count
|
|
|
|
* locally.
|
2004-08-27 19:07:42 +02:00
|
|
|
*/
|
|
|
|
if (locallock->nLocks > 0)
|
|
|
|
{
|
|
|
|
GrantLockLocal(locallock, owner);
|
2005-05-30 00:45:02 +02:00
|
|
|
return LOCKACQUIRE_ALREADY_HELD;
|
2004-08-27 19:07:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Otherwise we've got to mess with the shared lock table.
|
|
|
|
*/
|
2002-07-19 01:06:20 +02:00
|
|
|
masterLock = lockMethodTable->masterLock;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockAcquire(masterLock, LW_EXCLUSIVE);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-25 23:20:32 +02:00
|
|
|
/*
|
2004-08-27 19:07:42 +02:00
|
|
|
* Find or create a lock with this tag.
|
|
|
|
*
|
|
|
|
* Note: if the locallock object already existed, it might have a pointer
|
|
|
|
* to the lock already ... but we probably should not assume that that
|
|
|
|
* pointer is valid, since a lock object with no locks can go away
|
|
|
|
* anytime.
|
1998-08-25 23:20:32 +02:00
|
|
|
*/
|
2003-12-20 18:31:21 +01:00
|
|
|
lock = (LOCK *) hash_search(LockMethodLockHash[lockmethodid],
|
2001-10-01 07:36:17 +02:00
|
|
|
(void *) locktag,
|
2005-05-29 06:23:07 +02:00
|
|
|
HASH_ENTER_NULL, &found);
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!lock)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockRelease(masterLock);
|
2003-07-25 00:04:15 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
2003-10-16 22:59:35 +02:00
|
|
|
errmsg("out of shared memory"),
|
2004-08-29 07:07:03 +02:00
|
|
|
errhint("You may need to increase max_locks_per_transaction.")));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2004-08-27 19:07:42 +02:00
|
|
|
locallock->lock = lock;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-02-23 00:02:33 +01:00
|
|
|
/*
|
2000-12-22 01:51:54 +01:00
|
|
|
* if it's a new lock object, initialize it
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
if (!found)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-01-16 07:11:34 +01:00
|
|
|
lock->grantMask = 0;
|
|
|
|
lock->waitMask = 0;
|
2004-08-27 19:07:42 +02:00
|
|
|
SHMQueueInit(&(lock->procLocks));
|
2001-01-22 23:30:06 +01:00
|
|
|
ProcQueueInit(&(lock->waitProcs));
|
2001-01-16 07:11:34 +01:00
|
|
|
lock->nRequested = 0;
|
|
|
|
lock->nGranted = 0;
|
2005-05-11 03:26:02 +02:00
|
|
|
MemSet(lock->requested, 0, sizeof(int) * MAX_LOCKMODES);
|
|
|
|
MemSet(lock->granted, 0, sizeof(int) * MAX_LOCKMODES);
|
1998-08-25 23:20:32 +02:00
|
|
|
LOCK_PRINT("LockAcquire: new", lock, lockmode);
|
1998-09-01 06:40:42 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1998-08-25 23:20:32 +02:00
|
|
|
LOCK_PRINT("LockAcquire: found", lock, lockmode);
|
2001-01-16 07:11:34 +01:00
|
|
|
Assert((lock->nRequested >= 0) && (lock->requested[lockmode] >= 0));
|
|
|
|
Assert((lock->nGranted >= 0) && (lock->granted[lockmode] >= 0));
|
|
|
|
Assert(lock->nGranted <= lock->nRequested);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-02-23 00:02:33 +01:00
|
|
|
/*
|
2003-02-18 03:13:24 +01:00
|
|
|
* Create the hash key for the proclock table.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2005-06-15 00:15:33 +02:00
|
|
|
MemSet(&proclocktag, 0, sizeof(PROCLOCKTAG)); /* must clear padding */
|
2003-02-18 03:13:24 +01:00
|
|
|
proclocktag.lock = MAKE_OFFSET(lock);
|
|
|
|
proclocktag.proc = MAKE_OFFSET(MyProc);
|
1996-10-11 05:22:59 +02:00
|
|
|
|
1998-08-25 23:20:32 +02:00
|
|
|
/*
|
2003-02-18 03:13:24 +01:00
|
|
|
* Find or create a proclock entry with this tag
|
1998-08-25 23:20:32 +02:00
|
|
|
*/
|
2004-08-27 19:07:42 +02:00
|
|
|
proclock = (PROCLOCK *) hash_search(LockMethodProcLockHash[lockmethodid],
|
2003-08-04 02:43:34 +02:00
|
|
|
(void *) &proclocktag,
|
2005-05-29 06:23:07 +02:00
|
|
|
HASH_ENTER_NULL, &found);
|
2003-02-18 03:13:24 +01:00
|
|
|
if (!proclock)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-09-12 20:30:50 +02:00
|
|
|
/* Ooops, not enough shmem for the proclock */
|
|
|
|
if (lock->nRequested == 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* There are no other requestors of this lock, so garbage-collect
|
|
|
|
* the lock object. We *must* do this to avoid a permanent leak
|
|
|
|
* of shared memory, because there won't be anything to cause
|
|
|
|
* anyone to release the lock object later.
|
|
|
|
*/
|
|
|
|
Assert(SHMQueueEmpty(&(lock->procLocks)));
|
2005-05-20 01:30:18 +02:00
|
|
|
if (!hash_search(LockMethodLockHash[lockmethodid],
|
|
|
|
(void *) &(lock->tag),
|
|
|
|
HASH_REMOVE, NULL))
|
|
|
|
elog(PANIC, "lock table corrupted");
|
2004-09-12 20:30:50 +02:00
|
|
|
}
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockRelease(masterLock);
|
2003-07-25 00:04:15 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
2003-10-16 22:59:35 +02:00
|
|
|
errmsg("out of shared memory"),
|
2004-08-29 07:07:03 +02:00
|
|
|
errhint("You may need to increase max_locks_per_transaction.")));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2004-08-27 19:07:42 +02:00
|
|
|
locallock->proclock = proclock;
|
1998-08-25 23:20:32 +02:00
|
|
|
|
|
|
|
/*
|
2000-12-22 01:51:54 +01:00
|
|
|
* If new, initialize the new entry
|
1998-08-25 23:20:32 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!found)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-08-27 19:07:42 +02:00
|
|
|
proclock->holdMask = 0;
|
2005-06-15 00:15:33 +02:00
|
|
|
proclock->releaseMask = 0;
|
2003-02-18 03:13:24 +01:00
|
|
|
/* Add proclock to appropriate lists */
|
2004-08-27 19:07:42 +02:00
|
|
|
SHMQueueInsertBefore(&lock->procLocks, &proclock->lockLink);
|
|
|
|
SHMQueueInsertBefore(&MyProc->procLocks, &proclock->procLink);
|
2003-02-18 03:13:24 +01:00
|
|
|
PROCLOCK_PRINT("LockAcquire: new", proclock);
|
1998-09-01 06:40:42 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-02-18 03:13:24 +01:00
|
|
|
PROCLOCK_PRINT("LockAcquire: found", proclock);
|
2004-08-27 19:07:42 +02:00
|
|
|
Assert((proclock->holdMask & ~lock->grantMask) == 0);
|
2000-11-08 23:10:03 +01:00
|
|
|
|
2000-12-22 01:51:54 +01:00
|
|
|
#ifdef CHECK_DEADLOCK_RISK
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-11-08 23:10:03 +01:00
|
|
|
/*
|
|
|
|
* Issue warning if we already hold a lower-level lock on this
|
|
|
|
* object and do not hold a lock of the requested level or higher.
|
|
|
|
* This indicates a deadlock-prone coding practice (eg, we'd have
|
|
|
|
* a deadlock if another backend were following the same code path
|
|
|
|
* at about the same time).
|
|
|
|
*
|
2001-03-22 05:01:46 +01:00
|
|
|
* This is not enabled by default, because it may generate log
|
|
|
|
* entries about user-level coding practices that are in fact safe
|
|
|
|
* in context. It can be enabled to help find system-level
|
|
|
|
* problems.
|
2000-12-22 01:51:54 +01:00
|
|
|
*
|
2001-03-22 05:01:46 +01:00
|
|
|
* XXX Doing numeric comparison on the lockmodes is a hack; it'd be
|
|
|
|
* better to use a table. For now, though, this works.
|
2000-11-08 23:10:03 +01:00
|
|
|
*/
|
|
|
|
{
|
2005-06-15 00:15:33 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = lockMethodTable->numLockModes; i > 0; i--)
|
2000-11-08 23:10:03 +01:00
|
|
|
{
|
2005-06-15 00:15:33 +02:00
|
|
|
if (proclock->holdMask & LOCKBIT_ON(i))
|
|
|
|
{
|
|
|
|
if (i >= (int) lockmode)
|
|
|
|
break; /* safe: we have a lock >= req level */
|
|
|
|
elog(LOG, "deadlock risk: raising lock level"
|
|
|
|
" from %s to %s on object %u/%u/%u",
|
|
|
|
lock_mode_names[i], lock_mode_names[lockmode],
|
|
|
|
lock->tag.locktag_field1, lock->tag.locktag_field2,
|
|
|
|
lock->tag.locktag_field3);
|
|
|
|
break;
|
|
|
|
}
|
2000-11-08 23:10:03 +01:00
|
|
|
}
|
|
|
|
}
|
2001-11-05 18:46:40 +01:00
|
|
|
#endif /* CHECK_DEADLOCK_RISK */
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-02-23 00:02:33 +01:00
|
|
|
/*
|
2001-01-16 07:11:34 +01:00
|
|
|
* lock->nRequested and lock->requested[] count the total number of
|
2001-03-22 05:01:46 +01:00
|
|
|
* requests, whether granted or waiting, so increment those
|
|
|
|
* immediately. The other counts don't increment till we get the lock.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2001-01-16 07:11:34 +01:00
|
|
|
lock->nRequested++;
|
|
|
|
lock->requested[lockmode]++;
|
|
|
|
Assert((lock->nRequested > 0) && (lock->requested[lockmode] > 0));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-02-23 00:02:33 +01:00
|
|
|
/*
|
2005-06-15 00:15:33 +02:00
|
|
|
* We shouldn't already hold the desired lock; else locallock table
|
|
|
|
* is broken.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2005-06-15 00:15:33 +02:00
|
|
|
if (proclock->holdMask & LOCKBIT_ON(lockmode))
|
|
|
|
elog(ERROR, "lock %s on object %u/%u/%u is already held",
|
|
|
|
lock_mode_names[lockmode],
|
|
|
|
lock->tag.locktag_field1, lock->tag.locktag_field2,
|
|
|
|
lock->tag.locktag_field3);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-02-23 00:02:33 +01:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* If lock requested conflicts with locks requested by waiters, must
|
|
|
|
* join wait queue. Otherwise, check for conflict with already-held
|
|
|
|
* locks. (That's last because most complex check.)
|
1999-05-07 03:23:11 +02:00
|
|
|
*/
|
2002-07-19 01:06:20 +02:00
|
|
|
if (lockMethodTable->conflictTab[lockmode] & lock->waitMask)
|
2001-01-25 04:31:16 +01:00
|
|
|
status = STATUS_FOUND;
|
1999-05-07 03:23:11 +02:00
|
|
|
else
|
2001-01-25 04:31:16 +01:00
|
|
|
status = LockCheckConflicts(lockMethodTable, lockmode,
|
2005-06-15 00:15:33 +02:00
|
|
|
lock, proclock, MyProc);
|
1999-05-07 03:23:11 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (status == STATUS_OK)
|
2001-01-25 04:31:16 +01:00
|
|
|
{
|
|
|
|
/* No conflict with held or previously requested locks */
|
2003-02-18 03:13:24 +01:00
|
|
|
GrantLock(lock, proclock, lockmode);
|
2004-08-27 19:07:42 +02:00
|
|
|
GrantLockLocal(locallock, owner);
|
2001-01-25 04:31:16 +01:00
|
|
|
}
|
|
|
|
else
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-01-25 04:31:16 +01:00
|
|
|
Assert(status == STATUS_FOUND);
|
2001-10-25 07:50:21 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2001-06-22 02:04:59 +02:00
|
|
|
* We can't acquire the lock immediately. If caller specified no
|
2005-05-30 00:45:02 +02:00
|
|
|
* blocking, remove useless table entries and return NOT_AVAIL
|
|
|
|
* without waiting.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2001-06-22 02:04:59 +02:00
|
|
|
if (dontWait)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2004-08-27 19:07:42 +02:00
|
|
|
if (proclock->holdMask == 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-02-18 03:13:24 +01:00
|
|
|
SHMQueueDelete(&proclock->lockLink);
|
|
|
|
SHMQueueDelete(&proclock->procLink);
|
2005-05-20 01:30:18 +02:00
|
|
|
if (!hash_search(LockMethodProcLockHash[lockmethodid],
|
|
|
|
(void *) &(proclock->tag),
|
|
|
|
HASH_REMOVE, NULL))
|
|
|
|
elog(PANIC, "proclock table corrupted");
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-09-01 06:40:42 +02:00
|
|
|
else
|
2004-08-27 19:07:42 +02:00
|
|
|
PROCLOCK_PRINT("LockAcquire: NOWAIT", proclock);
|
2001-01-16 07:11:34 +01:00
|
|
|
lock->nRequested--;
|
|
|
|
lock->requested[lockmode]--;
|
2001-06-22 02:04:59 +02:00
|
|
|
LOCK_PRINT("LockAcquire: conditional lock failed", lock, lockmode);
|
2001-01-16 07:11:34 +01:00
|
|
|
Assert((lock->nRequested > 0) && (lock->requested[lockmode] >= 0));
|
|
|
|
Assert(lock->nGranted <= lock->nRequested);
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockRelease(masterLock);
|
2004-08-27 19:07:42 +02:00
|
|
|
if (locallock->nLocks == 0)
|
|
|
|
RemoveLocalLock(locallock);
|
2005-05-30 00:45:02 +02:00
|
|
|
return LOCKACQUIRE_NOT_AVAIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1999-05-07 03:23:11 +02:00
|
|
|
/*
|
2005-06-15 00:15:33 +02:00
|
|
|
* Set bitmask of locks this process already holds on this object.
|
1999-05-07 03:23:11 +02:00
|
|
|
*/
|
2005-06-15 00:15:33 +02:00
|
|
|
MyProc->heldLocks = proclock->holdMask;
|
1999-05-07 03:23:11 +02:00
|
|
|
|
2000-12-22 01:51:54 +01:00
|
|
|
/*
|
|
|
|
* Sleep till someone wakes me up.
|
|
|
|
*/
|
2005-05-30 00:45:02 +02:00
|
|
|
WaitOnLock(lockmethodid, locallock, owner);
|
1998-09-01 06:40:42 +02:00
|
|
|
|
2001-01-14 06:08:17 +01:00
|
|
|
/*
|
|
|
|
* NOTE: do not do any material change of state between here and
|
2001-03-22 05:01:46 +01:00
|
|
|
* return. All required changes in locktable state must have been
|
|
|
|
* done when the lock was granted to us --- see notes in
|
|
|
|
* WaitOnLock.
|
2001-01-14 06:08:17 +01:00
|
|
|
*/
|
|
|
|
|
1998-08-25 23:20:32 +02:00
|
|
|
/*
|
2003-02-18 03:13:24 +01:00
|
|
|
* Check the proclock entry status, in case something in the ipc
|
1998-09-01 06:40:42 +02:00
|
|
|
* communication doesn't work correctly.
|
1998-08-25 23:20:32 +02:00
|
|
|
*/
|
2004-08-27 19:07:42 +02:00
|
|
|
if (!(proclock->holdMask & LOCKBIT_ON(lockmode)))
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
2003-02-18 03:13:24 +01:00
|
|
|
PROCLOCK_PRINT("LockAcquire: INCONSISTENT", proclock);
|
2000-05-31 02:28:42 +02:00
|
|
|
LOCK_PRINT("LockAcquire: INCONSISTENT", lock, lockmode);
|
1998-08-25 23:20:32 +02:00
|
|
|
/* Should we retry ? */
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockRelease(masterLock);
|
2005-05-30 00:45:02 +02:00
|
|
|
elog(ERROR, "LockAcquire failed");
|
1998-08-25 23:20:32 +02:00
|
|
|
}
|
2003-02-18 03:13:24 +01:00
|
|
|
PROCLOCK_PRINT("LockAcquire: granted", proclock);
|
1998-08-25 23:20:32 +02:00
|
|
|
LOCK_PRINT("LockAcquire: granted", lock, lockmode);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockRelease(masterLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-30 00:45:02 +02:00
|
|
|
return LOCKACQUIRE_OK;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
/*
|
|
|
|
* Subroutine to free a locallock entry
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
RemoveLocalLock(LOCALLOCK *locallock)
|
|
|
|
{
|
|
|
|
LOCKMETHODID lockmethodid = LOCALLOCK_LOCKMETHOD(*locallock);
|
|
|
|
|
|
|
|
pfree(locallock->lockOwners);
|
|
|
|
locallock->lockOwners = NULL;
|
2005-05-20 01:30:18 +02:00
|
|
|
if (!hash_search(LockMethodLocalHash[lockmethodid],
|
|
|
|
(void *) &(locallock->tag),
|
|
|
|
HASH_REMOVE, NULL))
|
2004-08-27 19:07:42 +02:00
|
|
|
elog(WARNING, "locallock table corrupted");
|
|
|
|
}
|
|
|
|
|
2001-02-23 00:02:33 +01:00
|
|
|
/*
|
2001-01-25 04:31:16 +01:00
|
|
|
* LockCheckConflicts -- test whether requested lock conflicts
|
|
|
|
* with those already granted
|
|
|
|
*
|
|
|
|
* Returns STATUS_FOUND if conflict, STATUS_OK if no conflict.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* NOTES:
|
2001-01-25 04:31:16 +01:00
|
|
|
* Here's what makes this complicated: one process's locks don't
|
2005-06-15 00:15:33 +02:00
|
|
|
* conflict with one another, no matter what purpose they are held for
|
|
|
|
* (eg, session and transaction locks do not conflict).
|
2001-01-25 04:31:16 +01:00
|
|
|
* So, we must subtract off our own locks when determining whether the
|
|
|
|
* requested new lock conflicts with those already held.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
int
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
LockCheckConflicts(LockMethod lockMethodTable,
|
2001-01-25 04:31:16 +01:00
|
|
|
LOCKMODE lockmode,
|
|
|
|
LOCK *lock,
|
2003-02-18 03:13:24 +01:00
|
|
|
PROCLOCK *proclock,
|
2005-06-15 00:15:33 +02:00
|
|
|
PGPROC *proc)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2002-07-19 01:06:20 +02:00
|
|
|
int numLockModes = lockMethodTable->numLockModes;
|
2005-06-15 00:15:33 +02:00
|
|
|
LOCKMASK myLocks;
|
|
|
|
LOCKMASK otherLocks;
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
int i;
|
1998-09-01 06:40:42 +02:00
|
|
|
|
2001-02-23 00:02:33 +01:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* first check for global conflicts: If no locks conflict with my
|
|
|
|
* request, then I get the lock.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2001-01-16 07:11:34 +01:00
|
|
|
* Checking for conflict: lock->grantMask represents the types of
|
2001-03-22 05:01:46 +01:00
|
|
|
* currently held locks. conflictTable[lockmode] has a bit set for
|
|
|
|
* each type of lock that conflicts with request. Bitwise compare
|
|
|
|
* tells if there is a conflict.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2002-07-19 01:06:20 +02:00
|
|
|
if (!(lockMethodTable->conflictTab[lockmode] & lock->grantMask))
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2003-02-18 03:13:24 +01:00
|
|
|
PROCLOCK_PRINT("LockCheckConflicts: no conflict", proclock);
|
1998-09-01 05:29:17 +02:00
|
|
|
return STATUS_OK;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-02-23 00:02:33 +01:00
|
|
|
/*
|
2005-06-15 00:15:33 +02:00
|
|
|
* Rats. Something conflicts. But it could still be my own lock.
|
|
|
|
* We have to construct a conflict mask that does not reflect our own
|
|
|
|
* locks, but only lock types held by other processes.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2005-06-15 00:15:33 +02:00
|
|
|
myLocks = proclock->holdMask;
|
|
|
|
otherLocks = 0;
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
for (i = 1; i <= numLockModes; i++)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2005-06-15 00:15:33 +02:00
|
|
|
int myHolding = (myLocks & LOCKBIT_ON(i)) ? 1 : 0;
|
|
|
|
|
|
|
|
if (lock->granted[i] > myHolding)
|
|
|
|
otherLocks |= LOCKBIT_ON(i);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-02-23 00:02:33 +01:00
|
|
|
/*
|
2005-06-15 00:15:33 +02:00
|
|
|
* now check again for conflicts. 'otherLocks' describes the types of
|
2001-03-22 05:01:46 +01:00
|
|
|
* locks held by other processes. If one of these conflicts with the
|
|
|
|
* kind of lock that I want, there is a conflict and I have to sleep.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2005-06-15 00:15:33 +02:00
|
|
|
if (!(lockMethodTable->conflictTab[lockmode] & otherLocks))
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-12-22 01:51:54 +01:00
|
|
|
/* no conflict. OK to get the lock */
|
2003-02-18 03:13:24 +01:00
|
|
|
PROCLOCK_PRINT("LockCheckConflicts: resolved", proclock);
|
1998-09-01 05:29:17 +02:00
|
|
|
return STATUS_OK;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-02-18 03:13:24 +01:00
|
|
|
PROCLOCK_PRINT("LockCheckConflicts: conflicting", proclock);
|
1998-09-01 05:29:17 +02:00
|
|
|
return STATUS_FOUND;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-12-22 01:51:54 +01:00
|
|
|
/*
|
2003-02-18 03:13:24 +01:00
|
|
|
* GrantLock -- update the lock and proclock data structures to show
|
2001-01-16 07:11:34 +01:00
|
|
|
* the lock request has been granted.
|
2001-01-25 04:31:16 +01:00
|
|
|
*
|
|
|
|
* NOTE: if proc was blocked, it also needs to be removed from the wait list
|
2004-08-27 19:07:42 +02:00
|
|
|
* and have its waitLock/waitProcLock fields cleared. That's not done here.
|
2004-07-17 05:32:14 +02:00
|
|
|
*
|
2004-08-27 19:07:42 +02:00
|
|
|
* NOTE: the lock grant also has to be recorded in the associated LOCALLOCK
|
|
|
|
* table entry; but since we may be awaking some other process, we can't do
|
|
|
|
* that here; it's done by GrantLockLocal, instead.
|
1998-08-25 23:20:32 +02:00
|
|
|
*/
|
|
|
|
void
|
2003-02-18 03:13:24 +01:00
|
|
|
GrantLock(LOCK *lock, PROCLOCK *proclock, LOCKMODE lockmode)
|
1998-08-25 23:20:32 +02:00
|
|
|
{
|
2001-01-16 07:11:34 +01:00
|
|
|
lock->nGranted++;
|
|
|
|
lock->granted[lockmode]++;
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
lock->grantMask |= LOCKBIT_ON(lockmode);
|
2001-01-16 07:11:34 +01:00
|
|
|
if (lock->granted[lockmode] == lock->requested[lockmode])
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
lock->waitMask &= LOCKBIT_OFF(lockmode);
|
2004-08-27 19:07:42 +02:00
|
|
|
proclock->holdMask |= LOCKBIT_ON(lockmode);
|
1998-08-25 23:20:32 +02:00
|
|
|
LOCK_PRINT("GrantLock", lock, lockmode);
|
2001-01-16 07:11:34 +01:00
|
|
|
Assert((lock->nGranted > 0) && (lock->granted[lockmode] > 0));
|
|
|
|
Assert(lock->nGranted <= lock->nRequested);
|
2004-08-27 19:07:42 +02:00
|
|
|
}
|
|
|
|
|
2005-02-04 03:04:53 +01:00
|
|
|
/*
|
|
|
|
* UnGrantLock -- opposite of GrantLock.
|
|
|
|
*
|
|
|
|
* Updates the lock and proclock data structures to show that the lock
|
|
|
|
* is no longer held nor requested by the current holder.
|
|
|
|
*
|
|
|
|
* Returns true if there were any waiters waiting on the lock that
|
|
|
|
* should now be woken up with ProcLockWakeup.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
UnGrantLock(LOCK *lock, LOCKMODE lockmode,
|
|
|
|
PROCLOCK *proclock, LockMethod lockMethodTable)
|
|
|
|
{
|
|
|
|
bool wakeupNeeded = false;
|
|
|
|
|
|
|
|
Assert((lock->nRequested > 0) && (lock->requested[lockmode] > 0));
|
|
|
|
Assert((lock->nGranted > 0) && (lock->granted[lockmode] > 0));
|
|
|
|
Assert(lock->nGranted <= lock->nRequested);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fix the general lock stats
|
|
|
|
*/
|
|
|
|
lock->nRequested--;
|
|
|
|
lock->requested[lockmode]--;
|
|
|
|
lock->nGranted--;
|
|
|
|
lock->granted[lockmode]--;
|
|
|
|
|
|
|
|
if (lock->granted[lockmode] == 0)
|
|
|
|
{
|
|
|
|
/* change the conflict mask. No more of this lock type. */
|
|
|
|
lock->grantMask &= LOCKBIT_OFF(lockmode);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCK_PRINT("UnGrantLock: updated", lock, lockmode);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need only run ProcLockWakeup if the released lock conflicts with
|
|
|
|
* at least one of the lock types requested by waiter(s). Otherwise
|
|
|
|
* whatever conflict made them wait must still exist. NOTE: before
|
|
|
|
* MVCC, we could skip wakeup if lock->granted[lockmode] was still
|
|
|
|
* positive. But that's not true anymore, because the remaining
|
|
|
|
* granted locks might belong to some waiter, who could now be
|
|
|
|
* awakened because he doesn't conflict with his own locks.
|
|
|
|
*/
|
|
|
|
if (lockMethodTable->conflictTab[lockmode] & lock->waitMask)
|
|
|
|
wakeupNeeded = true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now fix the per-proclock state.
|
|
|
|
*/
|
|
|
|
proclock->holdMask &= LOCKBIT_OFF(lockmode);
|
|
|
|
PROCLOCK_PRINT("UnGrantLock: updated", proclock);
|
|
|
|
|
|
|
|
return wakeupNeeded;
|
|
|
|
}
|
|
|
|
|
2005-05-20 01:30:18 +02:00
|
|
|
/*
|
|
|
|
* CleanUpLock -- clean up after releasing a lock. We garbage-collect the
|
|
|
|
* proclock and lock objects if possible, and call ProcLockWakeup if there
|
|
|
|
* are remaining requests and the caller says it's OK. (Normally, this
|
|
|
|
* should be called after UnGrantLock, and wakeupNeeded is the result from
|
|
|
|
* UnGrantLock.)
|
|
|
|
*
|
|
|
|
* The locktable's masterLock must be held at entry, and will be
|
|
|
|
* held at exit.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
CleanUpLock(LOCKMETHODID lockmethodid, LOCK *lock, PROCLOCK *proclock,
|
|
|
|
bool wakeupNeeded)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If this was my last hold on this lock, delete my entry in the
|
|
|
|
* proclock table.
|
|
|
|
*/
|
|
|
|
if (proclock->holdMask == 0)
|
|
|
|
{
|
|
|
|
PROCLOCK_PRINT("CleanUpLock: deleting", proclock);
|
|
|
|
SHMQueueDelete(&proclock->lockLink);
|
|
|
|
SHMQueueDelete(&proclock->procLink);
|
|
|
|
if (!hash_search(LockMethodProcLockHash[lockmethodid],
|
|
|
|
(void *) &(proclock->tag),
|
|
|
|
HASH_REMOVE, NULL))
|
|
|
|
elog(PANIC, "proclock table corrupted");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lock->nRequested == 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The caller just released the last lock, so garbage-collect the
|
|
|
|
* lock object.
|
|
|
|
*/
|
|
|
|
LOCK_PRINT("CleanUpLock: deleting", lock, 0);
|
|
|
|
Assert(SHMQueueEmpty(&(lock->procLocks)));
|
|
|
|
if (!hash_search(LockMethodLockHash[lockmethodid],
|
|
|
|
(void *) &(lock->tag),
|
|
|
|
HASH_REMOVE, NULL))
|
|
|
|
elog(PANIC, "lock table corrupted");
|
|
|
|
}
|
|
|
|
else if (wakeupNeeded)
|
|
|
|
{
|
|
|
|
/* There are waiters on this lock, so wake them up. */
|
|
|
|
ProcLockWakeup(LockMethods[lockmethodid], lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
/*
|
|
|
|
* GrantLockLocal -- update the locallock data structures to show
|
|
|
|
* the lock request has been granted.
|
|
|
|
*
|
|
|
|
* We expect that LockAcquire made sure there is room to add a new
|
|
|
|
* ResourceOwner entry.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
GrantLockLocal(LOCALLOCK *locallock, ResourceOwner owner)
|
|
|
|
{
|
|
|
|
LOCALLOCKOWNER *lockOwners = locallock->lockOwners;
|
2004-08-29 07:07:03 +02:00
|
|
|
int i;
|
2004-08-27 19:07:42 +02:00
|
|
|
|
|
|
|
Assert(locallock->numLockOwners < locallock->maxLockOwners);
|
|
|
|
/* Count the total */
|
|
|
|
locallock->nLocks++;
|
|
|
|
/* Count the per-owner lock */
|
|
|
|
for (i = 0; i < locallock->numLockOwners; i++)
|
|
|
|
{
|
|
|
|
if (lockOwners[i].owner == owner)
|
|
|
|
{
|
|
|
|
lockOwners[i].nLocks++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lockOwners[i].owner = owner;
|
|
|
|
lockOwners[i].nLocks = 1;
|
|
|
|
locallock->numLockOwners++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GrantAwaitedLock -- call GrantLockLocal for the lock we are doing
|
|
|
|
* WaitOnLock on.
|
|
|
|
*
|
|
|
|
* proc.c needs this for the case where we are booted off the lock by
|
|
|
|
* timeout, but discover that someone granted us the lock anyway.
|
|
|
|
*
|
|
|
|
* We could just export GrantLockLocal, but that would require including
|
|
|
|
* resowner.h in lock.h, which creates circularity.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
GrantAwaitedLock(void)
|
|
|
|
{
|
|
|
|
GrantLockLocal(awaitedLock, awaitedOwner);
|
1998-08-25 23:20:32 +02:00
|
|
|
}
|
|
|
|
|
2000-12-22 01:51:54 +01:00
|
|
|
/*
|
|
|
|
* WaitOnLock -- wait to acquire a lock
|
|
|
|
*
|
2001-01-25 04:31:16 +01:00
|
|
|
* Caller must have set MyProc->heldLocks to reflect locks already held
|
2005-06-15 00:15:33 +02:00
|
|
|
* on the lockable object by this process.
|
2001-01-25 04:31:16 +01:00
|
|
|
*
|
2001-09-29 06:02:27 +02:00
|
|
|
* The locktable's masterLock must be held at entry.
|
2000-12-22 01:51:54 +01:00
|
|
|
*/
|
2005-05-30 00:45:02 +02:00
|
|
|
static void
|
2004-08-27 19:07:42 +02:00
|
|
|
WaitOnLock(LOCKMETHODID lockmethodid, LOCALLOCK *locallock,
|
|
|
|
ResourceOwner owner)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
LockMethod lockMethodTable = LockMethods[lockmethodid];
|
2000-12-22 01:51:54 +01:00
|
|
|
char *new_status,
|
|
|
|
*old_status;
|
2005-03-11 04:52:06 +01:00
|
|
|
size_t len;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
Assert(lockmethodid < NumLockMethods);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
LOCK_PRINT("WaitOnLock: sleeping on lock",
|
|
|
|
locallock->lock, locallock->tag.mode);
|
2000-06-04 03:44:38 +02:00
|
|
|
|
|
|
|
old_status = pstrdup(get_ps_display());
|
2005-03-11 04:52:06 +01:00
|
|
|
len = strlen(old_status);
|
|
|
|
new_status = (char *) palloc(len + 8 + 1);
|
|
|
|
memcpy(new_status, old_status, len);
|
|
|
|
strcpy(new_status + len, " waiting");
|
2000-06-04 03:44:38 +02:00
|
|
|
set_ps_display(new_status);
|
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
awaitedLock = locallock;
|
|
|
|
awaitedOwner = owner;
|
|
|
|
|
2001-02-23 00:02:33 +01:00
|
|
|
/*
|
2001-03-18 21:13:13 +01:00
|
|
|
* NOTE: Think not to put any shared-state cleanup after the call to
|
2001-01-14 06:08:17 +01:00
|
|
|
* ProcSleep, in either the normal or failure path. The lock state
|
2002-10-31 22:34:17 +01:00
|
|
|
* must be fully set by the lock grantor, or by CheckDeadLock if we
|
2001-01-14 06:08:17 +01:00
|
|
|
* give up waiting for the lock. This is necessary because of the
|
|
|
|
* possibility that a cancel/die interrupt will interrupt ProcSleep
|
|
|
|
* after someone else grants us the lock, but before we've noticed it.
|
|
|
|
* Hence, after granting, the locktable state must fully reflect the
|
|
|
|
* fact that we own the lock; we can't do additional work on return.
|
2001-03-18 21:13:13 +01:00
|
|
|
* Contrariwise, if we fail, any cleanup must happen in xact abort
|
2001-03-22 05:01:46 +01:00
|
|
|
* processing, not here, to ensure it will also happen in the
|
|
|
|
* cancel/die case.
|
2001-01-14 06:08:17 +01:00
|
|
|
*/
|
|
|
|
|
2001-01-25 04:31:16 +01:00
|
|
|
if (ProcSleep(lockMethodTable,
|
2004-08-27 19:07:42 +02:00
|
|
|
locallock->tag.mode,
|
|
|
|
locallock->lock,
|
|
|
|
locallock->proclock) != STATUS_OK)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-02-23 00:02:33 +01:00
|
|
|
/*
|
2002-10-31 22:34:17 +01:00
|
|
|
* We failed as a result of a deadlock, see CheckDeadLock(). Quit
|
2005-03-01 22:14:59 +01:00
|
|
|
* now.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2004-08-27 19:07:42 +02:00
|
|
|
awaitedLock = NULL;
|
|
|
|
LOCK_PRINT("WaitOnLock: aborting on lock",
|
|
|
|
locallock->lock, locallock->tag.mode);
|
2002-07-19 01:06:20 +02:00
|
|
|
LWLockRelease(lockMethodTable->masterLock);
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2003-01-16 22:01:45 +01:00
|
|
|
/*
|
2003-08-04 02:43:34 +02:00
|
|
|
* Now that we aren't holding the LockMgrLock, we can give an
|
|
|
|
* error report including details about the detected deadlock.
|
2003-01-16 22:01:45 +01:00
|
|
|
*/
|
|
|
|
DeadLockReport();
|
1998-08-25 23:20:32 +02:00
|
|
|
/* not reached */
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
awaitedLock = NULL;
|
|
|
|
|
2000-06-04 03:44:38 +02:00
|
|
|
set_ps_display(old_status);
|
|
|
|
pfree(old_status);
|
|
|
|
pfree(new_status);
|
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
LOCK_PRINT("WaitOnLock: wakeup on lock",
|
|
|
|
locallock->lock, locallock->tag.mode);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2001-06-22 02:04:59 +02:00
|
|
|
/*
|
2001-01-25 04:31:16 +01:00
|
|
|
* Remove a proc from the wait-queue it is on
|
|
|
|
* (caller must know it is on one).
|
|
|
|
*
|
|
|
|
* Locktable lock must be held by caller.
|
|
|
|
*
|
2005-03-01 22:14:59 +01:00
|
|
|
* NB: this does not clean up any locallock object that may exist for the lock.
|
2001-01-25 04:31:16 +01:00
|
|
|
*/
|
|
|
|
void
|
2002-06-11 15:40:53 +02:00
|
|
|
RemoveFromWaitQueue(PGPROC *proc)
|
2001-01-25 04:31:16 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
LOCK *waitLock = proc->waitLock;
|
2005-03-01 22:14:59 +01:00
|
|
|
PROCLOCK *proclock = proc->waitProcLock;
|
2001-03-22 05:01:46 +01:00
|
|
|
LOCKMODE lockmode = proc->waitLockMode;
|
2005-03-01 22:14:59 +01:00
|
|
|
LOCKMETHODID lockmethodid = LOCK_LOCKMETHOD(*waitLock);
|
2001-01-25 04:31:16 +01:00
|
|
|
|
|
|
|
/* Make sure proc is waiting */
|
|
|
|
Assert(proc->links.next != INVALID_OFFSET);
|
|
|
|
Assert(waitLock);
|
|
|
|
Assert(waitLock->waitProcs.size > 0);
|
2005-03-01 22:14:59 +01:00
|
|
|
Assert(0 < lockmethodid && lockmethodid < NumLockMethods);
|
2001-01-25 04:31:16 +01:00
|
|
|
|
|
|
|
/* Remove proc from lock's wait queue */
|
|
|
|
SHMQueueDelete(&(proc->links));
|
|
|
|
waitLock->waitProcs.size--;
|
|
|
|
|
|
|
|
/* Undo increments of request counts by waiting process */
|
|
|
|
Assert(waitLock->nRequested > 0);
|
|
|
|
Assert(waitLock->nRequested > proc->waitLock->nGranted);
|
|
|
|
waitLock->nRequested--;
|
|
|
|
Assert(waitLock->requested[lockmode] > 0);
|
|
|
|
waitLock->requested[lockmode]--;
|
|
|
|
/* don't forget to clear waitMask bit if appropriate */
|
|
|
|
if (waitLock->granted[lockmode] == waitLock->requested[lockmode])
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
waitLock->waitMask &= LOCKBIT_OFF(lockmode);
|
2001-01-25 04:31:16 +01:00
|
|
|
|
|
|
|
/* Clean up the proc's own state */
|
|
|
|
proc->waitLock = NULL;
|
2004-08-27 19:07:42 +02:00
|
|
|
proc->waitProcLock = NULL;
|
2001-01-25 04:31:16 +01:00
|
|
|
|
2005-03-01 22:14:59 +01:00
|
|
|
/*
|
|
|
|
* Delete the proclock immediately if it represents no already-held locks.
|
2005-05-20 01:30:18 +02:00
|
|
|
* (This must happen now because if the owner of the lock decides to
|
|
|
|
* release it, and the requested/granted counts then go to zero,
|
|
|
|
* LockRelease expects there to be no remaining proclocks.)
|
|
|
|
* Then see if any other waiters for the lock can be woken up now.
|
2005-03-01 22:14:59 +01:00
|
|
|
*/
|
2005-05-20 01:30:18 +02:00
|
|
|
CleanUpLock(lockmethodid, waitLock, proclock, true);
|
2001-01-25 04:31:16 +01:00
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
* LockRelease -- look up 'locktag' in lock table 'lockmethodid' and
|
2005-06-15 00:15:33 +02:00
|
|
|
* release one 'lockmode' lock on it. Release a session lock if
|
|
|
|
* 'sessionLock' is true, else release a regular transaction lock.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2001-01-25 04:31:16 +01:00
|
|
|
* Side Effects: find any waiting processes that are now wakable,
|
|
|
|
* grant them their requested locks and awaken them.
|
|
|
|
* (We have to grant the lock here to avoid a race between
|
|
|
|
* the waking process and any new process to
|
2000-12-22 01:51:54 +01:00
|
|
|
* come along and request the lock.)
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
bool
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
LockRelease(LOCKMETHODID lockmethodid, LOCKTAG *locktag,
|
2005-06-15 00:15:33 +02:00
|
|
|
LOCKMODE lockmode, bool sessionLock)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-08-27 19:07:42 +02:00
|
|
|
LOCALLOCKTAG localtag;
|
|
|
|
LOCALLOCK *locallock;
|
2000-12-22 01:51:54 +01:00
|
|
|
LOCK *lock;
|
2004-08-27 19:07:42 +02:00
|
|
|
PROCLOCK *proclock;
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockId masterLock;
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
LockMethod lockMethodTable;
|
2005-03-01 22:14:59 +01:00
|
|
|
bool wakeupNeeded;
|
1996-10-11 05:22:59 +02:00
|
|
|
|
2000-05-31 02:28:42 +02:00
|
|
|
#ifdef LOCK_DEBUG
|
2005-04-30 00:28:24 +02:00
|
|
|
if (Trace_userlocks && lockmethodid == USER_LOCKMETHOD)
|
|
|
|
elog(LOG, "LockRelease: user lock [%u,%u] %s",
|
|
|
|
locktag->locktag_field1, locktag->locktag_field2,
|
|
|
|
lock_mode_names[lockmode]);
|
1996-10-11 05:22:59 +02:00
|
|
|
#endif
|
|
|
|
|
2005-04-30 00:28:24 +02:00
|
|
|
/* ugly */
|
|
|
|
locktag->locktag_lockmethodid = lockmethodid;
|
1998-08-25 23:20:32 +02:00
|
|
|
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
Assert(lockmethodid < NumLockMethods);
|
|
|
|
lockMethodTable = LockMethods[lockmethodid];
|
1998-06-30 04:33:34 +02:00
|
|
|
if (!lockMethodTable)
|
2005-05-30 00:45:02 +02:00
|
|
|
elog(ERROR, "unrecognized lock method: %d", lockmethodid);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
2004-08-27 19:07:42 +02:00
|
|
|
* Find the LOCALLOCK entry for this lock and lockmode
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2004-08-29 07:07:03 +02:00
|
|
|
MemSet(&localtag, 0, sizeof(localtag)); /* must clear padding */
|
2004-08-27 19:07:42 +02:00
|
|
|
localtag.lock = *locktag;
|
|
|
|
localtag.mode = lockmode;
|
|
|
|
|
|
|
|
locallock = (LOCALLOCK *) hash_search(LockMethodLocalHash[lockmethodid],
|
|
|
|
(void *) &localtag,
|
|
|
|
HASH_FIND, NULL);
|
1996-10-11 05:22:59 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1998-09-01 06:40:42 +02:00
|
|
|
* let the caller print its own error message, too. Do not
|
2003-07-25 00:04:15 +02:00
|
|
|
* ereport(ERROR).
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2004-08-27 19:07:42 +02:00
|
|
|
if (!locallock || locallock->nLocks <= 0)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-08-27 19:07:42 +02:00
|
|
|
elog(WARNING, "you don't own a lock of type %s",
|
|
|
|
lock_mode_names[lockmode]);
|
1998-09-01 05:29:17 +02:00
|
|
|
return FALSE;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-25 23:20:32 +02:00
|
|
|
/*
|
2004-08-27 19:07:42 +02:00
|
|
|
* Decrease the count for the resource owner.
|
1998-08-25 23:20:32 +02:00
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-08-27 19:07:42 +02:00
|
|
|
LOCALLOCKOWNER *lockOwners = locallock->lockOwners;
|
|
|
|
ResourceOwner owner;
|
2004-08-29 07:07:03 +02:00
|
|
|
int i;
|
2004-08-27 19:07:42 +02:00
|
|
|
|
|
|
|
/* Session locks and user locks are not transactional */
|
2005-06-15 00:15:33 +02:00
|
|
|
if (!sessionLock && lockmethodid == DEFAULT_LOCKMETHOD)
|
2004-08-27 19:07:42 +02:00
|
|
|
owner = CurrentResourceOwner;
|
1998-09-01 06:40:42 +02:00
|
|
|
else
|
2004-08-27 19:07:42 +02:00
|
|
|
owner = NULL;
|
|
|
|
|
|
|
|
for (i = locallock->numLockOwners - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (lockOwners[i].owner == owner)
|
|
|
|
{
|
|
|
|
Assert(lockOwners[i].nLocks > 0);
|
|
|
|
if (--lockOwners[i].nLocks == 0)
|
|
|
|
{
|
|
|
|
/* compact out unused slot */
|
|
|
|
locallock->numLockOwners--;
|
|
|
|
if (i < locallock->numLockOwners)
|
|
|
|
lockOwners[i] = lockOwners[locallock->numLockOwners];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i < 0)
|
|
|
|
{
|
|
|
|
/* don't release a lock belonging to another owner */
|
|
|
|
elog(WARNING, "you don't own a lock of type %s",
|
|
|
|
lock_mode_names[lockmode]);
|
|
|
|
return FALSE;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2004-08-27 19:07:42 +02:00
|
|
|
|
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* Decrease the total local count. If we're still holding the lock,
|
2004-08-27 19:07:42 +02:00
|
|
|
* we're done.
|
|
|
|
*/
|
|
|
|
locallock->nLocks--;
|
|
|
|
|
|
|
|
if (locallock->nLocks > 0)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Otherwise we've got to mess with the shared lock table.
|
|
|
|
*/
|
|
|
|
masterLock = lockMethodTable->masterLock;
|
|
|
|
|
|
|
|
LWLockAcquire(masterLock, LW_EXCLUSIVE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't need to re-find the lock or proclock, since we kept their
|
|
|
|
* addresses in the locallock table, and they couldn't have been
|
|
|
|
* removed while we were holding a lock on them.
|
|
|
|
*/
|
|
|
|
lock = locallock->lock;
|
|
|
|
LOCK_PRINT("LockRelease: found", lock, lockmode);
|
|
|
|
proclock = locallock->proclock;
|
2003-02-18 03:13:24 +01:00
|
|
|
PROCLOCK_PRINT("LockRelease: found", proclock);
|
1998-08-25 23:20:32 +02:00
|
|
|
|
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* Double-check that we are actually holding a lock of the type we
|
|
|
|
* want to release.
|
1998-08-25 23:20:32 +02:00
|
|
|
*/
|
2004-08-27 19:07:42 +02:00
|
|
|
if (!(proclock->holdMask & LOCKBIT_ON(lockmode)))
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
2003-02-18 03:13:24 +01:00
|
|
|
PROCLOCK_PRINT("LockRelease: WRONGTYPE", proclock);
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockRelease(masterLock);
|
2003-07-25 00:04:15 +02:00
|
|
|
elog(WARNING, "you don't own a lock of type %s",
|
2001-02-23 00:20:06 +01:00
|
|
|
lock_mode_names[lockmode]);
|
2004-08-27 19:07:42 +02:00
|
|
|
RemoveLocalLock(locallock);
|
1998-09-01 05:29:17 +02:00
|
|
|
return FALSE;
|
1998-08-25 23:20:32 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2005-05-20 01:30:18 +02:00
|
|
|
* Do the releasing. CleanUpLock will waken any now-wakable waiters.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2005-05-20 01:30:18 +02:00
|
|
|
wakeupNeeded = UnGrantLock(lock, lockmode, proclock, lockMethodTable);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-20 01:30:18 +02:00
|
|
|
CleanUpLock(lockmethodid, lock, proclock, wakeupNeeded);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockRelease(masterLock);
|
2004-08-27 19:07:42 +02:00
|
|
|
|
|
|
|
RemoveLocalLock(locallock);
|
1998-09-01 05:29:17 +02:00
|
|
|
return TRUE;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1996-10-11 05:22:59 +02:00
|
|
|
/*
|
2004-07-01 02:52:04 +02:00
|
|
|
* LockReleaseAll -- Release all locks of the specified lock method that
|
2004-08-27 19:07:42 +02:00
|
|
|
* are held by the current process.
|
2000-12-22 01:51:54 +01:00
|
|
|
*
|
2004-07-01 02:52:04 +02:00
|
|
|
* Well, not necessarily *all* locks. The available behaviors are:
|
2005-06-15 00:15:33 +02:00
|
|
|
* allLocks == true: release all locks including session locks.
|
|
|
|
* allLocks == false: release all non-session locks.
|
1996-10-11 05:22:59 +02:00
|
|
|
*/
|
2005-05-20 01:30:18 +02:00
|
|
|
void
|
2005-06-15 00:15:33 +02:00
|
|
|
LockReleaseAll(LOCKMETHODID lockmethodid, bool allLocks)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-08-27 19:07:42 +02:00
|
|
|
HASH_SEQ_STATUS status;
|
|
|
|
SHM_QUEUE *procLocks = &(MyProc->procLocks);
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockId masterLock;
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
LockMethod lockMethodTable;
|
1997-09-08 04:41:22 +02:00
|
|
|
int i,
|
1998-06-30 04:33:34 +02:00
|
|
|
numLockModes;
|
2004-08-29 07:07:03 +02:00
|
|
|
LOCALLOCK *locallock;
|
2004-08-27 19:07:42 +02:00
|
|
|
PROCLOCK *proclock;
|
1997-09-08 04:41:22 +02:00
|
|
|
LOCK *lock;
|
1996-10-11 05:22:59 +02:00
|
|
|
|
2000-05-31 02:28:42 +02:00
|
|
|
#ifdef LOCK_DEBUG
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
if (lockmethodid == USER_LOCKMETHOD ? Trace_userlocks : Trace_locks)
|
2004-08-27 19:07:42 +02:00
|
|
|
elog(LOG, "LockReleaseAll: lockmethod=%d", lockmethodid);
|
1996-10-11 05:22:59 +02:00
|
|
|
#endif
|
|
|
|
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
Assert(lockmethodid < NumLockMethods);
|
|
|
|
lockMethodTable = LockMethods[lockmethodid];
|
1998-09-01 06:40:42 +02:00
|
|
|
if (!lockMethodTable)
|
2005-05-30 00:45:02 +02:00
|
|
|
elog(ERROR, "unrecognized lock method: %d", lockmethodid);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-07-19 01:06:20 +02:00
|
|
|
numLockModes = lockMethodTable->numLockModes;
|
|
|
|
masterLock = lockMethodTable->masterLock;
|
1998-08-25 23:20:32 +02:00
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
/*
|
|
|
|
* First we run through the locallock table and get rid of unwanted
|
|
|
|
* entries, then we scan the process's proclocks and get rid of those.
|
2004-08-29 07:07:03 +02:00
|
|
|
* We do this separately because we may have multiple locallock
|
|
|
|
* entries pointing to the same proclock, and we daren't end up with
|
|
|
|
* any dangling pointers.
|
2004-08-27 19:07:42 +02:00
|
|
|
*/
|
|
|
|
hash_seq_init(&status, LockMethodLocalHash[lockmethodid]);
|
|
|
|
|
|
|
|
while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
|
|
|
|
{
|
|
|
|
if (locallock->proclock == NULL || locallock->lock == NULL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We must've run out of shared memory while trying to set up
|
|
|
|
* this lock. Just forget the local entry.
|
|
|
|
*/
|
|
|
|
Assert(locallock->nLocks == 0);
|
|
|
|
RemoveLocalLock(locallock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ignore items that are not of the lockmethod to be removed */
|
|
|
|
if (LOCALLOCK_LOCKMETHOD(*locallock) != lockmethodid)
|
|
|
|
continue;
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
/*
|
2005-06-15 00:15:33 +02:00
|
|
|
* If we are asked to release all locks, we can just zap the
|
|
|
|
* entry. Otherwise, must scan to see if there are session locks.
|
|
|
|
* We assume there is at most one lockOwners entry for session locks.
|
2004-08-29 07:07:03 +02:00
|
|
|
*/
|
2005-06-15 00:15:33 +02:00
|
|
|
if (!allLocks)
|
|
|
|
{
|
|
|
|
LOCALLOCKOWNER *lockOwners = locallock->lockOwners;
|
2004-08-27 19:07:42 +02:00
|
|
|
|
2005-06-15 00:15:33 +02:00
|
|
|
/* If it's above array position 0, move it down to 0 */
|
|
|
|
for (i = locallock->numLockOwners - 1; i > 0; i--)
|
|
|
|
{
|
|
|
|
if (lockOwners[i].owner == NULL)
|
|
|
|
{
|
|
|
|
lockOwners[0] = lockOwners[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (locallock->numLockOwners > 0 &&
|
|
|
|
lockOwners[0].owner == NULL &&
|
|
|
|
lockOwners[0].nLocks > 0)
|
|
|
|
{
|
|
|
|
/* Fix the locallock to show just the session locks */
|
|
|
|
locallock->nLocks = lockOwners[0].nLocks;
|
|
|
|
locallock->numLockOwners = 1;
|
|
|
|
/* We aren't deleting this locallock, so done */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mark the proclock to show we need to release this lockmode */
|
|
|
|
if (locallock->nLocks > 0)
|
|
|
|
locallock->proclock->releaseMask |= LOCKBIT_ON(locallock->tag.mode);
|
|
|
|
|
|
|
|
/* And remove the locallock hashtable entry */
|
2004-08-27 19:07:42 +02:00
|
|
|
RemoveLocalLock(locallock);
|
|
|
|
}
|
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockAcquire(masterLock, LW_EXCLUSIVE);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
proclock = (PROCLOCK *) SHMQueueNext(procLocks, procLocks,
|
2003-08-04 02:43:34 +02:00
|
|
|
offsetof(PROCLOCK, procLink));
|
2000-12-22 01:51:54 +01:00
|
|
|
|
2003-02-18 03:13:24 +01:00
|
|
|
while (proclock)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
bool wakeupNeeded = false;
|
2005-06-18 00:32:51 +02:00
|
|
|
PROCLOCK *nextplock;
|
1998-09-01 06:40:42 +02:00
|
|
|
|
2003-02-18 03:13:24 +01:00
|
|
|
/* Get link first, since we may unlink/delete this proclock */
|
2005-06-18 00:32:51 +02:00
|
|
|
nextplock = (PROCLOCK *) SHMQueueNext(procLocks, &proclock->procLink,
|
2002-09-04 22:31:48 +02:00
|
|
|
offsetof(PROCLOCK, procLink));
|
2000-12-22 01:51:54 +01:00
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
Assert(proclock->tag.proc == MAKE_OFFSET(MyProc));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-02-18 03:13:24 +01:00
|
|
|
lock = (LOCK *) MAKE_PTR(proclock->tag.lock);
|
2000-12-22 01:51:54 +01:00
|
|
|
|
|
|
|
/* Ignore items that are not of the lockmethod to be removed */
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
if (LOCK_LOCKMETHOD(*lock) != lockmethodid)
|
2000-12-22 01:51:54 +01:00
|
|
|
goto next_item;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
/*
|
2005-06-15 00:15:33 +02:00
|
|
|
* In allLocks mode, force release of all locks even if locallock
|
|
|
|
* table had problems
|
2004-08-29 07:07:03 +02:00
|
|
|
*/
|
2005-06-15 00:15:33 +02:00
|
|
|
if (allLocks)
|
|
|
|
proclock->releaseMask = proclock->holdMask;
|
|
|
|
else
|
|
|
|
Assert((proclock->releaseMask & ~proclock->holdMask) == 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ignore items that have nothing to be released, unless they have
|
|
|
|
* holdMask == 0 and are therefore recyclable
|
|
|
|
*/
|
|
|
|
if (proclock->releaseMask == 0 && proclock->holdMask != 0)
|
1998-08-25 23:20:32 +02:00
|
|
|
goto next_item;
|
|
|
|
|
2003-02-18 03:13:24 +01:00
|
|
|
PROCLOCK_PRINT("LockReleaseAll", proclock);
|
2000-12-22 01:51:54 +01:00
|
|
|
LOCK_PRINT("LockReleaseAll", lock, 0);
|
2001-01-16 07:11:34 +01:00
|
|
|
Assert(lock->nRequested >= 0);
|
|
|
|
Assert(lock->nGranted >= 0);
|
|
|
|
Assert(lock->nGranted <= lock->nRequested);
|
2004-08-27 19:07:42 +02:00
|
|
|
Assert((proclock->holdMask & ~lock->grantMask) == 0);
|
1996-10-11 05:22:59 +02:00
|
|
|
|
2001-02-23 00:02:33 +01:00
|
|
|
/*
|
2005-06-15 00:15:33 +02:00
|
|
|
* Release the previously-marked lock modes
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2005-06-15 00:15:33 +02:00
|
|
|
for (i = 1; i <= numLockModes; i++)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2005-06-15 00:15:33 +02:00
|
|
|
if (proclock->releaseMask & LOCKBIT_ON(i))
|
|
|
|
wakeupNeeded |= UnGrantLock(lock, i, proclock,
|
|
|
|
lockMethodTable);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2004-08-27 19:07:42 +02:00
|
|
|
Assert((lock->nRequested >= 0) && (lock->nGranted >= 0));
|
|
|
|
Assert(lock->nGranted <= lock->nRequested);
|
1998-08-25 23:20:32 +02:00
|
|
|
LOCK_PRINT("LockReleaseAll: updated", lock, 0);
|
|
|
|
|
2005-06-15 00:15:33 +02:00
|
|
|
proclock->releaseMask = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-05-20 01:30:18 +02:00
|
|
|
/* CleanUpLock will wake up waiters if needed. */
|
|
|
|
CleanUpLock(lockmethodid, lock, proclock, wakeupNeeded);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
next_item:
|
2005-06-18 00:32:51 +02:00
|
|
|
proclock = nextplock;
|
1998-08-25 23:20:32 +02:00
|
|
|
}
|
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockRelease(masterLock);
|
2001-01-16 07:11:34 +01:00
|
|
|
|
2000-05-31 02:28:42 +02:00
|
|
|
#ifdef LOCK_DEBUG
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
if (lockmethodid == USER_LOCKMETHOD ? Trace_userlocks : Trace_locks)
|
2003-07-25 00:04:15 +02:00
|
|
|
elog(LOG, "LockReleaseAll done");
|
2000-05-31 02:28:42 +02:00
|
|
|
#endif
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
/*
|
|
|
|
* LockReleaseCurrentOwner
|
|
|
|
* Release all locks belonging to CurrentResourceOwner
|
|
|
|
*
|
|
|
|
* Only DEFAULT_LOCKMETHOD locks can belong to a resource owner.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
LockReleaseCurrentOwner(void)
|
|
|
|
{
|
|
|
|
HASH_SEQ_STATUS status;
|
2004-08-29 07:07:03 +02:00
|
|
|
LOCALLOCK *locallock;
|
2004-08-27 19:07:42 +02:00
|
|
|
LOCALLOCKOWNER *lockOwners;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
hash_seq_init(&status, LockMethodLocalHash[DEFAULT_LOCKMETHOD]);
|
|
|
|
|
|
|
|
while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
|
|
|
|
{
|
|
|
|
/* Ignore items that must be nontransactional */
|
|
|
|
if (LOCALLOCK_LOCKMETHOD(*locallock) != DEFAULT_LOCKMETHOD)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Scan to see if there are any locks belonging to current owner */
|
|
|
|
lockOwners = locallock->lockOwners;
|
|
|
|
for (i = locallock->numLockOwners - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (lockOwners[i].owner == CurrentResourceOwner)
|
|
|
|
{
|
|
|
|
Assert(lockOwners[i].nLocks > 0);
|
|
|
|
if (lockOwners[i].nLocks < locallock->nLocks)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We will still hold this lock after forgetting this
|
|
|
|
* ResourceOwner.
|
|
|
|
*/
|
|
|
|
locallock->nLocks -= lockOwners[i].nLocks;
|
|
|
|
/* compact out unused slot */
|
|
|
|
locallock->numLockOwners--;
|
|
|
|
if (i < locallock->numLockOwners)
|
|
|
|
lockOwners[i] = lockOwners[locallock->numLockOwners];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Assert(lockOwners[i].nLocks == locallock->nLocks);
|
|
|
|
/* We want to call LockRelease just once */
|
|
|
|
lockOwners[i].nLocks = 1;
|
|
|
|
locallock->nLocks = 1;
|
|
|
|
if (!LockRelease(DEFAULT_LOCKMETHOD,
|
|
|
|
&locallock->tag.lock,
|
2005-06-15 00:15:33 +02:00
|
|
|
locallock->tag.mode,
|
|
|
|
false))
|
2004-08-27 19:07:42 +02:00
|
|
|
elog(WARNING, "LockReleaseCurrentOwner: failed??");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* LockReassignCurrentOwner
|
|
|
|
* Reassign all locks belonging to CurrentResourceOwner to belong
|
|
|
|
* to its parent resource owner
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
LockReassignCurrentOwner(void)
|
|
|
|
{
|
|
|
|
ResourceOwner parent = ResourceOwnerGetParent(CurrentResourceOwner);
|
|
|
|
HASH_SEQ_STATUS status;
|
2004-08-29 07:07:03 +02:00
|
|
|
LOCALLOCK *locallock;
|
2004-08-27 19:07:42 +02:00
|
|
|
LOCALLOCKOWNER *lockOwners;
|
|
|
|
|
|
|
|
Assert(parent != NULL);
|
|
|
|
|
|
|
|
hash_seq_init(&status, LockMethodLocalHash[DEFAULT_LOCKMETHOD]);
|
|
|
|
|
|
|
|
while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int ic = -1;
|
|
|
|
int ip = -1;
|
|
|
|
|
|
|
|
/* Ignore items that must be nontransactional */
|
|
|
|
if (LOCALLOCK_LOCKMETHOD(*locallock) != DEFAULT_LOCKMETHOD)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scan to see if there are any locks belonging to current owner
|
|
|
|
* or its parent
|
|
|
|
*/
|
|
|
|
lockOwners = locallock->lockOwners;
|
|
|
|
for (i = locallock->numLockOwners - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (lockOwners[i].owner == CurrentResourceOwner)
|
|
|
|
ic = i;
|
|
|
|
else if (lockOwners[i].owner == parent)
|
|
|
|
ip = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ic < 0)
|
|
|
|
continue; /* no current locks */
|
|
|
|
|
|
|
|
if (ip < 0)
|
|
|
|
{
|
|
|
|
/* Parent has no slot, so just give it child's slot */
|
|
|
|
lockOwners[ic].owner = parent;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Merge child's count with parent's */
|
|
|
|
lockOwners[ip].nLocks += lockOwners[ic].nLocks;
|
|
|
|
/* compact out unused slot */
|
|
|
|
locallock->numLockOwners--;
|
|
|
|
if (ic < locallock->numLockOwners)
|
|
|
|
lockOwners[ic] = lockOwners[locallock->numLockOwners];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-18 00:32:51 +02:00
|
|
|
/*
|
|
|
|
* AtPrepare_Locks
|
|
|
|
* Do the preparatory work for a PREPARE: make 2PC state file records
|
|
|
|
* for all locks currently held.
|
|
|
|
*
|
|
|
|
* User locks are non-transactional and are therefore ignored.
|
|
|
|
*
|
|
|
|
* There are some special cases that we error out on: we can't be holding
|
|
|
|
* any session locks (should be OK since only VACUUM uses those) and we
|
|
|
|
* can't be holding any locks on temporary objects (since that would mess
|
|
|
|
* up the current backend if it tries to exit before the prepared xact is
|
|
|
|
* committed).
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
AtPrepare_Locks(void)
|
|
|
|
{
|
|
|
|
LOCKMETHODID lockmethodid = DEFAULT_LOCKMETHOD;
|
|
|
|
HASH_SEQ_STATUS status;
|
|
|
|
LOCALLOCK *locallock;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't need to touch shared memory for this --- all the necessary
|
|
|
|
* state information is in the locallock table.
|
|
|
|
*/
|
|
|
|
hash_seq_init(&status, LockMethodLocalHash[lockmethodid]);
|
|
|
|
|
|
|
|
while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
|
|
|
|
{
|
|
|
|
TwoPhaseLockRecord record;
|
|
|
|
LOCALLOCKOWNER *lockOwners = locallock->lockOwners;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Ignore items that are not of the lockmethod to be processed */
|
|
|
|
if (LOCALLOCK_LOCKMETHOD(*locallock) != lockmethodid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Ignore it if we don't actually hold the lock */
|
|
|
|
if (locallock->nLocks <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Scan to verify there are no session locks */
|
|
|
|
for (i = locallock->numLockOwners - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
/* elog not ereport since this should not happen */
|
|
|
|
if (lockOwners[i].owner == NULL)
|
|
|
|
elog(ERROR, "cannot PREPARE when session locks exist");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Can't handle it if the lock is on a temporary object */
|
|
|
|
if (locallock->isTempObject)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("cannot PREPARE a transaction that has operated on temporary tables")));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a 2PC record.
|
|
|
|
*/
|
|
|
|
memcpy(&(record.locktag), &(locallock->tag.lock), sizeof(LOCKTAG));
|
|
|
|
record.lockmode = locallock->tag.mode;
|
|
|
|
|
|
|
|
RegisterTwoPhaseRecord(TWOPHASE_RM_LOCK_ID, 0,
|
|
|
|
&record, sizeof(TwoPhaseLockRecord));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PostPrepare_Locks
|
|
|
|
* Clean up after successful PREPARE
|
|
|
|
*
|
|
|
|
* Here, we want to transfer ownership of our locks to a dummy PGPROC
|
|
|
|
* that's now associated with the prepared transaction, and we want to
|
|
|
|
* clean out the corresponding entries in the LOCALLOCK table.
|
|
|
|
*
|
|
|
|
* Note: by removing the LOCALLOCK entries, we are leaving dangling
|
|
|
|
* pointers in the transaction's resource owner. This is OK at the
|
|
|
|
* moment since resowner.c doesn't try to free locks retail at a toplevel
|
|
|
|
* transaction commit or abort. We could alternatively zero out nLocks
|
|
|
|
* and leave the LOCALLOCK entries to be garbage-collected by LockReleaseAll,
|
|
|
|
* but that probably costs more cycles.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
PostPrepare_Locks(TransactionId xid)
|
|
|
|
{
|
|
|
|
PGPROC *newproc = TwoPhaseGetDummyProc(xid);
|
|
|
|
LOCKMETHODID lockmethodid = DEFAULT_LOCKMETHOD;
|
|
|
|
HASH_SEQ_STATUS status;
|
|
|
|
SHM_QUEUE *procLocks = &(MyProc->procLocks);
|
|
|
|
LWLockId masterLock;
|
|
|
|
LockMethod lockMethodTable;
|
|
|
|
int numLockModes;
|
|
|
|
LOCALLOCK *locallock;
|
|
|
|
PROCLOCK *proclock;
|
|
|
|
PROCLOCKTAG proclocktag;
|
|
|
|
bool found;
|
|
|
|
LOCK *lock;
|
|
|
|
|
|
|
|
/* This is a critical section: any error means big trouble */
|
|
|
|
START_CRIT_SECTION();
|
|
|
|
|
|
|
|
lockMethodTable = LockMethods[lockmethodid];
|
|
|
|
if (!lockMethodTable)
|
|
|
|
elog(ERROR, "unrecognized lock method: %d", lockmethodid);
|
|
|
|
|
|
|
|
numLockModes = lockMethodTable->numLockModes;
|
|
|
|
masterLock = lockMethodTable->masterLock;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First we run through the locallock table and get rid of unwanted
|
|
|
|
* entries, then we scan the process's proclocks and transfer them
|
|
|
|
* to the target proc.
|
|
|
|
*
|
|
|
|
* We do this separately because we may have multiple locallock
|
|
|
|
* entries pointing to the same proclock, and we daren't end up with
|
|
|
|
* any dangling pointers.
|
|
|
|
*/
|
|
|
|
hash_seq_init(&status, LockMethodLocalHash[lockmethodid]);
|
|
|
|
|
|
|
|
while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
|
|
|
|
{
|
|
|
|
if (locallock->proclock == NULL || locallock->lock == NULL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We must've run out of shared memory while trying to set up
|
|
|
|
* this lock. Just forget the local entry.
|
|
|
|
*/
|
|
|
|
Assert(locallock->nLocks == 0);
|
|
|
|
RemoveLocalLock(locallock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ignore items that are not of the lockmethod to be removed */
|
|
|
|
if (LOCALLOCK_LOCKMETHOD(*locallock) != lockmethodid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* We already checked there are no session locks */
|
|
|
|
|
|
|
|
/* Mark the proclock to show we need to release this lockmode */
|
|
|
|
if (locallock->nLocks > 0)
|
|
|
|
locallock->proclock->releaseMask |= LOCKBIT_ON(locallock->tag.mode);
|
|
|
|
|
|
|
|
/* And remove the locallock hashtable entry */
|
|
|
|
RemoveLocalLock(locallock);
|
|
|
|
}
|
|
|
|
|
|
|
|
LWLockAcquire(masterLock, LW_EXCLUSIVE);
|
|
|
|
|
|
|
|
proclock = (PROCLOCK *) SHMQueueNext(procLocks, procLocks,
|
|
|
|
offsetof(PROCLOCK, procLink));
|
|
|
|
|
|
|
|
while (proclock)
|
|
|
|
{
|
|
|
|
PROCLOCK *nextplock;
|
|
|
|
LOCKMASK holdMask;
|
|
|
|
PROCLOCK *newproclock;
|
|
|
|
|
|
|
|
/* Get link first, since we may unlink/delete this proclock */
|
|
|
|
nextplock = (PROCLOCK *) SHMQueueNext(procLocks, &proclock->procLink,
|
|
|
|
offsetof(PROCLOCK, procLink));
|
|
|
|
|
|
|
|
Assert(proclock->tag.proc == MAKE_OFFSET(MyProc));
|
|
|
|
|
|
|
|
lock = (LOCK *) MAKE_PTR(proclock->tag.lock);
|
|
|
|
|
|
|
|
/* Ignore items that are not of the lockmethod to be removed */
|
|
|
|
if (LOCK_LOCKMETHOD(*lock) != lockmethodid)
|
|
|
|
goto next_item;
|
|
|
|
|
|
|
|
PROCLOCK_PRINT("PostPrepare_Locks", proclock);
|
|
|
|
LOCK_PRINT("PostPrepare_Locks", lock, 0);
|
|
|
|
Assert(lock->nRequested >= 0);
|
|
|
|
Assert(lock->nGranted >= 0);
|
|
|
|
Assert(lock->nGranted <= lock->nRequested);
|
|
|
|
Assert((proclock->holdMask & ~lock->grantMask) == 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since there were no session locks, we should be releasing all locks
|
|
|
|
*/
|
|
|
|
if (proclock->releaseMask != proclock->holdMask)
|
|
|
|
elog(PANIC, "we seem to have dropped a bit somewhere");
|
|
|
|
|
|
|
|
holdMask = proclock->holdMask;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We cannot simply modify proclock->tag.proc to reassign ownership
|
|
|
|
* of the lock, because that's part of the hash key and the proclock
|
|
|
|
* would then be in the wrong hash chain. So, unlink and delete the
|
|
|
|
* old proclock; create a new one with the right contents; and link
|
|
|
|
* it into place. We do it in this order to be certain we won't
|
|
|
|
* run out of shared memory (the way dynahash.c works, the deleted
|
|
|
|
* object is certain to be available for reallocation).
|
|
|
|
*/
|
|
|
|
SHMQueueDelete(&proclock->lockLink);
|
|
|
|
SHMQueueDelete(&proclock->procLink);
|
|
|
|
if (!hash_search(LockMethodProcLockHash[lockmethodid],
|
|
|
|
(void *) &(proclock->tag),
|
|
|
|
HASH_REMOVE, NULL))
|
|
|
|
elog(PANIC, "proclock table corrupted");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the hash key for the new proclock table.
|
|
|
|
*/
|
|
|
|
MemSet(&proclocktag, 0, sizeof(PROCLOCKTAG));
|
|
|
|
proclocktag.lock = MAKE_OFFSET(lock);
|
|
|
|
proclocktag.proc = MAKE_OFFSET(newproc);
|
|
|
|
|
|
|
|
newproclock = (PROCLOCK *) hash_search(LockMethodProcLockHash[lockmethodid],
|
|
|
|
(void *) &proclocktag,
|
|
|
|
HASH_ENTER_NULL, &found);
|
|
|
|
if (!newproclock)
|
|
|
|
ereport(PANIC, /* should not happen */
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("out of shared memory"),
|
|
|
|
errdetail("Not enough memory for reassigning the prepared transaction's locks.")));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If new, initialize the new entry
|
|
|
|
*/
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
newproclock->holdMask = 0;
|
|
|
|
newproclock->releaseMask = 0;
|
|
|
|
/* Add new proclock to appropriate lists */
|
|
|
|
SHMQueueInsertBefore(&lock->procLocks, &newproclock->lockLink);
|
|
|
|
SHMQueueInsertBefore(&newproc->procLocks, &newproclock->procLink);
|
|
|
|
PROCLOCK_PRINT("PostPrepare_Locks: new", newproclock);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PROCLOCK_PRINT("PostPrepare_Locks: found", newproclock);
|
|
|
|
Assert((newproclock->holdMask & ~lock->grantMask) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pass over the identified lock ownership.
|
|
|
|
*/
|
|
|
|
Assert((newproclock->holdMask & holdMask) == 0);
|
|
|
|
newproclock->holdMask |= holdMask;
|
|
|
|
|
|
|
|
next_item:
|
|
|
|
proclock = nextplock;
|
|
|
|
}
|
|
|
|
|
|
|
|
LWLockRelease(masterLock);
|
|
|
|
|
|
|
|
END_CRIT_SECTION();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-29 17:15:56 +02:00
|
|
|
/*
|
|
|
|
* Estimate shared-memory space used for lock tables
|
|
|
|
*/
|
2005-08-21 01:26:37 +02:00
|
|
|
Size
|
2005-06-18 00:32:51 +02:00
|
|
|
LockShmemSize(void)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2005-08-21 01:26:37 +02:00
|
|
|
Size size;
|
2005-06-18 00:32:51 +02:00
|
|
|
long max_table_size = NLOCKENTS();
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-09-29 17:15:56 +02:00
|
|
|
/* lock method headers */
|
2005-08-21 01:26:37 +02:00
|
|
|
size = MAX_LOCK_METHODS * MAXALIGN(sizeof(LockMethodData));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-02-22 07:16:57 +01:00
|
|
|
/* lockHash table */
|
2005-08-21 01:26:37 +02:00
|
|
|
size = add_size(size, hash_estimate_size(max_table_size, sizeof(LOCK)));
|
1999-02-22 07:16:57 +01:00
|
|
|
|
2003-02-18 03:13:24 +01:00
|
|
|
/* proclockHash table */
|
2005-08-21 01:26:37 +02:00
|
|
|
size = add_size(size, hash_estimate_size(max_table_size, sizeof(PROCLOCK)));
|
1999-03-06 22:17:56 +01:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
/*
|
2004-09-29 17:15:56 +02:00
|
|
|
* Note we count only one pair of hash tables, since the userlocks
|
|
|
|
* table actually overlays the main one.
|
|
|
|
*
|
1999-05-25 18:15:34 +02:00
|
|
|
* Since the lockHash entry count above is only an estimate, add 10%
|
|
|
|
* safety margin.
|
1999-03-06 22:17:56 +01:00
|
|
|
*/
|
2005-08-21 01:26:37 +02:00
|
|
|
size = add_size(size, size / 10);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
return size;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2002-08-17 15:04:19 +02:00
|
|
|
/*
|
|
|
|
* GetLockStatusData - Return a summary of the lock manager's internal
|
2002-08-31 19:14:28 +02:00
|
|
|
* status, for use in a user-level reporting function.
|
2002-08-17 15:04:19 +02:00
|
|
|
*
|
2002-08-31 19:14:28 +02:00
|
|
|
* The return data consists of an array of PROCLOCK objects, with the
|
|
|
|
* associated PGPROC and LOCK objects for each. Note that multiple
|
|
|
|
* copies of the same PGPROC and/or LOCK objects are likely to appear.
|
|
|
|
* It is the caller's responsibility to match up duplicates if wanted.
|
|
|
|
*
|
|
|
|
* The design goal is to hold the LockMgrLock for as short a time as possible;
|
|
|
|
* thus, this function simply makes a copy of the necessary data and releases
|
|
|
|
* the lock, allowing the caller to contemplate and format the data for as
|
|
|
|
* long as it pleases.
|
2002-08-17 15:04:19 +02:00
|
|
|
*/
|
2002-08-31 19:14:28 +02:00
|
|
|
LockData *
|
|
|
|
GetLockStatusData(void)
|
2002-08-17 15:04:19 +02:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
LockData *data;
|
2003-02-18 03:13:24 +01:00
|
|
|
HTAB *proclockTable;
|
|
|
|
PROCLOCK *proclock;
|
2002-08-17 15:04:19 +02:00
|
|
|
HASH_SEQ_STATUS seqstat;
|
2002-09-04 22:31:48 +02:00
|
|
|
int i;
|
2002-08-17 15:04:19 +02:00
|
|
|
|
2002-08-31 19:14:28 +02:00
|
|
|
data = (LockData *) palloc(sizeof(LockData));
|
2002-08-17 15:04:19 +02:00
|
|
|
|
|
|
|
LWLockAcquire(LockMgrLock, LW_EXCLUSIVE);
|
|
|
|
|
2003-12-20 18:31:21 +01:00
|
|
|
proclockTable = LockMethodProcLockHash[DEFAULT_LOCKMETHOD];
|
2002-08-17 15:04:19 +02:00
|
|
|
|
2003-02-18 03:13:24 +01:00
|
|
|
data->nelements = i = proclockTable->hctl->nentries;
|
2002-08-31 19:14:28 +02:00
|
|
|
|
2003-02-18 03:13:24 +01:00
|
|
|
data->proclockaddrs = (SHMEM_OFFSET *) palloc(sizeof(SHMEM_OFFSET) * i);
|
|
|
|
data->proclocks = (PROCLOCK *) palloc(sizeof(PROCLOCK) * i);
|
2002-08-31 19:14:28 +02:00
|
|
|
data->procs = (PGPROC *) palloc(sizeof(PGPROC) * i);
|
|
|
|
data->locks = (LOCK *) palloc(sizeof(LOCK) * i);
|
2002-08-17 15:04:19 +02:00
|
|
|
|
2003-02-18 03:13:24 +01:00
|
|
|
hash_seq_init(&seqstat, proclockTable);
|
2002-08-17 15:04:19 +02:00
|
|
|
|
2002-08-31 19:14:28 +02:00
|
|
|
i = 0;
|
2003-02-18 03:13:24 +01:00
|
|
|
while ((proclock = hash_seq_search(&seqstat)))
|
2002-08-17 15:04:19 +02:00
|
|
|
{
|
2003-02-18 03:13:24 +01:00
|
|
|
PGPROC *proc = (PGPROC *) MAKE_PTR(proclock->tag.proc);
|
|
|
|
LOCK *lock = (LOCK *) MAKE_PTR(proclock->tag.lock);
|
2002-08-17 15:04:19 +02:00
|
|
|
|
2003-02-18 03:13:24 +01:00
|
|
|
data->proclockaddrs[i] = MAKE_OFFSET(proclock);
|
|
|
|
memcpy(&(data->proclocks[i]), proclock, sizeof(PROCLOCK));
|
2002-08-17 15:04:19 +02:00
|
|
|
memcpy(&(data->procs[i]), proc, sizeof(PGPROC));
|
|
|
|
memcpy(&(data->locks[i]), lock, sizeof(LOCK));
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2002-08-31 19:14:28 +02:00
|
|
|
LWLockRelease(LockMgrLock);
|
|
|
|
|
2002-08-17 15:04:19 +02:00
|
|
|
Assert(i == data->nelements);
|
|
|
|
|
2002-08-31 19:14:28 +02:00
|
|
|
return data;
|
2002-08-17 15:04:19 +02:00
|
|
|
}
|
|
|
|
|
2002-08-31 19:14:28 +02:00
|
|
|
/* Provide the textual name of any lock mode */
|
|
|
|
const char *
|
2002-08-17 15:04:19 +02:00
|
|
|
GetLockmodeName(LOCKMODE mode)
|
|
|
|
{
|
|
|
|
Assert(mode <= MAX_LOCKMODES);
|
|
|
|
return lock_mode_names[mode];
|
|
|
|
}
|
1998-01-27 04:00:43 +01:00
|
|
|
|
2000-05-31 02:28:42 +02:00
|
|
|
#ifdef LOCK_DEBUG
|
1997-02-12 06:25:13 +01:00
|
|
|
/*
|
2005-06-18 00:32:51 +02:00
|
|
|
* Dump all locks in the given proc's procLocks list.
|
2001-01-22 23:30:06 +01:00
|
|
|
*
|
|
|
|
* Must have already acquired the masterLock.
|
1997-02-12 06:25:13 +01:00
|
|
|
*/
|
|
|
|
void
|
2005-06-18 00:32:51 +02:00
|
|
|
DumpLocks(PGPROC *proc)
|
1997-02-12 06:25:13 +01:00
|
|
|
{
|
2004-08-27 19:07:42 +02:00
|
|
|
SHM_QUEUE *procLocks;
|
2003-02-18 03:13:24 +01:00
|
|
|
PROCLOCK *proclock;
|
1997-09-08 04:41:22 +02:00
|
|
|
LOCK *lock;
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
int lockmethodid = DEFAULT_LOCKMETHOD;
|
|
|
|
LockMethod lockMethodTable;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-09-07 02:27:30 +02:00
|
|
|
if (proc == NULL)
|
1997-09-07 07:04:48 +02:00
|
|
|
return;
|
2001-09-07 02:27:30 +02:00
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
procLocks = &proc->procLocks;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
Assert(lockmethodid < NumLockMethods);
|
|
|
|
lockMethodTable = LockMethods[lockmethodid];
|
1998-06-30 04:33:34 +02:00
|
|
|
if (!lockMethodTable)
|
1997-09-07 07:04:48 +02:00
|
|
|
return;
|
|
|
|
|
2000-12-22 01:51:54 +01:00
|
|
|
if (proc->waitLock)
|
|
|
|
LOCK_PRINT("DumpLocks: waiting on", proc->waitLock, 0);
|
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
proclock = (PROCLOCK *) SHMQueueNext(procLocks, procLocks,
|
2003-08-04 02:43:34 +02:00
|
|
|
offsetof(PROCLOCK, procLink));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-02-18 03:13:24 +01:00
|
|
|
while (proclock)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-02-18 03:13:24 +01:00
|
|
|
Assert(proclock->tag.proc == MAKE_OFFSET(proc));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-02-18 03:13:24 +01:00
|
|
|
lock = (LOCK *) MAKE_PTR(proclock->tag.lock);
|
1998-08-25 23:20:32 +02:00
|
|
|
|
2003-02-18 03:13:24 +01:00
|
|
|
PROCLOCK_PRINT("DumpLocks", proclock);
|
2000-12-22 01:51:54 +01:00
|
|
|
LOCK_PRINT("DumpLocks", lock, 0);
|
|
|
|
|
2004-08-27 19:07:42 +02:00
|
|
|
proclock = (PROCLOCK *) SHMQueueNext(procLocks, &proclock->procLink,
|
2002-09-04 22:31:48 +02:00
|
|
|
offsetof(PROCLOCK, procLink));
|
2001-01-22 23:30:06 +01:00
|
|
|
}
|
1997-02-12 06:25:13 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-25 23:20:32 +02:00
|
|
|
/*
|
|
|
|
* Dump all postgres locks. Must have already acquired the masterLock.
|
|
|
|
*/
|
|
|
|
void
|
2000-12-22 01:51:54 +01:00
|
|
|
DumpAllLocks(void)
|
1998-08-25 23:20:32 +02:00
|
|
|
{
|
2002-06-11 15:40:53 +02:00
|
|
|
PGPROC *proc;
|
2003-02-18 03:13:24 +01:00
|
|
|
PROCLOCK *proclock;
|
1998-09-01 06:40:42 +02:00
|
|
|
LOCK *lock;
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
int lockmethodid = DEFAULT_LOCKMETHOD;
|
|
|
|
LockMethod lockMethodTable;
|
2003-02-18 03:13:24 +01:00
|
|
|
HTAB *proclockTable;
|
2001-01-02 05:33:24 +01:00
|
|
|
HASH_SEQ_STATUS status;
|
1998-08-25 23:20:32 +02:00
|
|
|
|
2001-09-07 02:27:30 +02:00
|
|
|
proc = MyProc;
|
|
|
|
if (proc == NULL)
|
1998-08-25 23:20:32 +02:00
|
|
|
return;
|
|
|
|
|
Try to reduce confusion about what is a lock method identifier, a lock
method control structure, or a table of control structures.
. Use type LOCKMASK where an int is not a counter.
. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.
. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
LOCKMETHOD is not a pointer.
. Define and use macro LockMethodIsValid.
. Rename LOCKMETHOD to LOCKMETHODID.
. Remove global variable LongTermTableId in lmgr.c, because it is
never used.
. Make LockTableId static in lmgr.c, because it is used nowhere else.
Why not remove it and use DEFAULT_LOCKMETHOD?
. Rename the lock method control structure from LOCKMETHODTABLE to
LockMethodData. Introduce a pointer type named LockMethod.
. Remove elog(FATAL) after InitLockTable() call in
CreateSharedMemoryAndSemaphores(), because if something goes wrong,
there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
help, an elog(ERROR) in InitLockTable() is promoted to FATAL.
. Make InitLockTable() void, because its only caller does not use its
return value any more.
. Rename variables in lock.c to avoid statements like
LockMethodTable[NumLockMethods] = lockMethodTable;
lockMethodTable = LockMethodTable[lockmethod];
. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.
. Remove static variables BITS_OFF and BITS_ON from lock.c, because
I agree to this doubt:
* XXX is a fetch from a static array really faster than a shift?
. Define and use macros LOCKBIT_ON/OFF.
Manfred Koizar
2003-12-01 22:59:25 +01:00
|
|
|
Assert(lockmethodid < NumLockMethods);
|
|
|
|
lockMethodTable = LockMethods[lockmethodid];
|
1998-08-25 23:20:32 +02:00
|
|
|
if (!lockMethodTable)
|
|
|
|
return;
|
|
|
|
|
2003-12-20 18:31:21 +01:00
|
|
|
proclockTable = LockMethodProcLockHash[lockmethodid];
|
1998-08-25 23:20:32 +02:00
|
|
|
|
2000-12-22 01:51:54 +01:00
|
|
|
if (proc->waitLock)
|
|
|
|
LOCK_PRINT("DumpAllLocks: waiting on", proc->waitLock, 0);
|
1998-08-25 23:20:32 +02:00
|
|
|
|
2003-02-18 03:13:24 +01:00
|
|
|
hash_seq_init(&status, proclockTable);
|
|
|
|
while ((proclock = (PROCLOCK *) hash_seq_search(&status)) != NULL)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
2003-02-18 03:13:24 +01:00
|
|
|
PROCLOCK_PRINT("DumpAllLocks", proclock);
|
1998-08-25 23:20:32 +02:00
|
|
|
|
2003-02-18 03:13:24 +01:00
|
|
|
if (proclock->tag.lock)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
2003-02-18 03:13:24 +01:00
|
|
|
lock = (LOCK *) MAKE_PTR(proclock->tag.lock);
|
2000-05-31 02:28:42 +02:00
|
|
|
LOCK_PRINT("DumpAllLocks", lock, 0);
|
1998-08-25 23:20:32 +02:00
|
|
|
}
|
1998-09-01 06:40:42 +02:00
|
|
|
else
|
2003-02-18 03:13:24 +01:00
|
|
|
elog(LOG, "DumpAllLocks: proclock->tag.lock = NULL");
|
1998-08-25 23:20:32 +02:00
|
|
|
}
|
|
|
|
}
|
2001-10-28 07:26:15 +01:00
|
|
|
|
2001-11-05 18:46:40 +01:00
|
|
|
#endif /* LOCK_DEBUG */
|
2005-06-18 00:32:51 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* LOCK 2PC resource manager's routines
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Re-acquire a lock belonging to a transaction that was prepared.
|
|
|
|
*
|
|
|
|
* Because this function is run at db startup, re-acquiring the locks should
|
|
|
|
* never conflict with running transactions because there are none. We
|
|
|
|
* assume that the lock state represented by the stored 2PC files is legal.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
lock_twophase_recover(TransactionId xid, uint16 info,
|
|
|
|
void *recdata, uint32 len)
|
|
|
|
{
|
|
|
|
TwoPhaseLockRecord *rec = (TwoPhaseLockRecord *) recdata;
|
|
|
|
PGPROC *proc = TwoPhaseGetDummyProc(xid);
|
|
|
|
LOCKTAG *locktag;
|
|
|
|
LOCKMODE lockmode;
|
|
|
|
LOCKMETHODID lockmethodid;
|
|
|
|
LOCK *lock;
|
|
|
|
PROCLOCK *proclock;
|
|
|
|
PROCLOCKTAG proclocktag;
|
|
|
|
bool found;
|
|
|
|
LWLockId masterLock;
|
|
|
|
LockMethod lockMethodTable;
|
|
|
|
|
|
|
|
Assert(len == sizeof(TwoPhaseLockRecord));
|
|
|
|
locktag = &rec->locktag;
|
|
|
|
lockmode = rec->lockmode;
|
|
|
|
lockmethodid = locktag->locktag_lockmethodid;
|
|
|
|
|
|
|
|
Assert(lockmethodid < NumLockMethods);
|
|
|
|
lockMethodTable = LockMethods[lockmethodid];
|
|
|
|
if (!lockMethodTable)
|
|
|
|
elog(ERROR, "unrecognized lock method: %d", lockmethodid);
|
|
|
|
|
|
|
|
masterLock = lockMethodTable->masterLock;
|
|
|
|
|
|
|
|
LWLockAcquire(masterLock, LW_EXCLUSIVE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find or create a lock with this tag.
|
|
|
|
*/
|
|
|
|
lock = (LOCK *) hash_search(LockMethodLockHash[lockmethodid],
|
|
|
|
(void *) locktag,
|
|
|
|
HASH_ENTER_NULL, &found);
|
|
|
|
if (!lock)
|
|
|
|
{
|
|
|
|
LWLockRelease(masterLock);
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("out of shared memory"),
|
|
|
|
errhint("You may need to increase max_locks_per_transaction.")));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if it's a new lock object, initialize it
|
|
|
|
*/
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
lock->grantMask = 0;
|
|
|
|
lock->waitMask = 0;
|
|
|
|
SHMQueueInit(&(lock->procLocks));
|
|
|
|
ProcQueueInit(&(lock->waitProcs));
|
|
|
|
lock->nRequested = 0;
|
|
|
|
lock->nGranted = 0;
|
|
|
|
MemSet(lock->requested, 0, sizeof(int) * MAX_LOCKMODES);
|
|
|
|
MemSet(lock->granted, 0, sizeof(int) * MAX_LOCKMODES);
|
|
|
|
LOCK_PRINT("lock_twophase_recover: new", lock, lockmode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOCK_PRINT("lock_twophase_recover: found", lock, lockmode);
|
|
|
|
Assert((lock->nRequested >= 0) && (lock->requested[lockmode] >= 0));
|
|
|
|
Assert((lock->nGranted >= 0) && (lock->granted[lockmode] >= 0));
|
|
|
|
Assert(lock->nGranted <= lock->nRequested);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the hash key for the proclock table.
|
|
|
|
*/
|
|
|
|
MemSet(&proclocktag, 0, sizeof(PROCLOCKTAG)); /* must clear padding */
|
|
|
|
proclocktag.lock = MAKE_OFFSET(lock);
|
|
|
|
proclocktag.proc = MAKE_OFFSET(proc);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find or create a proclock entry with this tag
|
|
|
|
*/
|
|
|
|
proclock = (PROCLOCK *) hash_search(LockMethodProcLockHash[lockmethodid],
|
|
|
|
(void *) &proclocktag,
|
|
|
|
HASH_ENTER_NULL, &found);
|
|
|
|
if (!proclock)
|
|
|
|
{
|
|
|
|
/* Ooops, not enough shmem for the proclock */
|
|
|
|
if (lock->nRequested == 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* There are no other requestors of this lock, so garbage-collect
|
|
|
|
* the lock object. We *must* do this to avoid a permanent leak
|
|
|
|
* of shared memory, because there won't be anything to cause
|
|
|
|
* anyone to release the lock object later.
|
|
|
|
*/
|
|
|
|
Assert(SHMQueueEmpty(&(lock->procLocks)));
|
|
|
|
if (!hash_search(LockMethodLockHash[lockmethodid],
|
|
|
|
(void *) &(lock->tag),
|
|
|
|
HASH_REMOVE, NULL))
|
|
|
|
elog(PANIC, "lock table corrupted");
|
|
|
|
}
|
|
|
|
LWLockRelease(masterLock);
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("out of shared memory"),
|
|
|
|
errhint("You may need to increase max_locks_per_transaction.")));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If new, initialize the new entry
|
|
|
|
*/
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
proclock->holdMask = 0;
|
|
|
|
proclock->releaseMask = 0;
|
|
|
|
/* Add proclock to appropriate lists */
|
|
|
|
SHMQueueInsertBefore(&lock->procLocks, &proclock->lockLink);
|
|
|
|
SHMQueueInsertBefore(&proc->procLocks, &proclock->procLink);
|
|
|
|
PROCLOCK_PRINT("lock_twophase_recover: new", proclock);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PROCLOCK_PRINT("lock_twophase_recover: found", proclock);
|
|
|
|
Assert((proclock->holdMask & ~lock->grantMask) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lock->nRequested and lock->requested[] count the total number of
|
|
|
|
* requests, whether granted or waiting, so increment those
|
|
|
|
* immediately.
|
|
|
|
*/
|
|
|
|
lock->nRequested++;
|
|
|
|
lock->requested[lockmode]++;
|
|
|
|
Assert((lock->nRequested > 0) && (lock->requested[lockmode] > 0));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We shouldn't already hold the desired lock.
|
|
|
|
*/
|
|
|
|
if (proclock->holdMask & LOCKBIT_ON(lockmode))
|
|
|
|
elog(ERROR, "lock %s on object %u/%u/%u is already held",
|
|
|
|
lock_mode_names[lockmode],
|
|
|
|
lock->tag.locktag_field1, lock->tag.locktag_field2,
|
|
|
|
lock->tag.locktag_field3);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We ignore any possible conflicts and just grant ourselves the lock.
|
|
|
|
*/
|
|
|
|
GrantLock(lock, proclock, lockmode);
|
|
|
|
|
|
|
|
LWLockRelease(masterLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 2PC processing routine for COMMIT PREPARED case.
|
|
|
|
*
|
|
|
|
* Find and release the lock indicated by the 2PC record.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
lock_twophase_postcommit(TransactionId xid, uint16 info,
|
|
|
|
void *recdata, uint32 len)
|
|
|
|
{
|
|
|
|
TwoPhaseLockRecord *rec = (TwoPhaseLockRecord *) recdata;
|
|
|
|
PGPROC *proc = TwoPhaseGetDummyProc(xid);
|
|
|
|
LOCKTAG *locktag;
|
|
|
|
LOCKMODE lockmode;
|
|
|
|
LOCKMETHODID lockmethodid;
|
|
|
|
PROCLOCKTAG proclocktag;
|
|
|
|
LOCK *lock;
|
|
|
|
PROCLOCK *proclock;
|
|
|
|
LWLockId masterLock;
|
|
|
|
LockMethod lockMethodTable;
|
|
|
|
bool wakeupNeeded;
|
|
|
|
|
|
|
|
Assert(len == sizeof(TwoPhaseLockRecord));
|
|
|
|
locktag = &rec->locktag;
|
|
|
|
lockmode = rec->lockmode;
|
|
|
|
lockmethodid = locktag->locktag_lockmethodid;
|
|
|
|
|
|
|
|
Assert(lockmethodid < NumLockMethods);
|
|
|
|
lockMethodTable = LockMethods[lockmethodid];
|
|
|
|
if (!lockMethodTable)
|
|
|
|
elog(ERROR, "unrecognized lock method: %d", lockmethodid);
|
|
|
|
|
|
|
|
masterLock = lockMethodTable->masterLock;
|
|
|
|
|
|
|
|
LWLockAcquire(masterLock, LW_EXCLUSIVE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Re-find the lock object (it had better be there).
|
|
|
|
*/
|
|
|
|
lock = (LOCK *) hash_search(LockMethodLockHash[lockmethodid],
|
|
|
|
(void *) locktag,
|
|
|
|
HASH_FIND, NULL);
|
|
|
|
if (!lock)
|
|
|
|
elog(PANIC, "failed to re-find shared lock object");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Re-find the proclock object (ditto).
|
|
|
|
*/
|
|
|
|
MemSet(&proclocktag, 0, sizeof(PROCLOCKTAG)); /* must clear padding */
|
|
|
|
proclocktag.lock = MAKE_OFFSET(lock);
|
|
|
|
proclocktag.proc = MAKE_OFFSET(proc);
|
|
|
|
proclock = (PROCLOCK *) hash_search(LockMethodProcLockHash[lockmethodid],
|
|
|
|
(void *) &proclocktag,
|
|
|
|
HASH_FIND, NULL);
|
|
|
|
if (!proclock)
|
|
|
|
elog(PANIC, "failed to re-find shared proclock object");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Double-check that we are actually holding a lock of the type we
|
|
|
|
* want to release.
|
|
|
|
*/
|
|
|
|
if (!(proclock->holdMask & LOCKBIT_ON(lockmode)))
|
|
|
|
{
|
|
|
|
PROCLOCK_PRINT("lock_twophase_postcommit: WRONGTYPE", proclock);
|
|
|
|
LWLockRelease(masterLock);
|
|
|
|
elog(WARNING, "you don't own a lock of type %s",
|
|
|
|
lock_mode_names[lockmode]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do the releasing. CleanUpLock will waken any now-wakable waiters.
|
|
|
|
*/
|
|
|
|
wakeupNeeded = UnGrantLock(lock, lockmode, proclock, lockMethodTable);
|
|
|
|
|
|
|
|
CleanUpLock(lockmethodid, lock, proclock, wakeupNeeded);
|
|
|
|
|
|
|
|
LWLockRelease(masterLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 2PC processing routine for ROLLBACK PREPARED case.
|
|
|
|
*
|
|
|
|
* This is actually just the same as the COMMIT case.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
lock_twophase_postabort(TransactionId xid, uint16 info,
|
|
|
|
void *recdata, uint32 len)
|
|
|
|
{
|
|
|
|
lock_twophase_postcommit(xid, info, recdata, len);
|
|
|
|
}
|