PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
rewriteSupport.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define ViewSelectRuleName   "_RETURN"
 

Functions

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)
 
Oid get_rewrite_oid_without_relid (const char *rulename, Oid *relid, bool missing_ok)
 

Macro Definition Documentation

#define ViewSelectRuleName   "_RETURN"

Definition at line 18 of file rewriteSupport.h.

Referenced by DefineQueryRewrite(), DefineViewRules(), and pg_get_viewdef_worker().

Function Documentation

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;
98 
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,
108  (errcode(ERRCODE_UNDEFINED_OBJECT),
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 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define PointerGetDatum(X)
Definition: postgres.h:564
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:670
FormData_pg_rewrite * Form_pg_rewrite
Definition: pg_rewrite.h:53
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1694
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:151
Oid get_rewrite_oid_without_relid ( const char *  rulename,
Oid relid,
bool  missing_ok 
)

Definition at line 127 of file rewriteSupport.c.

References AccessShareLock, Anum_pg_rewrite_rulename, BTEqualStrategyNumber, CStringGetDatum, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errhint(), errmsg(), ERROR, ForwardScanDirection, GETSTRUCT, heap_beginscan_catalog(), heap_close, heap_endscan(), heap_getnext(), heap_open(), HeapTupleGetOid, HeapTupleIsValid, InvalidOid, NULL, RewriteRelationId, and ScanKeyInit().

Referenced by get_object_address_relobject().

129 {
130  Relation RewriteRelation;
131  HeapScanDesc scanDesc;
132  ScanKeyData scanKeyData;
133  HeapTuple htup;
134  Oid ruleoid;
135 
136  /* Search pg_rewrite for such a rule */
137  ScanKeyInit(&scanKeyData,
139  BTEqualStrategyNumber, F_NAMEEQ,
140  CStringGetDatum(rulename));
141 
142  RewriteRelation = heap_open(RewriteRelationId, AccessShareLock);
143  scanDesc = heap_beginscan_catalog(RewriteRelation, 1, &scanKeyData);
144 
145  htup = heap_getnext(scanDesc, ForwardScanDirection);
146  if (!HeapTupleIsValid(htup))
147  {
148  if (!missing_ok)
149  ereport(ERROR,
150  (errcode(ERRCODE_UNDEFINED_OBJECT),
151  errmsg("rule \"%s\" does not exist", rulename)));
152  ruleoid = InvalidOid;
153  }
154  else
155  {
156  ruleoid = HeapTupleGetOid(htup);
157  if (reloid != NULL)
158  *reloid = ((Form_pg_rewrite) GETSTRUCT(htup))->ev_class;
159 
160  htup = heap_getnext(scanDesc, ForwardScanDirection);
161  if (HeapTupleIsValid(htup))
162  ereport(ERROR,
164  errmsg("there are multiple rules named \"%s\"", rulename),
165  errhint("Specify a relation name as well as a rule name.")));
166  }
167  heap_endscan(scanDesc);
168  heap_close(RewriteRelation, AccessShareLock);
169 
170  return ruleoid;
171 }
int errhint(const char *fmt,...)
Definition: elog.c:987
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
void heap_endscan(HeapScanDesc scan)
Definition: heapam.c:1581
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:575
#define heap_close(r, l)
Definition: heapam.h:97
unsigned int Oid
Definition: postgres_ext.h:31
#define Anum_pg_rewrite_rulename
Definition: pg_rewrite.h:60
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:586
HeapScanDesc heap_beginscan_catalog(Relation relation, int nkeys, ScanKey key)
Definition: heapam.c:1402
#define ereport(elevel, rest)
Definition: elog.h:122
HeapTuple heap_getnext(HeapScanDesc scan, ScanDirection direction)
Definition: heapam.c:1781
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define InvalidOid
Definition: postgres_ext.h:36
#define RewriteRelationId
Definition: pg_rewrite.h:32
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
FormData_pg_rewrite * Form_pg_rewrite
Definition: pg_rewrite.h:53
int errmsg(const char *fmt,...)
Definition: elog.c:797
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:34
#define BTEqualStrategyNumber
Definition: stratnum.h:31
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:564
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition: syscache.h:169
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, RelationRelationId, RELOID, RowExclusiveLock, SearchSysCacheCopy1, and HeapTupleData::t_self.

Referenced by DefineQueryRewrite().

56 {
57  Relation relationRelation;
58  HeapTuple tuple;
59  Form_pg_class classForm;
60 
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);
69 
70  if (classForm->relhasrules != relHasRules)
71  {
72  /* Do the update */
73  classForm->relhasrules = relHasRules;
74 
75  CatalogTupleUpdate(relationRelation, &tuple->t_self, tuple);
76  }
77  else
78  {
79  /* no need to change tuple, but force relcache rebuild anyway */
81  }
82 
83  heap_freetuple(tuple);
84  heap_close(relationRelation, RowExclusiveLock);
85 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define RelationRelationId
Definition: pg_class.h:29
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1374
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#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:1287
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:210
FormData_pg_class * Form_pg_class
Definition: pg_class.h:95
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:158
void CacheInvalidateRelcacheByTuple(HeapTuple classTuple)
Definition: inval.c:1259
#define elog
Definition: elog.h:219