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
|
|
|
*
|
2001-01-24 19:43:33 +00:00
|
|
|
* Portions Copyright (c) 1996-2001, 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
|
2001-08-12 21:35:19 +00:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteSupport.c,v 1.49 2001/08/12 21:35:19 tgl Exp $
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
1998-04-27 04:08:07 +00:00
|
|
|
|
|
|
|
#include "access/heapam.h"
|
1996-07-09 06:22:35 +00:00
|
|
|
#include "catalog/catname.h"
|
1998-04-27 04:08:07 +00:00
|
|
|
#include "catalog/indexing.h"
|
1999-07-16 05:00:38 +00:00
|
|
|
#include "rewrite/rewriteSupport.h"
|
1999-07-16 03:14:30 +00:00
|
|
|
#include "utils/syscache.h"
|
1998-04-27 04:08:07 +00:00
|
|
|
|
2001-08-12 21:35:19 +00:00
|
|
|
#ifdef MULTIBYTE
|
|
|
|
#include "mb/pg_wchar.h"
|
|
|
|
#endif
|
1996-07-09 06:22:35 +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
|
2001-08-12 21:35:19 +00:00
|
|
|
IsDefinedRewriteRule(const char *ruleName)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
2000-11-16 22:30:52 +00:00
|
|
|
return SearchSysCacheExists(RULENAME,
|
1998-09-01 04:40:42 +00:00
|
|
|
PointerGetDatum(ruleName),
|
|
|
|
0, 0, 0);
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
|
2001-08-12 21:35:19 +00:00
|
|
|
/*
|
|
|
|
* makeViewRetrieveRuleName
|
|
|
|
*
|
|
|
|
* Given a view name, returns the name for the associated ON SELECT rule.
|
|
|
|
*
|
|
|
|
* XXX this is not the only place in the backend that knows about the _RET
|
|
|
|
* name-forming convention.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
MakeRetrieveViewRuleName(const char *viewName)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
int buflen,
|
|
|
|
maxlen;
|
|
|
|
|
|
|
|
buflen = strlen(viewName) + 5;
|
|
|
|
buf = palloc(buflen);
|
|
|
|
snprintf(buf, buflen, "_RET%s", viewName);
|
|
|
|
/* clip to less than NAMEDATALEN bytes, if necessary */
|
|
|
|
#ifdef MULTIBYTE
|
|
|
|
maxlen = pg_mbcliplen(buf, strlen(buf), NAMEDATALEN - 1);
|
|
|
|
#else
|
|
|
|
maxlen = NAMEDATALEN - 1;
|
|
|
|
#endif
|
|
|
|
if (maxlen < buflen)
|
|
|
|
buf[maxlen] = '\0';
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
* 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 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;
|
|
|
|
Relation idescs[Num_pg_class_indices];
|
1997-09-07 05:04:48 +00:00
|
|
|
|
|
|
|
/*
|
2001-03-22 04:01:46 +00:00
|
|
|
* Find the tuple to update in pg_class, using syscache for the
|
|
|
|
* lookup.
|
1997-09-07 05:04:48 +00:00
|
|
|
*/
|
1999-09-18 19:08:25 +00:00
|
|
|
relationRelation = heap_openr(RelationRelationName, RowExclusiveLock);
|
2000-11-16 22:30:52 +00: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 05:04:48 +00:00
|
|
|
|
2000-06-30 07:04:23 +00:00
|
|
|
/* Do the update */
|
2000-09-29 18:21:41 +00:00
|
|
|
((Form_pg_class) GETSTRUCT(tuple))->relhasrules = relHasRules;
|
|
|
|
if (relIsBecomingView)
|
|
|
|
((Form_pg_class) GETSTRUCT(tuple))->relkind = RELKIND_VIEW;
|
|
|
|
|
2001-01-23 04:32:23 +00:00
|
|
|
simple_heap_update(relationRelation, &tuple->t_self, tuple);
|
1997-09-07 05:04:48 +00:00
|
|
|
|
2000-06-30 07:04:23 +00:00
|
|
|
/* Keep the catalog indices up to date */
|
1997-09-07 05:04:48 +00:00
|
|
|
CatalogOpenIndices(Num_pg_class_indices, Name_pg_class_indices, idescs);
|
1998-08-19 02:04:17 +00:00
|
|
|
CatalogIndexInsert(idescs, Num_pg_class_indices, relationRelation, tuple);
|
1997-09-07 05:04:48 +00:00
|
|
|
CatalogCloseIndices(Num_pg_class_indices, idescs);
|
|
|
|
|
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
|
|
|
}
|