lmgr.c 13.2 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * lmgr.c
4
 *	  POSTGRES lock manager code
5
 *
P
 
PostgreSQL Daemon 已提交
6
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
7
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
11
 *	  $PostgreSQL: pgsql/src/backend/storage/lmgr/lmgr.c,v 1.74 2005/05/19 21:35:46 tgl Exp $
12 13 14
 *
 *-------------------------------------------------------------------------
 */
15

16
#include "postgres.h"
17

18
#include "access/subtrans.h"
B
Bruce Momjian 已提交
19
#include "access/transam.h"
20
#include "access/xact.h"
B
Bruce Momjian 已提交
21
#include "catalog/catalog.h"
22
#include "miscadmin.h"
23
#include "storage/lmgr.h"
24
#include "storage/procarray.h"
25
#include "utils/inval.h"
26 27


28 29 30 31
/*
 * This conflict table defines the semantics of the various lock modes.
 */
static const LOCKMASK LockConflicts[] = {
32
	0,
V
Vadim B. Mikheev 已提交
33

34
	/* AccessShareLock */
V
Vadim B. Mikheev 已提交
35 36
	(1 << AccessExclusiveLock),

37
	/* RowShareLock */
V
Vadim B. Mikheev 已提交
38 39
	(1 << ExclusiveLock) | (1 << AccessExclusiveLock),

40
	/* RowExclusiveLock */
41 42 43 44 45 46 47
	(1 << ShareLock) | (1 << ShareRowExclusiveLock) |
	(1 << ExclusiveLock) | (1 << AccessExclusiveLock),

	/* ShareUpdateExclusiveLock */
	(1 << ShareUpdateExclusiveLock) |
	(1 << ShareLock) | (1 << ShareRowExclusiveLock) |
	(1 << ExclusiveLock) | (1 << AccessExclusiveLock),
V
Vadim B. Mikheev 已提交
48

49
	/* ShareLock */
50 51 52
	(1 << RowExclusiveLock) | (1 << ShareUpdateExclusiveLock) |
	(1 << ShareRowExclusiveLock) |
	(1 << ExclusiveLock) | (1 << AccessExclusiveLock),
V
Vadim B. Mikheev 已提交
53

54
	/* ShareRowExclusiveLock */
55 56 57
	(1 << RowExclusiveLock) | (1 << ShareUpdateExclusiveLock) |
	(1 << ShareLock) | (1 << ShareRowExclusiveLock) |
	(1 << ExclusiveLock) | (1 << AccessExclusiveLock),
V
Vadim B. Mikheev 已提交
58

59
	/* ExclusiveLock */
60 61 62 63
	(1 << RowShareLock) |
	(1 << RowExclusiveLock) | (1 << ShareUpdateExclusiveLock) |
	(1 << ShareLock) | (1 << ShareRowExclusiveLock) |
	(1 << ExclusiveLock) | (1 << AccessExclusiveLock),
V
Vadim B. Mikheev 已提交
64

65
	/* AccessExclusiveLock */
66 67 68 69
	(1 << AccessShareLock) | (1 << RowShareLock) |
	(1 << RowExclusiveLock) | (1 << ShareUpdateExclusiveLock) |
	(1 << ShareLock) | (1 << ShareRowExclusiveLock) |
	(1 << ExclusiveLock) | (1 << AccessExclusiveLock)
V
Vadim B. Mikheev 已提交
70 71 72

};

B
Bruce Momjian 已提交
73
static LOCKMETHODID LockTableId = INVALID_LOCKMETHOD;
V
Vadim B. Mikheev 已提交
74

75

V
Vadim B. Mikheev 已提交
76
/*
77
 * Create the lock table described by LockConflicts
V
Vadim B. Mikheev 已提交
78
 */
79
void
80
InitLockTable(int maxBackends)
V
Vadim B. Mikheev 已提交
81
{
B
Bruce Momjian 已提交
82
	LOCKMETHODID LongTermTableId;
V
Vadim B. Mikheev 已提交
83

84 85 86 87 88 89 90
	/* there's no zero-th table */
	NumLockMethods = 1;

	/*
	 * Create the default lock method table
	 */

91
	/* number of lock modes is lengthof()-1 because of dummy zero */
92 93 94 95 96
	LockTableId = LockMethodTableInit("LockTable",
									  LockConflicts,
									  lengthof(LockConflicts) - 1,
									  maxBackends);
	if (!LockMethodIsValid(LockTableId))
97
		elog(ERROR, "could not initialize lock table");
98
	Assert(LockTableId == DEFAULT_LOCKMETHOD);
V
Vadim B. Mikheev 已提交
99 100

#ifdef USER_LOCKS
B
Bruce Momjian 已提交
101

V
Vadim B. Mikheev 已提交
102
	/*
103
	 * Allocate another tableId for user locks (same shared hashtable though)
V
Vadim B. Mikheev 已提交
104 105
	 */
	LongTermTableId = LockMethodTableRename(LockTableId);
106
	if (!LockMethodIsValid(LongTermTableId))
107
		elog(ERROR, "could not rename user lock table");
108
	Assert(LongTermTableId == USER_LOCKMETHOD);
V
Vadim B. Mikheev 已提交
109 110 111
#endif
}

112
/*
B
Bruce Momjian 已提交
113
 * RelationInitLockInfo
114
 *		Initializes the lock information in a relation descriptor.
115 116
 *
 *		relcache.c must call this during creation of any reldesc.
117 118 119 120
 */
void
RelationInitLockInfo(Relation relation)
{
121
	Assert(RelationIsValid(relation));
122
	Assert(OidIsValid(RelationGetRelid(relation)));
123

124
	relation->rd_lockInfo.lockRelId.relId = RelationGetRelid(relation);
125

126
	if (relation->rd_rel->relisshared)
127
		relation->rd_lockInfo.lockRelId.dbId = InvalidOid;
128
	else
129
		relation->rd_lockInfo.lockRelId.dbId = MyDatabaseId;
130 131 132
}

/*
V
Vadim B. Mikheev 已提交
133
 *		LockRelation
134 135
 */
void
V
Vadim B. Mikheev 已提交
136
LockRelation(Relation relation, LOCKMODE lockmode)
137
{
V
Vadim B. Mikheev 已提交
138
	LOCKTAG		tag;
139

140 141 142
	SET_LOCKTAG_RELATION(tag,
						 relation->rd_lockInfo.lockRelId.dbId,
						 relation->rd_lockInfo.lockRelId.relId);
V
Vadim B. Mikheev 已提交
143

144
	if (!LockAcquire(LockTableId, &tag, GetTopTransactionId(),
145
					 lockmode, false))
146
		elog(ERROR, "LockAcquire failed");
147 148

	/*
149
	 * Check to see if the relcache entry has been invalidated while we
150
	 * were waiting to lock it.  If so, rebuild it, or ereport() trying.
151 152
	 * Increment the refcount to ensure that RelationFlushRelation will
	 * rebuild it and not just delete it.
153 154
	 */
	RelationIncrementReferenceCount(relation);
155
	AcceptInvalidationMessages();
156
	RelationDecrementReferenceCount(relation);
V
Vadim B. Mikheev 已提交
157
}
158

159 160 161 162 163 164
/*
 *		ConditionalLockRelation
 *
 * As above, but only lock if we can get the lock without blocking.
 * Returns TRUE iff the lock was acquired.
 *
165
 * NOTE: we do not currently need conditional versions of all the
166 167 168 169 170 171 172
 * LockXXX routines in this file, but they could easily be added if needed.
 */
bool
ConditionalLockRelation(Relation relation, LOCKMODE lockmode)
{
	LOCKTAG		tag;

173 174 175
	SET_LOCKTAG_RELATION(tag,
						 relation->rd_lockInfo.lockRelId.dbId,
						 relation->rd_lockInfo.lockRelId.relId);
176

177
	if (!LockAcquire(LockTableId, &tag, GetTopTransactionId(),
178 179 180 181 182
					 lockmode, true))
		return false;

	/*
	 * Check to see if the relcache entry has been invalidated while we
183
	 * were waiting to lock it.  If so, rebuild it, or ereport() trying.
184 185 186 187 188 189 190 191 192 193
	 * Increment the refcount to ensure that RelationFlushRelation will
	 * rebuild it and not just delete it.
	 */
	RelationIncrementReferenceCount(relation);
	AcceptInvalidationMessages();
	RelationDecrementReferenceCount(relation);

	return true;
}

194
/*
V
Vadim B. Mikheev 已提交
195
 *		UnlockRelation
196 197
 */
void
V
Vadim B. Mikheev 已提交
198
UnlockRelation(Relation relation, LOCKMODE lockmode)
199
{
V
Vadim B. Mikheev 已提交
200
	LOCKTAG		tag;
201

202 203 204
	SET_LOCKTAG_RELATION(tag,
						 relation->rd_lockInfo.lockRelId.dbId,
						 relation->rd_lockInfo.lockRelId.relId);
205

206
	LockRelease(LockTableId, &tag, GetTopTransactionId(), lockmode);
207 208 209 210 211
}

/*
 *		LockRelationForSession
 *
B
Bruce Momjian 已提交
212
 * This routine grabs a session-level lock on the target relation.	The
213
 * session lock persists across transaction boundaries.  It will be removed
214
 * when UnlockRelationForSession() is called, or if an ereport(ERROR) occurs,
215 216 217 218 219 220 221 222 223 224 225
 * or if the backend exits.
 *
 * Note that one should also grab a transaction-level lock on the rel
 * in any transaction that actually uses the rel, to ensure that the
 * relcache entry is up to date.
 */
void
LockRelationForSession(LockRelId *relid, LOCKMODE lockmode)
{
	LOCKTAG		tag;

226
	SET_LOCKTAG_RELATION(tag, relid->dbId, relid->relId);
227

228 229
	if (!LockAcquire(LockTableId, &tag, InvalidTransactionId,
					 lockmode, false))
230
		elog(ERROR, "LockAcquire failed");
231 232 233 234 235 236 237 238 239 240
}

/*
 *		UnlockRelationForSession
 */
void
UnlockRelationForSession(LockRelId *relid, LOCKMODE lockmode)
{
	LOCKTAG		tag;

241
	SET_LOCKTAG_RELATION(tag, relid->dbId, relid->relId);
242 243

	LockRelease(LockTableId, &tag, InvalidTransactionId, lockmode);
V
Vadim B. Mikheev 已提交
244
}
245

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
/*
 *		LockRelationForExtension
 *
 * This lock tag is used to interlock addition of pages to relations.
 * We need such locking because bufmgr/smgr definition of P_NEW is not
 * race-condition-proof.
 *
 * We assume the caller is already holding some type of regular lock on
 * the relation, so no AcceptInvalidationMessages call is needed here.
 */
void
LockRelationForExtension(Relation relation, LOCKMODE lockmode)
{
	LOCKTAG		tag;

	SET_LOCKTAG_RELATION_EXTEND(tag,
								relation->rd_lockInfo.lockRelId.dbId,
								relation->rd_lockInfo.lockRelId.relId);

	if (!LockAcquire(LockTableId, &tag, GetTopTransactionId(),
					 lockmode, false))
		elog(ERROR, "LockAcquire failed");
}

/*
 *		UnlockRelationForExtension
 */
void
UnlockRelationForExtension(Relation relation, LOCKMODE lockmode)
{
	LOCKTAG		tag;

	SET_LOCKTAG_RELATION_EXTEND(tag,
								relation->rd_lockInfo.lockRelId.dbId,
								relation->rd_lockInfo.lockRelId.relId);

	LockRelease(LockTableId, &tag, GetTopTransactionId(), lockmode);
}

285
/*
V
Vadim B. Mikheev 已提交
286
 *		LockPage
287 288
 *
 * Obtain a page-level lock.  This is currently used by some index access
289
 * methods to lock individual index pages.
290 291
 */
void
V
Vadim B. Mikheev 已提交
292
LockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
293
{
V
Vadim B. Mikheev 已提交
294
	LOCKTAG		tag;
295

296 297 298 299
	SET_LOCKTAG_PAGE(tag,
					 relation->rd_lockInfo.lockRelId.dbId,
					 relation->rd_lockInfo.lockRelId.relId,
					 blkno);
V
Vadim B. Mikheev 已提交
300

301
	if (!LockAcquire(LockTableId, &tag, GetTopTransactionId(),
302
					 lockmode, false))
303
		elog(ERROR, "LockAcquire failed");
V
Vadim B. Mikheev 已提交
304
}
305

306 307 308 309 310 311 312 313 314 315 316
/*
 *		ConditionalLockPage
 *
 * As above, but only lock if we can get the lock without blocking.
 * Returns TRUE iff the lock was acquired.
 */
bool
ConditionalLockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
{
	LOCKTAG		tag;

317 318 319 320
	SET_LOCKTAG_PAGE(tag,
					 relation->rd_lockInfo.lockRelId.dbId,
					 relation->rd_lockInfo.lockRelId.relId,
					 blkno);
321

322
	return LockAcquire(LockTableId, &tag, GetTopTransactionId(),
323 324 325
					   lockmode, true);
}

326
/*
V
Vadim B. Mikheev 已提交
327
 *		UnlockPage
328 329
 */
void
V
Vadim B. Mikheev 已提交
330
UnlockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
331
{
V
Vadim B. Mikheev 已提交
332
	LOCKTAG		tag;
333

334 335 336 337
	SET_LOCKTAG_PAGE(tag,
					 relation->rd_lockInfo.lockRelId.dbId,
					 relation->rd_lockInfo.lockRelId.relId,
					 blkno);
338

339
	LockRelease(LockTableId, &tag, GetTopTransactionId(), lockmode);
340 341
}

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
/*
 *		LockTuple
 *
 * Obtain a tuple-level lock.  This is used in a less-than-intuitive fashion
 * because we can't afford to keep a separate lock in shared memory for every
 * tuple.  See heap_lock_tuple before using this!
 */
void
LockTuple(Relation relation, ItemPointer tid, LOCKMODE lockmode)
{
	LOCKTAG		tag;

	SET_LOCKTAG_TUPLE(tag,
					  relation->rd_lockInfo.lockRelId.dbId,
					  relation->rd_lockInfo.lockRelId.relId,
					  ItemPointerGetBlockNumber(tid),
					  ItemPointerGetOffsetNumber(tid));

	if (!LockAcquire(LockTableId, &tag, GetTopTransactionId(),
					 lockmode, false))
		elog(ERROR, "LockAcquire failed");
}

/*
 *		UnlockTuple
 */
void
UnlockTuple(Relation relation, ItemPointer tid, LOCKMODE lockmode)
{
	LOCKTAG		tag;

	SET_LOCKTAG_TUPLE(tag,
					  relation->rd_lockInfo.lockRelId.dbId,
					  relation->rd_lockInfo.lockRelId.relId,
					  ItemPointerGetBlockNumber(tid),
					  ItemPointerGetOffsetNumber(tid));

	LockRelease(LockTableId, &tag, GetTopTransactionId(), lockmode);
}

382 383 384 385 386 387 388
/*
 *		XactLockTableInsert
 *
 * Insert a lock showing that the given transaction ID is running ---
 * this is done during xact startup.  The lock can then be used to wait
 * for the transaction to finish.
 */
389
void
V
Vadim B. Mikheev 已提交
390
XactLockTableInsert(TransactionId xid)
391
{
V
Vadim B. Mikheev 已提交
392
	LOCKTAG		tag;
393

394
	SET_LOCKTAG_TRANSACTION(tag, xid);
395

396
	if (!LockAcquire(LockTableId, &tag, GetTopTransactionId(),
397
					 ExclusiveLock, false))
398
		elog(ERROR, "LockAcquire failed");
399 400
}

401 402 403 404 405 406 407 408 409 410 411 412 413
/*
 *		XactLockTableDelete
 *
 * Delete the lock showing that the given transaction ID is running.
 * (This is never used for main transaction IDs; those locks are only
 * released implicitly at transaction end.  But we do use it for subtrans
 * IDs.)
 */
void
XactLockTableDelete(TransactionId xid)
{
	LOCKTAG		tag;

414
	SET_LOCKTAG_TRANSACTION(tag, xid);
415 416 417 418

	LockRelease(LockTableId, &tag, GetTopTransactionId(), ExclusiveLock);
}

419 420 421 422
/*
 *		XactLockTableWait
 *
 * Wait for the specified transaction to commit or abort.
423
 *
424 425 426 427 428 429
 * Note that this does the right thing for subtransactions: if we wait on a
 * subtransaction, we will exit as soon as it aborts or its top parent commits.
 * It takes some extra work to ensure this, because to save on shared memory
 * the XID lock of a subtransaction is released when it ends, whether
 * successfully or unsuccessfully.  So we have to check if it's "still running"
 * and if so wait for its parent.
430
 */
431
void
V
Vadim B. Mikheev 已提交
432
XactLockTableWait(TransactionId xid)
433
{
V
Vadim B. Mikheev 已提交
434
	LOCKTAG		tag;
435
	TransactionId myxid = GetTopTransactionId();
436

437 438 439 440
	for (;;)
	{
		Assert(TransactionIdIsValid(xid));
		Assert(!TransactionIdEquals(xid, myxid));
441

442
		SET_LOCKTAG_TRANSACTION(tag, xid);
443

444 445 446
		if (!LockAcquire(LockTableId, &tag, myxid, ShareLock, false))
			elog(ERROR, "LockAcquire failed");
		LockRelease(LockTableId, &tag, myxid, ShareLock);
447

448 449 450 451
		if (!TransactionIdIsInProgress(xid))
			break;
		xid = SubTransGetParent(xid);
	}
452

V
Vadim B. Mikheev 已提交
453
	/*
B
Bruce Momjian 已提交
454
	 * Transaction was committed/aborted/crashed - we have to update
455
	 * pg_clog if transaction is still marked as running.
456
	 */
457 458
	if (!TransactionIdDidCommit(xid) && !TransactionIdDidAbort(xid))
		TransactionIdAbort(xid);
459
}
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543


/*
 *		LockDatabaseObject
 *
 * Obtain a lock on a general object of the current database.  Don't use
 * this for shared objects (such as tablespaces).  It's usually unwise to
 * apply it to entire relations, also, since a lock taken this way will
 * NOT conflict with LockRelation.
 */
void
LockDatabaseObject(Oid classid, Oid objid, uint16 objsubid,
				   LOCKMODE lockmode)
{
	LOCKTAG		tag;

	SET_LOCKTAG_OBJECT(tag,
					   MyDatabaseId,
					   classid,
					   objid,
					   objsubid);

	if (!LockAcquire(LockTableId, &tag, GetTopTransactionId(),
					 lockmode, false))
		elog(ERROR, "LockAcquire failed");
}

/*
 *		UnlockDatabaseObject
 */
void
UnlockDatabaseObject(Oid classid, Oid objid, uint16 objsubid,
					 LOCKMODE lockmode)
{
	LOCKTAG		tag;

	SET_LOCKTAG_OBJECT(tag,
					   MyDatabaseId,
					   classid,
					   objid,
					   objsubid);

	LockRelease(LockTableId, &tag, GetTopTransactionId(), lockmode);
}

/*
 *		LockSharedObject
 *
 * Obtain a lock on a shared-across-databases object.
 */
void
LockSharedObject(Oid classid, Oid objid, uint16 objsubid,
				 LOCKMODE lockmode)
{
	LOCKTAG		tag;

	SET_LOCKTAG_OBJECT(tag,
					   InvalidOid,
					   classid,
					   objid,
					   objsubid);

	if (!LockAcquire(LockTableId, &tag, GetTopTransactionId(),
					 lockmode, false))
		elog(ERROR, "LockAcquire failed");
}

/*
 *		UnlockSharedObject
 */
void
UnlockSharedObject(Oid classid, Oid objid, uint16 objsubid,
				   LOCKMODE lockmode)
{
	LOCKTAG		tag;

	SET_LOCKTAG_OBJECT(tag,
					   InvalidOid,
					   classid,
					   objid,
					   objsubid);

	LockRelease(LockTableId, &tag, GetTopTransactionId(), lockmode);
}