lmgr.c 18.3 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * lmgr.c
4
 *	  POSTGRES lock manager code
5
 *
B
Bruce Momjian 已提交
6
 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
7
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
11
 *	  src/backend/storage/lmgr/lmgr.c
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
#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


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

40
	relation->rd_lockInfo.lockRelId.relId = RelationGetRelid(relation);
41

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

48 49 50 51 52 53 54
/*
 * SetLocktagRelationOid
 *		Set up a locktag for a relation, given only relation OID
 */
static inline void
SetLocktagRelationOid(LOCKTAG *tag, Oid relid)
{
B
Bruce Momjian 已提交
55
	Oid			dbid;
56 57 58 59 60 61 62 63 64 65 66 67

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

	SET_LOCKTAG_RELATION(*tag, dbid, relid);
}

/*
 *		LockRelationOid
 *
B
Bruce Momjian 已提交
68
 * Lock a relation given only its OID.	This should generally be used
69 70 71 72 73 74 75 76 77 78 79 80 81
 * 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);

	/*
B
Bruce Momjian 已提交
82 83
	 * 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.
84 85 86 87 88
	 * RangeVarGetRelid() specifically relies on us for this.  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
B
Bruce Momjian 已提交
89
	 * CommandCounterIncrement, not here.)
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
	 */
	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;

	/*
B
Bruce Momjian 已提交
118 119
	 * Now that we have the lock, check for invalidation messages; see notes
	 * in LockRelationOid.
120 121 122 123 124 125 126 127 128 129
	 */
	if (res != LOCKACQUIRE_ALREADY_HELD)
		AcceptInvalidationMessages();

	return true;
}

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

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

	LockRelease(&tag, lockmode, false);
}

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
/*
 *		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);
}

158
/*
V
Vadim B. Mikheev 已提交
159
 *		LockRelation
160 161 162 163
 *
 * 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.
164 165
 */
void
V
Vadim B. Mikheev 已提交
166
LockRelation(Relation relation, LOCKMODE lockmode)
167
{
V
Vadim B. Mikheev 已提交
168
	LOCKTAG		tag;
169
	LockAcquireResult res;
170

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

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

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

185 186 187
/*
 *		ConditionalLockRelation
 *
188 189 190
 * 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.
191 192 193 194 195
 */
bool
ConditionalLockRelation(Relation relation, LOCKMODE lockmode)
{
	LOCKTAG		tag;
196
	LockAcquireResult res;
197

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

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

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

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

	return true;
}

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

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

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

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
/*
 *		LockHasWaitersRelation
 *
 * This is a functiion to check if someone else is waiting on a
 * lock, we are currently holding.
 */
bool
LockHasWaitersRelation(Relation relation, LOCKMODE lockmode)
{
	LOCKTAG		tag;

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

	return LockHasWaiters(&tag, lockmode, false);
}

253
/*
254
 *		LockRelationIdForSession
255
 *
B
Bruce Momjian 已提交
256
 * This routine grabs a session-level lock on the target relation.	The
257
 * session lock persists across transaction boundaries.  It will be removed
258
 * when UnlockRelationIdForSession() is called, or if an ereport(ERROR) occurs,
259 260 261 262 263 264 265
 * 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
266
LockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
267 268 269
{
	LOCKTAG		tag;

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

272
	(void) LockAcquire(&tag, lockmode, true, false);
273 274 275
}

/*
276
 *		UnlockRelationIdForSession
277 278
 */
void
279
UnlockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
280 281 282
{
	LOCKTAG		tag;

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

285
	LockRelease(&tag, lockmode, true);
V
Vadim B. Mikheev 已提交
286
}
287

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
/*
 *		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);

307
	(void) LockAcquire(&tag, lockmode, false, false);
308 309 310 311 312 313 314 315 316 317 318 319 320 321
}

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

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

322
	LockRelease(&tag, lockmode, false);
323 324
}

325
/*
V
Vadim B. Mikheev 已提交
326
 *		LockPage
327 328
 *
 * Obtain a page-level lock.  This is currently used by some index access
329
 * methods to lock individual index pages.
330 331
 */
void
V
Vadim B. Mikheev 已提交
332
LockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
333
{
V
Vadim B. Mikheev 已提交
334
	LOCKTAG		tag;
335

336 337 338 339
	SET_LOCKTAG_PAGE(tag,
					 relation->rd_lockInfo.lockRelId.dbId,
					 relation->rd_lockInfo.lockRelId.relId,
					 blkno);
V
Vadim B. Mikheev 已提交
340

341
	(void) LockAcquire(&tag, lockmode, false, false);
V
Vadim B. Mikheev 已提交
342
}
343

344 345 346 347 348 349 350 351 352 353 354
/*
 *		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;

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

360
	return (LockAcquire(&tag, lockmode, false, true) != LOCKACQUIRE_NOT_AVAIL);
361 362
}

363
/*
V
Vadim B. Mikheev 已提交
364
 *		UnlockPage
365 366
 */
void
V
Vadim B. Mikheev 已提交
367
UnlockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
368
{
V
Vadim B. Mikheev 已提交
369
	LOCKTAG		tag;
370

371 372 373 374
	SET_LOCKTAG_PAGE(tag,
					 relation->rd_lockInfo.lockRelId.dbId,
					 relation->rd_lockInfo.lockRelId.relId,
					 blkno);
375

376
	LockRelease(&tag, lockmode, false);
377 378
}

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
/*
 *		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));

397
	(void) LockAcquire(&tag, lockmode, false, false);
398 399
}

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

417
	return (LockAcquire(&tag, lockmode, false, true) != LOCKACQUIRE_NOT_AVAIL);
418 419
}

420 421 422 423 424 425 426 427 428 429 430 431 432 433
/*
 *		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));

434
	LockRelease(&tag, lockmode, false);
435 436
}

437 438 439 440
/*
 *		XactLockTableInsert
 *
 * Insert a lock showing that the given transaction ID is running ---
441 442
 * this is done when an XID is acquired by a transaction or subtransaction.
 * The lock can then be used to wait for the transaction to finish.
443
 */
444
void
V
Vadim B. Mikheev 已提交
445
XactLockTableInsert(TransactionId xid)
446
{
V
Vadim B. Mikheev 已提交
447
	LOCKTAG		tag;
448

449
	SET_LOCKTAG_TRANSACTION(tag, xid);
450

451
	(void) LockAcquire(&tag, ExclusiveLock, false, false);
452 453
}

454 455 456 457 458
/*
 *		XactLockTableDelete
 *
 * Delete the lock showing that the given transaction ID is running.
 * (This is never used for main transaction IDs; those locks are only
459
 * released implicitly at transaction end.	But we do use it for subtrans IDs.)
460 461 462 463 464 465
 */
void
XactLockTableDelete(TransactionId xid)
{
	LOCKTAG		tag;

466
	SET_LOCKTAG_TRANSACTION(tag, xid);
467

468
	LockRelease(&tag, ExclusiveLock, false);
469 470
}

471 472 473 474
/*
 *		XactLockTableWait
 *
 * Wait for the specified transaction to commit or abort.
475
 *
476 477 478 479
 * 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 已提交
480
 * successfully or unsuccessfully.	So we have to check if it's "still running"
481
 * and if so wait for its parent.
482
 */
483
void
V
Vadim B. Mikheev 已提交
484
XactLockTableWait(TransactionId xid)
485
{
V
Vadim B. Mikheev 已提交
486
	LOCKTAG		tag;
487

488 489 490
	for (;;)
	{
		Assert(TransactionIdIsValid(xid));
491
		Assert(!TransactionIdEquals(xid, GetTopTransactionIdIfAny()));
492

493
		SET_LOCKTAG_TRANSACTION(tag, xid);
494

495
		(void) LockAcquire(&tag, ShareLock, false, false);
496

497
		LockRelease(&tag, ShareLock, false);
498

499 500 501 502
		if (!TransactionIdIsInProgress(xid))
			break;
		xid = SubTransGetParent(xid);
	}
503
}
504

505 506 507 508 509 510 511 512 513 514 515 516 517 518
/*
 *		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));
519
		Assert(!TransactionIdEquals(xid, GetTopTransactionIdIfAny()));
520 521 522

		SET_LOCKTAG_TRANSACTION(tag, xid);

523
		if (LockAcquire(&tag, ShareLock, false, true) == LOCKACQUIRE_NOT_AVAIL)
524 525
			return false;

526
		LockRelease(&tag, ShareLock, false);
527 528 529 530 531 532 533 534

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

	return true;
}
535

536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
/*
 * WaitForLockersMultiple
 *		Wait until no transaction holds locks that conflict with the given
 *		locktags at the given lockmode.
 *
 * To do this, obtain the current list of lockers, and wait on their VXIDs
 * until they are finished.
 *
 * Note we don't try to acquire the locks on the given locktags, only the VXIDs
 * of its lock holders; if somebody grabs a conflicting lock on the objects
 * after we obtained our initial list of lockers, we will not wait for them.
 */
void
WaitForLockersMultiple(List *locktags, LOCKMODE lockmode)
{
	List	   *holders = NIL;
	ListCell   *lc;

	/* Done if no locks to wait for */
	if (list_length(locktags) == 0)
		return;

	/* Collect the transactions we need to wait on */
	foreach(lc, locktags)
	{
		LOCKTAG    *locktag = lfirst(lc);

		holders = lappend(holders, GetLockConflicts(locktag, lockmode));
	}

	/*
	 * Note: GetLockConflicts() never reports our own xid, hence we need not
	 * check for that.	Also, prepared xacts are not reported, which is fine
	 * since they certainly aren't going to do anything anymore.
	 */

	/* Finally wait for each such transaction to complete */
	foreach(lc, holders)
	{
		VirtualTransactionId *lockholders = lfirst(lc);

		while (VirtualTransactionIdIsValid(*lockholders))
		{
			VirtualXactLock(*lockholders, true);
			lockholders++;
		}
	}

	list_free_deep(holders);
}

/*
 * WaitForLockers
 *
 * Same as WaitForLockersMultiple, for a single lock tag.
 */
void
WaitForLockers(LOCKTAG heaplocktag, LOCKMODE lockmode)
{
	List   *l;

	l = list_make1(&heaplocktag);
	WaitForLockersMultiple(l, lockmode);
	list_free(l);
}


603 604 605 606
/*
 *		LockDatabaseObject
 *
 * Obtain a lock on a general object of the current database.  Don't use
607 608
 * 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
609
 * locks taken via LockRelation and friends.
610 611 612 613 614 615 616 617 618 619 620 621 622
 */
void
LockDatabaseObject(Oid classid, Oid objid, uint16 objsubid,
				   LOCKMODE lockmode)
{
	LOCKTAG		tag;

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

623
	(void) LockAcquire(&tag, lockmode, false, false);
624 625 626

	/* Make sure syscaches are up-to-date with any changes we waited for */
	AcceptInvalidationMessages();
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
}

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

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

644
	LockRelease(&tag, lockmode, false);
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
}

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

664
	(void) LockAcquire(&tag, lockmode, false, false);
665 666 667

	/* Make sure syscaches are up-to-date with any changes we waited for */
	AcceptInvalidationMessages();
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
}

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

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

685
	LockRelease(&tag, lockmode, false);
686
}
687

688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
/*
 *		LockSharedObjectForSession
 *
 * Obtain a session-level lock on a shared-across-databases object.
 * See LockRelationIdForSession for notes about session-level locks.
 */
void
LockSharedObjectForSession(Oid classid, Oid objid, uint16 objsubid,
						   LOCKMODE lockmode)
{
	LOCKTAG		tag;

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

	(void) LockAcquire(&tag, lockmode, true, false);
}

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

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

	LockRelease(&tag, lockmode, true);
}

727

728 729 730 731 732 733 734 735 736 737
/*
 * Append a description of a lockable object to buf.
 *
 * Ideally we would print names for the numeric values, but that requires
 * getting locks on system tables, which might cause problems since this is
 * typically used to report deadlock situations.
 */
void
DescribeLockTag(StringInfo buf, const LOCKTAG *tag)
{
738
	switch ((LockTagType) tag->locktag_type)
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
	{
		case LOCKTAG_RELATION:
			appendStringInfo(buf,
							 _("relation %u of database %u"),
							 tag->locktag_field2,
							 tag->locktag_field1);
			break;
		case LOCKTAG_RELATION_EXTEND:
			appendStringInfo(buf,
							 _("extension of relation %u of database %u"),
							 tag->locktag_field2,
							 tag->locktag_field1);
			break;
		case LOCKTAG_PAGE:
			appendStringInfo(buf,
							 _("page %u of relation %u of database %u"),
							 tag->locktag_field3,
							 tag->locktag_field2,
							 tag->locktag_field1);
			break;
		case LOCKTAG_TUPLE:
			appendStringInfo(buf,
							 _("tuple (%u,%u) of relation %u of database %u"),
							 tag->locktag_field3,
							 tag->locktag_field4,
							 tag->locktag_field2,
							 tag->locktag_field1);
			break;
		case LOCKTAG_TRANSACTION:
			appendStringInfo(buf,
							 _("transaction %u"),
							 tag->locktag_field1);
			break;
772 773 774 775 776 777
		case LOCKTAG_VIRTUALTRANSACTION:
			appendStringInfo(buf,
							 _("virtual transaction %d/%u"),
							 tag->locktag_field1,
							 tag->locktag_field2);
			break;
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
		case LOCKTAG_OBJECT:
			appendStringInfo(buf,
							 _("object %u of class %u of database %u"),
							 tag->locktag_field3,
							 tag->locktag_field2,
							 tag->locktag_field1);
			break;
		case LOCKTAG_USERLOCK:
			/* reserved for old contrib code, now on pgfoundry */
			appendStringInfo(buf,
							 _("user lock [%u,%u,%u]"),
							 tag->locktag_field1,
							 tag->locktag_field2,
							 tag->locktag_field3);
			break;
		case LOCKTAG_ADVISORY:
			appendStringInfo(buf,
							 _("advisory lock [%u,%u,%u,%u]"),
							 tag->locktag_field1,
							 tag->locktag_field2,
							 tag->locktag_field3,
							 tag->locktag_field4);
			break;
		default:
			appendStringInfo(buf,
							 _("unrecognized locktag type %d"),
804
							 (int) tag->locktag_type);
805 806 807
			break;
	}
}