PostgreSQL Source Code  git master
rewriteSupport.c File Reference
#include "postgres.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "catalog/indexing.h"
#include "catalog/pg_rewrite.h"
#include "rewrite/rewriteSupport.h"
#include "utils/fmgroids.h"
#include "utils/inval.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/tqual.h"
Include dependency graph for rewriteSupport.c:

Go to the source code of this file.


bool IsDefinedRewriteRule (Oid owningRel, const char *ruleName)
void SetRelationRuleStatus (Oid relationId, bool relHasRules)
Oid get_rewrite_oid (Oid relid, const char *rulename, bool missing_ok)

Function Documentation

◆ get_rewrite_oid()

Oid get_rewrite_oid ( Oid  relid,
const char *  rulename,
bool  missing_ok 

Definition at line 94 of file rewriteSupport.c.

References Assert, ereport, errcode(), errmsg(), ERROR, get_rel_name(), GETSTRUCT, HeapTupleGetOid, HeapTupleIsValid, InvalidOid, ObjectIdGetDatum, PointerGetDatum, ReleaseSysCache(), RULERELNAME, and SearchSysCache2().

Referenced by get_object_address_relobject().

95 {
96  HeapTuple tuple;
97  Oid ruleoid;
99  /* Find the rule's pg_rewrite tuple, get its OID */
101  ObjectIdGetDatum(relid),
102  PointerGetDatum(rulename));
103  if (!HeapTupleIsValid(tuple))
104  {
105  if (missing_ok)
106  return InvalidOid;
107  ereport(ERROR,
109  errmsg("rule \"%s\" for relation \"%s\" does not exist",
110  rulename, get_rel_name(relid))));
111  }
112  Assert(relid == ((Form_pg_rewrite) GETSTRUCT(tuple))->ev_class);
113  ruleoid = HeapTupleGetOid(tuple);
114  ReleaseSysCache(tuple);
115  return ruleoid;
116 }
Definition: htup_details.h:668
#define PointerGetDatum(X)
Definition: postgres.h:541
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:699
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1123
FormData_pg_rewrite * Form_pg_rewrite
Definition: pg_rewrite.h:51
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:707
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1730

◆ IsDefinedRewriteRule()

bool IsDefinedRewriteRule ( Oid  owningRel,
const char *  ruleName 

Definition at line 34 of file rewriteSupport.c.

References ObjectIdGetDatum, PointerGetDatum, RULERELNAME, and SearchSysCacheExists2.

Referenced by RenameRewriteRule().

35 {
37  ObjectIdGetDatum(owningRel),
38  PointerGetDatum(ruleName));
39 }
#define PointerGetDatum(X)
Definition: postgres.h:541
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition: syscache.h:184

◆ SetRelationRuleStatus()

void SetRelationRuleStatus ( Oid  relationId,
bool  relHasRules 

Definition at line 55 of file rewriteSupport.c.

References CacheInvalidateRelcacheByTuple(), CatalogTupleUpdate(), elog, ERROR, GETSTRUCT, heap_close, heap_freetuple(), heap_open(), HeapTupleIsValid, ObjectIdGetDatum, RELOID, RowExclusiveLock, SearchSysCacheCopy1, and HeapTupleData::t_self.

Referenced by DefineQueryRewrite().

56 {
57  Relation relationRelation;
58  HeapTuple tuple;
59  Form_pg_class classForm;
61  /*
62  * Find the tuple to update in pg_class, using syscache for the lookup.
63  */
64  relationRelation = heap_open(RelationRelationId, RowExclusiveLock);
65  tuple = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relationId));
66  if (!HeapTupleIsValid(tuple))
67  elog(ERROR, "cache lookup failed for relation %u", relationId);
68  classForm = (Form_pg_class) GETSTRUCT(tuple);
70  if (classForm->relhasrules != relHasRules)
71  {
72  /* Do the update */
73  classForm->relhasrules = relHasRules;
75  CatalogTupleUpdate(relationRelation, &tuple->t_self, tuple);
76  }
77  else
78  {
79  /* no need to change tuple, but force relcache rebuild anyway */
81  }
83  heap_freetuple(tuple);
84  heap_close(relationRelation, RowExclusiveLock);
85 }
Definition: htup_details.h:668
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1773
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:211
FormData_pg_class * Form_pg_class
Definition: pg_class.h:93
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:173
void CacheInvalidateRelcacheByTuple(HeapTuple classTuple)
Definition: inval.c:1269
#define elog
Definition: elog.h:219