1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* rewriteSupport.c
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2001-01-23 05:32:23 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteSupport.c,v 1.46 2001/01/23 04:32:23 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
|
|
|
|
#include "access/heapam.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "catalog/catname.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "catalog/indexing.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "rewrite/rewriteSupport.h"
|
1999-07-16 05:14:30 +02:00
|
|
|
#include "utils/syscache.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
bool
|
1996-07-09 08:22:35 +02:00
|
|
|
IsDefinedRewriteRule(char *ruleName)
|
|
|
|
{
|
2000-11-16 23:30:52 +01:00
|
|
|
return SearchSysCacheExists(RULENAME,
|
1998-09-01 06:40:42 +02:00
|
|
|
PointerGetDatum(ruleName),
|
|
|
|
0, 0, 0);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-06-30 09:04:23 +02:00
|
|
|
/*
|
2000-09-29 20:21:41 +02:00
|
|
|
* SetRelationRuleStatus
|
|
|
|
* Set the value of the relation's relhasrules field in pg_class;
|
|
|
|
* if the relation is becoming a view, also adjust its relkind.
|
2000-06-30 09:04:23 +02:00
|
|
|
*
|
2000-09-29 20:21:41 +02:00
|
|
|
* NOTE: caller must be holding an appropriate lock on the relation.
|
2000-06-30 09:04:23 +02:00
|
|
|
*
|
|
|
|
* 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
|
2000-09-29 20:21:41 +02:00
|
|
|
SetRelationRuleStatus(Oid relationId, bool relHasRules,
|
|
|
|
bool relIsBecomingView)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Relation relationRelation;
|
|
|
|
HeapTuple tuple;
|
|
|
|
Relation idescs[Num_pg_class_indices];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
2000-06-30 09:04:23 +02:00
|
|
|
* Find the tuple to update in pg_class, using syscache for the lookup.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1999-09-18 21:08:25 +02:00
|
|
|
relationRelation = heap_openr(RelationRelationName, RowExclusiveLock);
|
2000-11-16 23:30:52 +01:00
|
|
|
tuple = SearchSysCacheCopy(RELOID,
|
|
|
|
ObjectIdGetDatum(relationId),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
|
|
|
elog(ERROR, "SetRelationRuleStatus: cache lookup failed for relation %u", relationId);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-30 09:04:23 +02:00
|
|
|
/* Do the update */
|
2000-09-29 20:21:41 +02:00
|
|
|
((Form_pg_class) GETSTRUCT(tuple))->relhasrules = relHasRules;
|
|
|
|
if (relIsBecomingView)
|
|
|
|
((Form_pg_class) GETSTRUCT(tuple))->relkind = RELKIND_VIEW;
|
|
|
|
|
2001-01-23 05:32:23 +01:00
|
|
|
simple_heap_update(relationRelation, &tuple->t_self, tuple);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-30 09:04:23 +02:00
|
|
|
/* Keep the catalog indices up to date */
|
1997-09-07 07:04:48 +02:00
|
|
|
CatalogOpenIndices(Num_pg_class_indices, Name_pg_class_indices, idescs);
|
1998-08-19 04:04:17 +02:00
|
|
|
CatalogIndexInsert(idescs, Num_pg_class_indices, relationRelation, tuple);
|
1997-09-07 07:04:48 +02:00
|
|
|
CatalogCloseIndices(Num_pg_class_indices, idescs);
|
|
|
|
|
1999-12-16 23:20:03 +01:00
|
|
|
heap_freetuple(tuple);
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(relationRelation, RowExclusiveLock);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|