lmgr.c 14.3 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * lmgr.c
4
 *	  POSTGRES lock manager code
5
 *
6
 * Portions Copyright (c) 1996-2006, 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.88 2006/09/22 23:20:13 tgl Exp $
12 13 14
 *
 *-------------------------------------------------------------------------
 */
15

16
#include "postgres.h"
17

18
#include "access/subtrans.h"
19 20
#include "access/transam.h"
#include "access/xact.h"
21 22
#include "catalog/catalog.h"
#include "catalog/namespace.h"
23
#include "miscadmin.h"
24
#include "storage/lmgr.h"
25
#include "storage/procarray.h"
26
#include "utils/inval.h"
27
#include "utils/lsyscache.h"
28 29 30


/*
B
Bruce Momjian 已提交
31
 * RelationInitLockInfo
32
 *		Initializes the lock information in a relation descriptor.
33 34
 *
 *		relcache.c must call this during creation of any reldesc.
35 36 37 38
 */
void
RelationInitLockInfo(Relation relation)
{
39
	Assert(RelationIsValid(relation));
40
	Assert(OidIsValid(RelationGetRelid(relation)));
41

42
	relation->rd_lockInfo.lockRelId.relId = RelationGetRelid(relation);
43

44
	if (relation->rd_rel->relisshared)
45
		relation->rd_lockInfo.lockRelId.dbId = InvalidOid;
46
	else
47
		relation->rd_lockInfo.lockRelId.dbId = MyDatabaseId;
48 49
}

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
/*
 * SetLocktagRelationOid
 *		Set up a locktag for a relation, given only relation OID
 */
static inline void
SetLocktagRelationOid(LOCKTAG *tag, Oid relid)
{
	Oid		dbid;

	if (IsSharedRelation(relid))
		dbid = InvalidOid;
	else
		dbid = MyDatabaseId;

	SET_LOCKTAG_RELATION(*tag, dbid, relid);
}

/*
 *		LockRelationOid
 *
 * Lock a relation given only its OID.  This should generally be used
 * before attempting to open the relation's relcache entry.
 */
void
LockRelationOid(Oid relid, LOCKMODE lockmode)
{
	LOCKTAG		tag;
	LockAcquireResult res;

	SetLocktagRelationOid(&tag, relid);

	res = LockAcquire(&tag, lockmode, false, false);

	/*
	 * Now that we have the lock, check for invalidation messages, so that
	 * we will update or flush any stale relcache entry before we try to use
	 * it.  We can skip this in the not-uncommon case that we already had
	 * the same type of lock being requested, since then no one else could
	 * have modified the relcache entry in an undesirable way.  (In the
	 * case where our own xact modifies the rel, the relcache update happens
	 * via CommandCounterIncrement, not here.)
	 */
	if (res != LOCKACQUIRE_ALREADY_HELD)
		AcceptInvalidationMessages();
}

/*
 *		ConditionalLockRelationOid
 *
 * As above, but only lock if we can get the lock without blocking.
 * Returns TRUE iff the lock was acquired.
 *
 * NOTE: we do not currently need conditional versions of all the
 * LockXXX routines in this file, but they could easily be added if needed.
 */
bool
ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
{
	LOCKTAG		tag;
	LockAcquireResult res;

	SetLocktagRelationOid(&tag, relid);

	res = LockAcquire(&tag, lockmode, false, true);

	if (res == LOCKACQUIRE_NOT_AVAIL)
		return false;

	/*
	 * Now that we have the lock, check for invalidation messages; see
	 * notes in LockRelationOid.
	 */
	if (res != LOCKACQUIRE_ALREADY_HELD)
		AcceptInvalidationMessages();

	return true;
}

/*
 *		UnlockRelationId
 *
131 132
 * Unlock, given a LockRelId.  This is preferred over UnlockRelationOid
 * for speed reasons.
133 134 135 136 137 138 139 140 141 142 143
 */
void
UnlockRelationId(LockRelId *relid, LOCKMODE lockmode)
{
	LOCKTAG		tag;

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

	LockRelease(&tag, lockmode, false);
}

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
/*
 *		UnlockRelationOid
 *
 * Unlock, given only a relation Oid.  Use UnlockRelationId if you can.
 */
void
UnlockRelationOid(Oid relid, LOCKMODE lockmode)
{
	LOCKTAG		tag;

	SetLocktagRelationOid(&tag, relid);

	LockRelease(&tag, lockmode, false);
}

159
/*
V
Vadim B. Mikheev 已提交
160
 *		LockRelation
161 162 163 164
 *
 * This is a convenience routine for acquiring an additional lock on an
 * already-open relation.  Never try to do "relation_open(foo, NoLock)"
 * and then lock with this.
165 166
 */
void
V
Vadim B. Mikheev 已提交
167
LockRelation(Relation relation, LOCKMODE lockmode)
168
{
V
Vadim B. Mikheev 已提交
169
	LOCKTAG		tag;
170
	LockAcquireResult res;
171

172 173 174
	SET_LOCKTAG_RELATION(tag,
						 relation->rd_lockInfo.lockRelId.dbId,
						 relation->rd_lockInfo.lockRelId.relId);
V
Vadim B. Mikheev 已提交
175

176
	res = LockAcquire(&tag, lockmode, false, false);
177 178

	/*
179 180
	 * Now that we have the lock, check for invalidation messages; see
	 * notes in LockRelationOid.
181
	 */
182 183
	if (res != LOCKACQUIRE_ALREADY_HELD)
		AcceptInvalidationMessages();
V
Vadim B. Mikheev 已提交
184
}
185

186 187 188
/*
 *		ConditionalLockRelation
 *
189 190 191
 * This is a convenience routine for acquiring an additional lock on an
 * already-open relation.  Never try to do "relation_open(foo, NoLock)"
 * and then lock with this.
192 193 194 195 196
 */
bool
ConditionalLockRelation(Relation relation, LOCKMODE lockmode)
{
	LOCKTAG		tag;
197
	LockAcquireResult res;
198

199 200 201
	SET_LOCKTAG_RELATION(tag,
						 relation->rd_lockInfo.lockRelId.dbId,
						 relation->rd_lockInfo.lockRelId.relId);
202

203
	res = LockAcquire(&tag, lockmode, false, true);
204 205

	if (res == LOCKACQUIRE_NOT_AVAIL)
206 207 208
		return false;

	/*
209 210
	 * Now that we have the lock, check for invalidation messages; see
	 * notes in LockRelationOid.
211
	 */
212 213
	if (res != LOCKACQUIRE_ALREADY_HELD)
		AcceptInvalidationMessages();
214 215 216 217

	return true;
}

218
/*
V
Vadim B. Mikheev 已提交
219
 *		UnlockRelation
220 221 222
 *
 * This is a convenience routine for unlocking a relation without also
 * closing it.
223 224
 */
void
V
Vadim B. Mikheev 已提交
225
UnlockRelation(Relation relation, LOCKMODE lockmode)
226
{
V
Vadim B. Mikheev 已提交
227
	LOCKTAG		tag;
228

229 230 231
	SET_LOCKTAG_RELATION(tag,
						 relation->rd_lockInfo.lockRelId.dbId,
						 relation->rd_lockInfo.lockRelId.relId);
232

233
	LockRelease(&tag, lockmode, false);
234 235 236
}

/*
237
 *		LockRelationIdForSession
238
 *
B
Bruce Momjian 已提交
239
 * This routine grabs a session-level lock on the target relation.	The
240
 * session lock persists across transaction boundaries.  It will be removed
241
 * when UnlockRelationIdForSession() is called, or if an ereport(ERROR) occurs,
242 243 244 245 246 247 248
 * 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
249
LockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
250 251 252
{
	LOCKTAG		tag;

253
	SET_LOCKTAG_RELATION(tag, relid->dbId, relid->relId);
254

255
	(void) LockAcquire(&tag, lockmode, true, false);
256 257 258
}

/*
259
 *		UnlockRelationIdForSession
260 261
 */
void
262
UnlockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
263 264 265
{
	LOCKTAG		tag;

266
	SET_LOCKTAG_RELATION(tag, relid->dbId, relid->relId);
267

268
	LockRelease(&tag, lockmode, true);
V
Vadim B. Mikheev 已提交
269
}
270

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
/*
 *		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);

290
	(void) LockAcquire(&tag, lockmode, false, false);
291 292 293 294 295 296 297 298 299 300 301 302 303 304
}

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

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

305
	LockRelease(&tag, lockmode, false);
306 307
}

308
/*
V
Vadim B. Mikheev 已提交
309
 *		LockPage
310 311
 *
 * Obtain a page-level lock.  This is currently used by some index access
312
 * methods to lock individual index pages.
313 314
 */
void
V
Vadim B. Mikheev 已提交
315
LockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
316
{
V
Vadim B. Mikheev 已提交
317
	LOCKTAG		tag;
318

319 320 321 322
	SET_LOCKTAG_PAGE(tag,
					 relation->rd_lockInfo.lockRelId.dbId,
					 relation->rd_lockInfo.lockRelId.relId,
					 blkno);
V
Vadim B. Mikheev 已提交
323

324
	(void) LockAcquire(&tag, lockmode, false, false);
V
Vadim B. Mikheev 已提交
325
}
326

327 328 329 330 331 332 333 334 335 336 337
/*
 *		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;

338 339 340 341
	SET_LOCKTAG_PAGE(tag,
					 relation->rd_lockInfo.lockRelId.dbId,
					 relation->rd_lockInfo.lockRelId.relId,
					 blkno);
342

343
	return (LockAcquire(&tag, lockmode, false, true) != LOCKACQUIRE_NOT_AVAIL);
344 345
}

346
/*
V
Vadim B. Mikheev 已提交
347
 *		UnlockPage
348 349
 */
void
V
Vadim B. Mikheev 已提交
350
UnlockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
351
{
V
Vadim B. Mikheev 已提交
352
	LOCKTAG		tag;
353

354 355 356 357
	SET_LOCKTAG_PAGE(tag,
					 relation->rd_lockInfo.lockRelId.dbId,
					 relation->rd_lockInfo.lockRelId.relId,
					 blkno);
358

359
	LockRelease(&tag, lockmode, false);
360 361
}

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
/*
 *		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));

380
	(void) LockAcquire(&tag, lockmode, false, false);
381 382
}

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
/*
 *		ConditionalLockTuple
 *
 * As above, but only lock if we can get the lock without blocking.
 * Returns TRUE iff the lock was acquired.
 */
bool
ConditionalLockTuple(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));

400
	return (LockAcquire(&tag, lockmode, false, true) != LOCKACQUIRE_NOT_AVAIL);
401 402
}

403 404 405 406 407 408 409 410 411 412 413 414 415 416
/*
 *		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));

417
	LockRelease(&tag, lockmode, false);
418 419
}

420 421 422 423 424 425 426
/*
 *		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.
 */
427
void
V
Vadim B. Mikheev 已提交
428
XactLockTableInsert(TransactionId xid)
429
{
V
Vadim B. Mikheev 已提交
430
	LOCKTAG		tag;
431

432
	SET_LOCKTAG_TRANSACTION(tag, xid);
433

434
	(void) LockAcquire(&tag, ExclusiveLock, false, false);
435 436
}

437 438 439 440 441
/*
 *		XactLockTableDelete
 *
 * Delete the lock showing that the given transaction ID is running.
 * (This is never used for main transaction IDs; those locks are only
B
Bruce Momjian 已提交
442
 * released implicitly at transaction end.	But we do use it for subtrans
443 444 445 446 447 448 449
 * IDs.)
 */
void
XactLockTableDelete(TransactionId xid)
{
	LOCKTAG		tag;

450
	SET_LOCKTAG_TRANSACTION(tag, xid);
451

452
	LockRelease(&tag, ExclusiveLock, false);
453 454
}

455 456 457 458
/*
 *		XactLockTableWait
 *
 * Wait for the specified transaction to commit or abort.
459
 *
460 461 462 463
 * 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
B
Bruce Momjian 已提交
464
 * successfully or unsuccessfully.	So we have to check if it's "still running"
465
 * and if so wait for its parent.
466
 */
467
void
V
Vadim B. Mikheev 已提交
468
XactLockTableWait(TransactionId xid)
469
{
V
Vadim B. Mikheev 已提交
470
	LOCKTAG		tag;
471

472 473 474
	for (;;)
	{
		Assert(TransactionIdIsValid(xid));
475
		Assert(!TransactionIdEquals(xid, GetTopTransactionId()));
476

477
		SET_LOCKTAG_TRANSACTION(tag, xid);
478

479
		(void) LockAcquire(&tag, ShareLock, false, false);
480

481
		LockRelease(&tag, ShareLock, false);
482

483 484 485 486
		if (!TransactionIdIsInProgress(xid))
			break;
		xid = SubTransGetParent(xid);
	}
487
}
488

489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
/*
 *		ConditionalXactLockTableWait
 *
 * As above, but only lock if we can get the lock without blocking.
 * Returns TRUE if the lock was acquired.
 */
bool
ConditionalXactLockTableWait(TransactionId xid)
{
	LOCKTAG		tag;

	for (;;)
	{
		Assert(TransactionIdIsValid(xid));
		Assert(!TransactionIdEquals(xid, GetTopTransactionId()));

		SET_LOCKTAG_TRANSACTION(tag, xid);

507
		if (LockAcquire(&tag, ShareLock, false, true) == LOCKACQUIRE_NOT_AVAIL)
508 509
			return false;

510
		LockRelease(&tag, ShareLock, false);
511 512 513 514 515 516 517 518

		if (!TransactionIdIsInProgress(xid))
			break;
		xid = SubTransGetParent(xid);
	}

	return true;
}
519 520 521 522 523

/*
 *		LockDatabaseObject
 *
 * Obtain a lock on a general object of the current database.  Don't use
524 525
 * this for shared objects (such as tablespaces).  It's unwise to apply it
 * to relations, also, since a lock taken this way will NOT conflict with
526
 * locks taken via LockRelation and friends.
527 528 529 530 531 532 533 534 535 536 537 538 539
 */
void
LockDatabaseObject(Oid classid, Oid objid, uint16 objsubid,
				   LOCKMODE lockmode)
{
	LOCKTAG		tag;

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

540
	(void) LockAcquire(&tag, lockmode, false, false);
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
}

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

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

558
	LockRelease(&tag, lockmode, false);
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
}

/*
 *		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);

578
	(void) LockAcquire(&tag, lockmode, false, false);
579 580 581

	/* Make sure syscaches are up-to-date with any changes we waited for */
	AcceptInvalidationMessages();
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
}

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

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

599
	LockRelease(&tag, lockmode, false);
600
}
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631


/*
 * LockTagIsTemp
 *		Determine whether a locktag is for a lock on a temporary object
 *
 * We need this because 2PC cannot deal with temp objects
 */
bool
LockTagIsTemp(const LOCKTAG *tag)
{
	switch (tag->locktag_type)
	{
		case LOCKTAG_RELATION:
		case LOCKTAG_RELATION_EXTEND:
		case LOCKTAG_PAGE:
		case LOCKTAG_TUPLE:
			/* check for lock on a temp relation */
			/* field1 is dboid, field2 is reloid for all of these */
			if ((Oid) tag->locktag_field1 == InvalidOid)
				return false;	/* shared, so not temp */
			if (isTempNamespace(get_rel_namespace((Oid) tag->locktag_field2)))
				return true;
			break;
		case LOCKTAG_TRANSACTION:
			/* there are no temp transactions */
			break;
		case LOCKTAG_OBJECT:
			/* there are currently no non-table temp objects */
			break;
		case LOCKTAG_USERLOCK:
632
		case LOCKTAG_ADVISORY:
633 634 635 636 637
			/* assume these aren't temp */
			break;
	}
	return false;				/* default case */
}