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
|
|
|
*
|
2002-06-20 22:29:54 +02:00
|
|
|
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2003-07-25 02:01:09 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteSupport.c,v 1.55 2003/07/25 00:01:09 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"
|
2002-07-12 20:43:19 +02:00
|
|
|
#include "utils/inval.h"
|
1999-07-16 05:14:30 +02:00
|
|
|
#include "utils/syscache.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
|
2001-08-12 23:35:19 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Is there a rule by the given name?
|
|
|
|
*/
|
2000-11-16 23:30:52 +01:00
|
|
|
bool
|
2002-04-18 22:01:11 +02:00
|
|
|
IsDefinedRewriteRule(Oid owningRel, const char *ruleName)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2002-04-18 22:01:11 +02:00
|
|
|
return SearchSysCacheExists(RULERELNAME,
|
|
|
|
ObjectIdGetDatum(owningRel),
|
1998-09-01 06:40:42 +02:00
|
|
|
PointerGetDatum(ruleName),
|
2002-04-18 22:01:11 +02:00
|
|
|
0, 0);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2001-08-12 23:35:19 +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.
|
2002-07-12 20:43:19 +02:00
|
|
|
* This must happen even if we find that no change is needed in the pg_class
|
|
|
|
* row.
|
2000-06-30 09:04:23 +02:00
|
|
|
*/
|
|
|
|
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;
|
2002-07-12 20:43:19 +02:00
|
|
|
Form_pg_class classForm;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
2001-03-22 05:01:46 +01: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))
|
2003-07-25 02:01:09 +02:00
|
|
|
elog(ERROR, "cache lookup failed for relation %u", relationId);
|
2002-07-12 20:43:19 +02:00
|
|
|
classForm = (Form_pg_class) GETSTRUCT(tuple);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
if (classForm->relhasrules != relHasRules ||
|
|
|
|
(relIsBecomingView && classForm->relkind != RELKIND_VIEW))
|
|
|
|
{
|
|
|
|
/* Do the update */
|
|
|
|
classForm->relhasrules = relHasRules;
|
|
|
|
if (relIsBecomingView)
|
|
|
|
classForm->relkind = RELKIND_VIEW;
|
2000-09-29 20:21:41 +02:00
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
simple_heap_update(relationRelation, &tuple->t_self, tuple);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-08-05 05:29:17 +02:00
|
|
|
/* Keep the catalog indexes up to date */
|
|
|
|
CatalogUpdateIndexes(relationRelation, tuple);
|
2002-07-12 20:43:19 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* no need to change tuple, but force relcache rebuild anyway */
|
|
|
|
CacheInvalidateRelcache(relationId);
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
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
|
|
|
}
|