syscache.c 14.4 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * syscache.c--
4
 *	  System cache management routines
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
10
 *	  $Header: /cvsroot/pgsql/src/backend/utils/cache/syscache.c,v 1.13 1998/01/31 04:38:54 momjian Exp $
11 12
 *
 * NOTES
13 14
 *	  These routines allow the parser/planner/executor to perform
 *	  rapid lookups on the contents of the system catalogs.
15
 *
16
 *	  see catalog/syscache.h for a list of the cache id's
17 18 19
 *
 *-------------------------------------------------------------------------
 */
20
#include "postgres.h"
21

22 23 24 25
#include "access/heapam.h"
#include "access/htup.h"
#include "catalog/catname.h"
#include "utils/catcache.h"
26
#ifndef HAVE_MEMMOVE
27
#include <regex/utils.h>
28
#else
29
#include <string.h>
30 31
#endif

32

33
/* ----------------
34
 *		hardwired attribute information comes from system catalog files.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
 * ----------------
 */
#include "catalog/pg_am.h"
#include "catalog/pg_amop.h"
#include "catalog/pg_attribute.h"
#include "catalog/pg_group.h"
#include "catalog/pg_index.h"
#include "catalog/pg_inherits.h"
#include "catalog/pg_language.h"
#include "catalog/pg_opclass.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_class.h"
#include "catalog/pg_type.h"
#include "catalog/pg_rewrite.h"
#include "catalog/pg_aggregate.h"
#include "catalog/pg_user.h"
#include "storage/large_object.h"
#include "catalog/pg_listener.h"
54

55
extern bool AMI_OVERRIDE;		/* XXX style */
56

57 58
#include "utils/syscache.h"
#include "catalog/indexing.h"
59

60
typedef		HeapTuple(*ScanFunc) ();
61 62

/* ----------------
63 64
 *		Warning:  cacheinfo[] below is changed, then be sure and
 *		update the magic constants in syscache.h!
65 66 67
 * ----------------
 */
static struct cachedesc cacheinfo[] = {
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	{AccessMethodOperatorRelationName,	/* AMOPOPID */
		3,
		{Anum_pg_amop_amopclaid,
			Anum_pg_amop_amopopr,
			Anum_pg_amop_amopid,
		0},
		sizeof(FormData_pg_amop),
		NULL,
	(ScanFunc) NULL},
	{AccessMethodOperatorRelationName,	/* AMOPSTRATEGY */
		3,
		{Anum_pg_amop_amopid,
			Anum_pg_amop_amopclaid,
			Anum_pg_amop_amopstrategy,
		0},
		sizeof(FormData_pg_amop),
		NULL,
	(ScanFunc) NULL},
	{AttributeRelationName,		/* ATTNAME */
		2,
		{Anum_pg_attribute_attrelid,
			Anum_pg_attribute_attname,
			0,
		0},
		ATTRIBUTE_TUPLE_SIZE,
		AttributeNameIndex,
	(ScanFunc) AttributeNameIndexScan},
	{AttributeRelationName,		/* ATTNUM */
		2,
		{Anum_pg_attribute_attrelid,
			Anum_pg_attribute_attnum,
			0,
		0},
		ATTRIBUTE_TUPLE_SIZE,
		AttributeNumIndex,
	(ScanFunc) AttributeNumIndexScan},
	{IndexRelationName,			/* INDEXRELID */
		1,
		{Anum_pg_index_indexrelid,
			0,
			0,
		0},
		offsetof(FormData_pg_index, indpred),
		NULL,
	NULL},
	{LanguageRelationName,		/* LANNAME */
		1,
		{Anum_pg_language_lanname,
			0,
			0,
		0},
		offsetof(FormData_pg_language, lancompiler),
		NULL,
	NULL},
	{OperatorRelationName,		/* OPRNAME */
		4,
		{Anum_pg_operator_oprname,
			Anum_pg_operator_oprleft,
			Anum_pg_operator_oprright,
		Anum_pg_operator_oprkind},
		sizeof(FormData_pg_operator),
		NULL,
	NULL},
	{OperatorRelationName,		/* OPROID */
		1,
		{ObjectIdAttributeNumber,
			0,
			0,
		0},
		sizeof(FormData_pg_operator),
		NULL,
	(ScanFunc) NULL},
	{ProcedureRelationName,		/* PRONAME */
		3,
		{Anum_pg_proc_proname,
			Anum_pg_proc_pronargs,
			Anum_pg_proc_proargtypes,
		0},
		offsetof(FormData_pg_proc, prosrc),
		ProcedureNameIndex,
	(ScanFunc) ProcedureNameIndexScan},
	{ProcedureRelationName,		/* PROOID */
		1,
		{ObjectIdAttributeNumber,
			0,
			0,
		0},
		offsetof(FormData_pg_proc, prosrc),
		ProcedureOidIndex,
	(ScanFunc) ProcedureOidIndexScan},
	{RelationRelationName,		/* RELNAME */
		1,
		{Anum_pg_class_relname,
			0,
			0,
		0},
		CLASS_TUPLE_SIZE,
		ClassNameIndex,
	(ScanFunc) ClassNameIndexScan},
	{RelationRelationName,		/* RELOID */
		1,
		{ObjectIdAttributeNumber,
			0,
			0,
		0},
		CLASS_TUPLE_SIZE,
		ClassOidIndex,
	(ScanFunc) ClassOidIndexScan},
	{TypeRelationName,			/* TYPNAME */
		1,
		{Anum_pg_type_typname,
			0,
			0,
		0},
182
		offsetof(TypeTupleFormData, typalign) + sizeof(char),
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
		TypeNameIndex,
	TypeNameIndexScan},
	{TypeRelationName,			/* TYPOID */
		1,
		{ObjectIdAttributeNumber,
			0,
			0,
		0},
		offsetof(TypeTupleFormData, typalign) +sizeof(char),
		TypeOidIndex,
	TypeOidIndexScan},
	{AccessMethodRelationName,	/* AMNAME */
		1,
		{Anum_pg_am_amname,
			0,
			0,
		0},
		sizeof(FormData_pg_am),
		NULL,
	NULL},
	{OperatorClassRelationName, /* CLANAME */
		1,
		{Anum_pg_opclass_opcname,
			0,
			0,
		0},
		sizeof(FormData_pg_opclass),
		NULL,
	NULL},
	{IndexRelationName,			/* INDRELIDKEY */
		2,
		{Anum_pg_index_indrelid,
			Anum_pg_index_indkey,
			0,
		0},
		offsetof(FormData_pg_index, indpred),
		NULL,
	(ScanFunc) NULL},
	{InheritsRelationName,		/* INHRELID */
		2,
		{Anum_pg_inherits_inhrel,
			Anum_pg_inherits_inhseqno,
			0,
226
		0},
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
		sizeof(FormData_pg_inherits),
		NULL,
	(ScanFunc) NULL},
	{RewriteRelationName,		/* RULOID */
		1,
		{ObjectIdAttributeNumber,
			0,
			0,
		0},
		offsetof(FormData_pg_rewrite, ev_qual),
		NULL,
	(ScanFunc) NULL},
	{AggregateRelationName,		/* AGGNAME */
		2,
		{Anum_pg_aggregate_aggname,
			Anum_pg_aggregate_aggbasetype,
			0,
244
		0},
245 246 247 248 249 250 251 252
		offsetof(FormData_pg_aggregate, agginitval1),
		NULL,
	(ScanFunc) NULL},
	{ListenerRelationName,		/* LISTENREL */
		2,
		{Anum_pg_listener_relname,
			Anum_pg_listener_pid,
			0,
253
		0},
254 255 256 257 258 259 260 261
		sizeof(FormData_pg_listener),
		NULL,
	(ScanFunc) NULL},
	{UserRelationName,			/* USENAME */
		1,
		{Anum_pg_user_usename,
			0,
			0,
262
		0},
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
		sizeof(FormData_pg_user),
		NULL,
	(ScanFunc) NULL},
	{UserRelationName,			/* USESYSID */
		1,
		{Anum_pg_user_usesysid,
			0,
			0,
		0},
		sizeof(FormData_pg_user),
		NULL,
	(ScanFunc) NULL},
	{GroupRelationName,			/* GRONAME */
		1,
		{Anum_pg_group_groname,
			0,
			0,
		0},
		offsetof(FormData_pg_group, grolist[0]),
		NULL,
	(ScanFunc) NULL},
	{GroupRelationName,			/* GROSYSID */
		1,
		{Anum_pg_group_grosysid,
			0,
			0,
289
		0},
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
		offsetof(FormData_pg_group, grolist[0]),
		NULL,
	(ScanFunc) NULL},
	{RewriteRelationName,		/* REWRITENAME */
		1,
		{Anum_pg_rewrite_rulename,
			0,
			0,
		0},
		offsetof(FormData_pg_rewrite, ev_qual),
		NULL,
	(ScanFunc) NULL},
	{ProcedureRelationName,		/* PROSRC */
		1,
		{Anum_pg_proc_prosrc,
			0,
			0,
		0},
		offsetof(FormData_pg_proc, prosrc),
		ProcedureSrcIndex,
	(ScanFunc) ProcedureSrcIndexScan},
	{OperatorClassRelationName, /* CLADEFTYPE */
		1,
		{Anum_pg_opclass_opcdeftype,
			0,
			0,
		0},
		sizeof(FormData_pg_opclass),
		NULL,
319 320 321 322 323 324 325 326 327 328
	(ScanFunc) NULL},
	{LanguageRelationName,		/* LANOID */
		1,
		{ObjectIdAttributeNumber,
			0,
			0,
		0},
		offsetof(FormData_pg_language, lancompiler),
		NULL,
	NULL}
329
};
330 331 332

static struct catcache *SysCache[
								 lengthof(cacheinfo)];
333
static int32 SysCacheSize = lengthof(cacheinfo);
334 335


336 337 338
/*
 * zerocaches--
 *
339
 *	  Make sure the SysCache structure is zero'd.
340 341 342 343
 */
void
zerocaches()
{
Bruce Momjian's avatar
Bruce Momjian committed
344
	MemSet((char *) SysCache, 0, SysCacheSize * sizeof(struct catcache *));
345 346 347 348
}

/*
 * Note:
349 350 351
 *		This function was written because the initialized catalog caches
 *		are used to determine which caches may contain tuples which need
 *		to be invalidated in other backends.
352 353 354 355
 */
void
InitCatalogCache()
{
356
	int			cacheId;		/* XXX type */
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

	if (!AMI_OVERRIDE)
	{
		for (cacheId = 0; cacheId < SysCacheSize; cacheId += 1)
		{

			Assert(!PointerIsValid((Pointer) SysCache[cacheId]));

			SysCache[cacheId] =
				InitSysCache(cacheinfo[cacheId].name,
							 cacheinfo[cacheId].indname,
							 cacheId,
							 cacheinfo[cacheId].nkeys,
							 cacheinfo[cacheId].key,
							 cacheinfo[cacheId].iScanFunc);
			if (!PointerIsValid((char *) SysCache[cacheId]))
			{
374
				elog(ERROR,
375 376 377 378 379 380
					 "InitCatalogCache: Can't init cache %.16s(%d)",
					 cacheinfo[cacheId].name,
					 cacheId);
			}

		}
381 382 383 384 385 386
	}
}

/*
 * SearchSysCacheTuple--
 *
387 388
 *	  A layer on top of SearchSysCache that does the initialization and
 *	  key-setting for you.
389 390 391 392 393 394
 *
 * Returns the tuple if one is found, NULL if not.
 *
 * XXX The tuple that is returned is NOT supposed to be pfree'd!
 */
HeapTuple
395
SearchSysCacheTuple(int cacheId,		/* cache selection code */
396 397 398 399
					Datum key1,
					Datum key2,
					Datum key3,
					Datum key4)
400
{
401 402 403 404
	register HeapTuple tp;

	if (cacheId < 0 || cacheId >= SysCacheSize)
	{
405
		elog(ERROR, "SearchSysCacheTuple: Bad cache id %d", cacheId);
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
		return ((HeapTuple) NULL);
	}

	if (!AMI_OVERRIDE)
	{
		Assert(PointerIsValid(SysCache[cacheId]));
	}
	else
	{
		if (!PointerIsValid(SysCache[cacheId]))
		{
			SysCache[cacheId] =
				InitSysCache(cacheinfo[cacheId].name,
							 cacheinfo[cacheId].indname,
							 cacheId,
							 cacheinfo[cacheId].nkeys,
							 cacheinfo[cacheId].key,
							 cacheinfo[cacheId].iScanFunc);
			if (!PointerIsValid(SysCache[cacheId]))
			{
426
				elog(ERROR,
427 428 429 430 431 432
					 "InitCatalogCache: Can't init cache %.16s(%d)",
					 cacheinfo[cacheId].name,
					 cacheId);
			}

		}
433
	}
434 435 436 437

	tp = SearchSysCache(SysCache[cacheId], key1, key2, key3, key4);
	if (!HeapTupleIsValid(tp))
	{
438
#ifdef CACHEDEBUG
439 440 441 442
		elog(DEBUG,
			 "SearchSysCacheTuple: Search %s(%d) %d %d %d %d failed",
			 (*cacheinfo[cacheId].name)->data,
			 cacheId, key1, key2, key3, key4);
443
#endif
444 445 446
		return ((HeapTuple) NULL);
	}
	return (tp);
447 448 449 450
}

/*
 * SearchSysCacheStruct--
451 452 453
 *	  Fills 's' with the information retrieved by calling SearchSysCache()
 *	  with arguments key1...key4.  Retrieves only the portion of the tuple
 *	  which is not variable-length.
454 455
 *
 * NOTE: we are assuming that non-variable-length fields in the system
456
 *		 catalogs will always be defined!
457 458 459 460 461
 *
 * Returns 1L if a tuple was found, 0L if not.
 */
int32
SearchSysCacheStruct(int cacheId,		/* cache selection code */
462 463 464 465 466
					 char *returnStruct,		/* (preallocated!) */
					 Datum key1,
					 Datum key2,
					 Datum key3,
					 Datum key4)
467
{
468
	HeapTuple	tp;
469 470 471

	if (!PointerIsValid(returnStruct))
	{
472
		elog(ERROR, "SearchSysCacheStruct: No receiving struct");
473 474 475 476 477 478 479
		return (0);
	}
	tp = SearchSysCacheTuple(cacheId, key1, key2, key3, key4);
	if (!HeapTupleIsValid(tp))
		return (0);
	memmove(returnStruct, (char *) GETSTRUCT(tp), cacheinfo[cacheId].size);
	return (1);
480 481 482 483 484
}


/*
 * SearchSysCacheGetAttribute--
485 486
 *	  Returns the attribute corresponding to 'attributeNumber' for
 *	  a given cached tuple.
487 488 489 490 491
 *
 * XXX This re-opens a relation, so this is slower.
 *
 * [callers all assume this returns a (struct varlena *). -ay 10/94]
 */
492
void	   *
493
SearchSysCacheGetAttribute(int cacheId,
494 495 496 497 498
						   AttrNumber attributeNumber,
						   Datum key1,
						   Datum key2,
						   Datum key3,
						   Datum key4)
499
{
500 501 502 503 504 505
	HeapTuple	tp;
	char	   *cacheName;
	Relation	relation;
	int32		attributeLength,
				attributeByValue;
	bool		isNull;
506
	Datum		attributeValue;
507
	void	   *returnValue;
508 509 510 511 512 513

	tp = SearchSysCacheTuple(cacheId, key1, key2, key3, key4);
	cacheName = cacheinfo[cacheId].name;

	if (!HeapTupleIsValid(tp))
	{
514
#ifdef	CACHEDEBUG
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
		elog(DEBUG,
			 "SearchSysCacheGetAttribute: Lookup in %s(%d) failed",
			 cacheName, cacheId);
#endif							/* defined(CACHEDEBUG) */
		return (NULL);
	}

	relation = heap_openr(cacheName);

	if (attributeNumber < 0 &&
		attributeNumber > FirstLowInvalidHeapAttributeNumber)
	{
		attributeLength = heap_sysattrlen(attributeNumber);
		attributeByValue = heap_sysattrbyval(attributeNumber);
	}
	else if (attributeNumber > 0 &&
			 attributeNumber <= relation->rd_rel->relnatts)
	{
		attributeLength =
			relation->rd_att->attrs[attributeNumber - 1]->attlen;
		attributeByValue =
			relation->rd_att->attrs[attributeNumber - 1]->attbyval;
	}
	else
	{
540
		elog(ERROR,
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
			 "SearchSysCacheGetAttribute: Bad attr # %d in %s(%d)",
			 attributeNumber, cacheName, cacheId);
		return (NULL);
	}

	attributeValue = heap_getattr(tp,
								  attributeNumber,
								  RelationGetTupleDescriptor(relation),
								  &isNull);

	if (isNull)
	{

		/*
		 * Used to be an elog(DEBUG, ...) here and a claim that it should
		 * be a FATAL error, I don't think either is warranted -mer 6/9/92
		 */
		return (NULL);
	}

	if (attributeByValue)
	{
		returnValue = (void *) attributeValue;
	}
	else
	{
567 568
		char	   *tmp;
		int			size = (attributeLength < 0)
569 570 571 572
		? VARSIZE((struct varlena *) attributeValue)	/* variable length */
		: attributeLength;		/* fixed length */

		tmp = (char *) palloc(size);
573
		memmove(tmp, (void *) attributeValue, size);
574 575 576 577 578
		returnValue = (void *) tmp;
	}

	heap_close(relation);
	return (returnValue);
579 580 581 582 583
}

/*
 * TypeDefaultRetrieve--
 *
584 585 586
 *	  Given a type OID, return the typdefault field associated with that
 *	  type.  The typdefault is returned as the car of a dotted pair which
 *	  is passed to TypeDefaultRetrieve by the calling routine.
587 588 589 590 591
 *
 * Returns a fixnum for types which are passed by value and a ppreserve'd
 * vectori for types which are not.
 *
 * [identical to get_typdefault, expecting a (struct varlena *) as ret val.
592
 *	some day, either of the functions should be removed		 -ay 10/94]
593
 */
594
void	   *
595 596
TypeDefaultRetrieve(Oid typId)
{
597 598 599 600
	HeapTuple	typeTuple;
	TypeTupleForm type;
	int32		typByVal,
				typLen;
601
	struct varlena *typDefault;
602 603
	int32		dataSize;
	void	   *returnValue;
604 605 606 607 608 609 610

	typeTuple = SearchSysCacheTuple(TYPOID,
									ObjectIdGetDatum(typId),
									0, 0, 0);

	if (!HeapTupleIsValid(typeTuple))
	{
611
#ifdef	CACHEDEBUG
612 613 614 615
		elog(DEBUG, "TypeDefaultRetrieve: Lookup in %s(%d) failed",
			 (*cacheinfo[TYPOID].name)->data, TYPOID);
#endif							/* defined(CACHEDEBUG) */
		return (NULL);
616
	}
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635

	type = (TypeTupleForm) GETSTRUCT(typeTuple);
	typByVal = type->typbyval;
	typLen = type->typlen;

	typDefault = (struct varlena *)
		SearchSysCacheGetAttribute(TYPOID,
								   Anum_pg_type_typdefault,
								   ObjectIdGetDatum(typId),
								   0, 0, 0);

	if (typDefault == (struct varlena *) NULL)
	{
#ifdef	CACHEDEBUG
		elog(DEBUG, "TypeDefaultRetrieve: No extractable typdefault",
			 (*cacheinfo[TYPOID].name)->data, TYPOID);
#endif							/* defined(CACHEDEBUG) */
		return (NULL);

636 637
	}

638
	dataSize = VARSIZE(typDefault) - VARHDRSZ;
639

640 641
	if (typByVal)
	{
642 643 644
		int8		i8;
		int16		i16;
		int32		i32;
645 646 647 648 649

		if (dataSize == typLen)
		{
			switch (typLen)
			{
650 651 652 653 654 655 656 657 658 659 660
				case sizeof(int8):
					memmove((char *) &i8, VARDATA(typDefault), sizeof(int8));
					i32 = i8;
					break;
				case sizeof(int16):
					memmove((char *) &i16, VARDATA(typDefault), sizeof(int16));
					i32 = i16;
					break;
				case sizeof(int32):
					memmove((char *) &i32, VARDATA(typDefault), sizeof(int32));
					break;
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
			}
			returnValue = (void *) i32;
		}
		else
		{
			returnValue = NULL;
		}
	}
	else
	{
		if ((typLen < 0 && dataSize < 0) || dataSize != typLen)
			returnValue = NULL;
		else
		{
			returnValue = (void *) palloc(VARSIZE(typDefault));
			memmove((char *) returnValue,
					(char *) typDefault,
					(int) VARSIZE(typDefault));
		}
	}

	return (returnValue);
}