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
|
|
|
*
|
2013-01-01 23:15:01 +01:00
|
|
|
* Portions Copyright (c) 1996-2013, 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
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/rewrite/rewriteSupport.c
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
|
|
|
|
#include "access/heapam.h"
|
2012-08-30 22:15:44 +02:00
|
|
|
#include "access/htup_details.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "catalog/indexing.h"
|
2010-08-05 17:25:36 +02:00
|
|
|
#include "catalog/pg_rewrite.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "rewrite/rewriteSupport.h"
|
2010-08-05 17:25:36 +02:00
|
|
|
#include "utils/fmgroids.h"
|
2002-07-12 20:43:19 +02:00
|
|
|
#include "utils/inval.h"
|
2010-08-05 17:25:36 +02:00
|
|
|
#include "utils/lsyscache.h"
|
|
|
|
#include "utils/rel.h"
|
1999-07-16 05:14:30 +02:00
|
|
|
#include "utils/syscache.h"
|
2010-08-05 17:25:36 +02:00
|
|
|
#include "utils/tqual.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
|
|
|
{
|
2010-02-14 19:42:19 +01:00
|
|
|
return SearchSysCacheExists2(RULERELNAME,
|
|
|
|
ObjectIdGetDatum(owningRel),
|
|
|
|
PointerGetDatum(ruleName));
|
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
|
2013-03-04 01:05:47 +01:00
|
|
|
* Set the value of the relation's relhasrules field in pg_class.
|
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
|
2013-03-04 01:05:47 +01:00
|
|
|
SetRelationRuleStatus(Oid relationId, bool relHasRules)
|
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
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Find the tuple to update in pg_class, using syscache for the lookup.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2005-04-14 22:03:27 +02:00
|
|
|
relationRelation = heap_open(RelationRelationId, RowExclusiveLock);
|
2010-02-14 19:42:19 +01:00
|
|
|
tuple = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relationId));
|
2000-11-16 23:30:52 +01:00
|
|
|
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
|
|
|
|
2013-03-04 01:05:47 +01:00
|
|
|
if (classForm->relhasrules != relHasRules)
|
2002-07-12 20:43:19 +02:00
|
|
|
{
|
|
|
|
/* Do the update */
|
|
|
|
classForm->relhasrules = relHasRules;
|
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 */
|
2004-02-10 02:55:27 +01:00
|
|
|
CacheInvalidateRelcacheByTuple(tuple);
|
2002-07-12 20:43:19 +02:00
|
|
|
}
|
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
|
|
|
}
|
2010-08-05 17:25:36 +02: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
|
2011-04-10 17:42:00 +02:00
|
|
|
* error, too. In general, this should be avoided - it is provided to support
|
2010-08-05 17:25:36 +02:00
|
|
|
* syntax that is compatible with pre-7.3 versions of PG, where rule names
|
|
|
|
* were unique across the entire database.
|
|
|
|
*/
|
|
|
|
Oid
|
2011-06-28 03:17:25 +02:00
|
|
|
get_rewrite_oid_without_relid(const char *rulename,
|
|
|
|
Oid *reloid, bool missing_ok)
|
2010-08-05 17:25:36 +02:00
|
|
|
{
|
|
|
|
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))
|
2011-06-28 03:17:25 +02:00
|
|
|
{
|
|
|
|
if (!missing_ok)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("rule \"%s\" does not exist", rulename)));
|
|
|
|
ruleoid = InvalidOid;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ruleoid = HeapTupleGetOid(htup);
|
|
|
|
if (reloid != NULL)
|
|
|
|
*reloid = ((Form_pg_rewrite) GETSTRUCT(htup))->ev_class;
|
|
|
|
|
|
|
|
htup = heap_getnext(scanDesc, ForwardScanDirection);
|
|
|
|
if (HeapTupleIsValid(htup))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DUPLICATE_OBJECT),
|
2012-06-10 21:20:04 +02:00
|
|
|
errmsg("there are multiple rules named \"%s\"", rulename),
|
|
|
|
errhint("Specify a relation name as well as a rule name.")));
|
2011-06-28 03:17:25 +02:00
|
|
|
}
|
2010-08-05 17:25:36 +02:00
|
|
|
heap_endscan(scanDesc);
|
|
|
|
heap_close(RewriteRelation, AccessShareLock);
|
|
|
|
|
|
|
|
return ruleoid;
|
|
|
|
}
|