rewriteSupport.c 2.46 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * rewriteSupport.c
4
 *
5
 *
Bruce Momjian's avatar
Add:  
Bruce Momjian committed
6 7
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
11
 *	  $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteSupport.c,v 1.46 2001/01/23 04:32:23 tgl Exp $
12 13 14 15
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"
Bruce Momjian's avatar
Bruce Momjian committed
16 17

#include "access/heapam.h"
18
#include "catalog/catname.h"
Bruce Momjian's avatar
Bruce Momjian committed
19
#include "catalog/indexing.h"
Bruce Momjian's avatar
Bruce Momjian committed
20
#include "rewrite/rewriteSupport.h"
Bruce Momjian's avatar
Bruce Momjian committed
21
#include "utils/syscache.h"
Bruce Momjian's avatar
Bruce Momjian committed
22

23

24
bool
25 26
IsDefinedRewriteRule(char *ruleName)
{
27
	return SearchSysCacheExists(RULENAME,
28 29
								PointerGetDatum(ruleName),
								0, 0, 0);
30 31
}

32
/*
33 34 35
 * SetRelationRuleStatus
 *		Set the value of the relation's relhasrules field in pg_class;
 *		if the relation is becoming a view, also adjust its relkind.
36
 *
37
 * NOTE: caller must be holding an appropriate lock on the relation.
38 39 40 41 42 43 44 45 46
 *
 * NOTE: an important side-effect of this operation is that an SI invalidation
 * message is sent out to all backends --- including me --- causing relcache
 * entries to be flushed or updated with the new set of rules for the table.
 * Therefore, we execute the update even if relhasrules has the right value
 * already.  Possible future improvement: skip the disk update and just send
 * an SI message in that case.
 */
void
47 48
SetRelationRuleStatus(Oid relationId, bool relHasRules,
					  bool relIsBecomingView)
49
{
50 51 52
	Relation	relationRelation;
	HeapTuple	tuple;
	Relation	idescs[Num_pg_class_indices];
53 54

	/*
55
	 * Find the tuple to update in pg_class, using syscache for the lookup.
56
	 */
57
	relationRelation = heap_openr(RelationRelationName, RowExclusiveLock);
58 59 60 61 62
	tuple = SearchSysCacheCopy(RELOID,
							   ObjectIdGetDatum(relationId),
							   0, 0, 0);
	if (!HeapTupleIsValid(tuple))
		elog(ERROR, "SetRelationRuleStatus: cache lookup failed for relation %u", relationId);
63

64
	/* Do the update */
65 66 67 68
	((Form_pg_class) GETSTRUCT(tuple))->relhasrules = relHasRules;
	if (relIsBecomingView)
		((Form_pg_class) GETSTRUCT(tuple))->relkind = RELKIND_VIEW;

69
	simple_heap_update(relationRelation, &tuple->t_self, tuple);
70

71
	/* Keep the catalog indices up to date */
72
	CatalogOpenIndices(Num_pg_class_indices, Name_pg_class_indices, idescs);
73
	CatalogIndexInsert(idescs, Num_pg_class_indices, relationRelation, tuple);
74 75
	CatalogCloseIndices(Num_pg_class_indices, idescs);

76
	heap_freetuple(tuple);
77
	heap_close(relationRelation, RowExclusiveLock);
78
}