1996-07-09 06:22:35 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-13 23:22:53 +00:00
|
|
|
* rewriteSupport.c
|
1997-09-07 05:04:48 +00:00
|
|
|
*
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
2010-01-02 16:58:17 +00:00
|
|
|
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
|
2000-01-26 05:58:53 +00:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-08-05 15:25:36 +00:00
|
|
|
* $PostgreSQL: pgsql/src/backend/rewrite/rewriteSupport.c,v 1.70 2010/08/05 15:25:35 rhaas Exp $
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
1998-04-27 04:08:07 +00:00
|
|
|
|
|
|
|
#include "access/heapam.h"
|
|
|
|
#include "catalog/indexing.h"
|
2008-06-19 00:46:06 +00:00
|
|
|
#include "catalog/pg_class.h"
|
2010-08-05 15:25:36 +00:00
|
|
|
#include "catalog/pg_rewrite.h"
|
1999-07-16 05:00:38 +00:00
|
|
|
#include "rewrite/rewriteSupport.h"
|
2010-08-05 15:25:36 +00:00
|
|
|
#include "utils/fmgroids.h"
|
2002-07-12 18:43:19 +00:00
|
|
|
#include "utils/inval.h"
|
2010-08-05 15:25:36 +00:00
|
|
|
#include "utils/lsyscache.h"
|
|
|
|
#include "utils/rel.h"
|
1999-07-16 03:14:30 +00:00
|
|
|
#include "utils/syscache.h"
|
2010-08-05 15:25:36 +00:00
|
|
|
#include "utils/tqual.h"
|
1998-04-27 04:08:07 +00:00
|
|
|
|
2001-08-12 21:35:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Is there a rule by the given name?
|
|
|
|
*/
|
2000-11-16 22:30:52 +00:00
|
|
|
bool
|
2002-04-18 20:01:11 +00:00
|
|
|
IsDefinedRewriteRule(Oid owningRel, const char *ruleName)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
2010-02-14 18:42:19 +00:00
|
|
|
return SearchSysCacheExists2(RULERELNAME,
|
|
|
|
ObjectIdGetDatum(owningRel),
|
|
|
|
PointerGetDatum(ruleName));
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
|
2001-08-12 21:35:19 +00:00
|
|
|
|
2000-06-30 07:04:23 +00:00
|
|
|
/*
|
2000-09-29 18:21:41 +00: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 07:04:23 +00:00
|
|
|
*
|
2000-09-29 18:21:41 +00:00
|
|
|
* NOTE: caller must be holding an appropriate lock on the relation.
|
2000-06-30 07:04:23 +00: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 18:43:19 +00:00
|
|
|
* This must happen even if we find that no change is needed in the pg_class
|
|
|
|
* row.
|
2000-06-30 07:04:23 +00:00
|
|
|
*/
|
|
|
|
void
|
2000-09-29 18:21:41 +00:00
|
|
|
SetRelationRuleStatus(Oid relationId, bool relHasRules,
|
|
|
|
bool relIsBecomingView)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
1997-09-08 02:41:22 +00:00
|
|
|
Relation relationRelation;
|
|
|
|
HeapTuple tuple;
|
2002-07-12 18:43:19 +00:00
|
|
|
Form_pg_class classForm;
|
1997-09-07 05:04:48 +00:00
|
|
|
|
|
|
|
/*
|
2005-10-15 02:49:52 +00:00
|
|
|
* Find the tuple to update in pg_class, using syscache for the lookup.
|
1997-09-07 05:04:48 +00:00
|
|
|
*/
|
2005-04-14 20:03:27 +00:00
|
|
|
relationRelation = heap_open(RelationRelationId, RowExclusiveLock);
|
2010-02-14 18:42:19 +00:00
|
|
|
tuple = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relationId));
|
2000-11-16 22:30:52 +00:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
2003-07-25 00:01:09 +00:00
|
|
|
elog(ERROR, "cache lookup failed for relation %u", relationId);
|
2002-07-12 18:43:19 +00:00
|
|
|
classForm = (Form_pg_class) GETSTRUCT(tuple);
|
1997-09-07 05:04:48 +00:00
|
|
|
|
2002-07-12 18:43:19 +00: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 18:21:41 +00:00
|
|
|
|
2002-07-12 18:43:19 +00:00
|
|
|
simple_heap_update(relationRelation, &tuple->t_self, tuple);
|
1997-09-07 05:04:48 +00:00
|
|
|
|
2002-08-05 03:29:17 +00:00
|
|
|
/* Keep the catalog indexes up to date */
|
|
|
|
CatalogUpdateIndexes(relationRelation, tuple);
|
2002-07-12 18:43:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* no need to change tuple, but force relcache rebuild anyway */
|
2004-02-10 01:55:27 +00:00
|
|
|
CacheInvalidateRelcacheByTuple(tuple);
|
2002-07-12 18:43:19 +00:00
|
|
|
}
|
1997-09-07 05:04:48 +00:00
|
|
|
|
1999-12-16 22:20:03 +00:00
|
|
|
heap_freetuple(tuple);
|
1999-09-18 19:08:25 +00:00
|
|
|
heap_close(relationRelation, RowExclusiveLock);
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
2010-08-05 15:25:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Find rule oid.
|
|
|
|
*
|
|
|
|
* If missing_ok is false, throw an error if rule name not found. If
|
|
|
|
* true, just return InvalidOid.
|
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
get_rewrite_oid(Oid relid, const char *rulename, bool missing_ok)
|
|
|
|
{
|
|
|
|
HeapTuple tuple;
|
|
|
|
Oid ruleoid;
|
|
|
|
|
|
|
|
/* Find the rule's pg_rewrite tuple, get its OID */
|
|
|
|
tuple = SearchSysCache2(RULERELNAME,
|
|
|
|
ObjectIdGetDatum(relid),
|
|
|
|
PointerGetDatum(rulename));
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
|
|
|
{
|
|
|
|
if (missing_ok)
|
|
|
|
return InvalidOid;
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("rule \"%s\" for relation \"%s\" does not exist",
|
|
|
|
rulename, get_rel_name(relid))));
|
|
|
|
}
|
|
|
|
Assert(relid == ((Form_pg_rewrite) GETSTRUCT(tuple))->ev_class);
|
|
|
|
ruleoid = HeapTupleGetOid(tuple);
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
return ruleoid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find rule oid, given only a rule name but no rel OID.
|
|
|
|
*
|
|
|
|
* If there's more than one, it's an error. If there aren't any, that's an
|
|
|
|
* error, too. In general, this should be avoided - it is provided to support
|
|
|
|
* syntax that is compatible with pre-7.3 versions of PG, where rule names
|
|
|
|
* were unique across the entire database.
|
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
get_rewrite_oid_without_relid(const char *rulename, Oid *reloid)
|
|
|
|
{
|
|
|
|
Relation RewriteRelation;
|
|
|
|
HeapScanDesc scanDesc;
|
|
|
|
ScanKeyData scanKeyData;
|
|
|
|
HeapTuple htup;
|
|
|
|
Oid ruleoid;
|
|
|
|
|
|
|
|
/* Search pg_rewrite for such a rule */
|
|
|
|
ScanKeyInit(&scanKeyData,
|
|
|
|
Anum_pg_rewrite_rulename,
|
|
|
|
BTEqualStrategyNumber, F_NAMEEQ,
|
|
|
|
CStringGetDatum(rulename));
|
|
|
|
|
|
|
|
RewriteRelation = heap_open(RewriteRelationId, AccessShareLock);
|
|
|
|
scanDesc = heap_beginscan(RewriteRelation, SnapshotNow, 1, &scanKeyData);
|
|
|
|
|
|
|
|
htup = heap_getnext(scanDesc, ForwardScanDirection);
|
|
|
|
if (!HeapTupleIsValid(htup))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("rule \"%s\" does not exist", rulename)));
|
|
|
|
|
|
|
|
ruleoid = HeapTupleGetOid(htup);
|
|
|
|
if (reloid != NULL)
|
|
|
|
*reloid = ((Form_pg_rewrite) GETSTRUCT(htup))->ev_class;
|
|
|
|
|
|
|
|
if (HeapTupleIsValid(htup = heap_getnext(scanDesc, ForwardScanDirection)))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DUPLICATE_OBJECT),
|
|
|
|
errmsg("there are multiple rules named \"%s\"", rulename),
|
|
|
|
errhint("Specify a relation name as well as a rule name.")));
|
|
|
|
|
|
|
|
heap_endscan(scanDesc);
|
|
|
|
heap_close(RewriteRelation, AccessShareLock);
|
|
|
|
|
|
|
|
return ruleoid;
|
|
|
|
}
|