PostgreSQL Source Code  git master
rewriteRemove.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/htup_details.h"
#include "access/table.h"
#include "catalog/catalog.h"
#include "catalog/indexing.h"
#include "catalog/pg_rewrite.h"
#include "miscadmin.h"
#include "rewrite/rewriteRemove.h"
#include "utils/fmgroids.h"
#include "utils/inval.h"
#include "utils/rel.h"
Include dependency graph for rewriteRemove.c:

Go to the source code of this file.


void RemoveRewriteRuleById (Oid ruleOid)

Function Documentation

◆ RemoveRewriteRuleById()

void RemoveRewriteRuleById ( Oid  ruleOid)

Definition at line 33 of file rewriteRemove.c.

34 {
35  Relation RewriteRelation;
36  ScanKeyData skey[1];
37  SysScanDesc rcscan;
38  Relation event_relation;
39  HeapTuple tuple;
40  Oid eventRelationOid;
42  /*
43  * Open the pg_rewrite relation.
44  */
45  RewriteRelation = table_open(RewriteRelationId, RowExclusiveLock);
47  /*
48  * Find the tuple for the target rule.
49  */
50  ScanKeyInit(&skey[0],
51  Anum_pg_rewrite_oid,
52  BTEqualStrategyNumber, F_OIDEQ,
53  ObjectIdGetDatum(ruleOid));
55  rcscan = systable_beginscan(RewriteRelation, RewriteOidIndexId, true,
56  NULL, 1, skey);
58  tuple = systable_getnext(rcscan);
60  if (!HeapTupleIsValid(tuple))
61  elog(ERROR, "could not find tuple for rule %u", ruleOid);
63  /*
64  * We had better grab AccessExclusiveLock to ensure that no queries are
65  * going on that might depend on this rule. (Note: a weaker lock would
66  * suffice if it's not an ON SELECT rule.)
67  */
68  eventRelationOid = ((Form_pg_rewrite) GETSTRUCT(tuple))->ev_class;
69  event_relation = table_open(eventRelationOid, AccessExclusiveLock);
71  if (!allowSystemTableMods && IsSystemRelation(event_relation))
72  ereport(ERROR,
74  errmsg("permission denied: \"%s\" is a system catalog",
75  RelationGetRelationName(event_relation))));
77  /*
78  * Now delete the pg_rewrite tuple for the rule
79  */
80  CatalogTupleDelete(RewriteRelation, &tuple->t_self);
82  systable_endscan(rcscan);
84  table_close(RewriteRelation, RowExclusiveLock);
86  /*
87  * Issue shared-inval notice to force all backends (including me!) to
88  * update relcache entries with the new rule set.
89  */
90  CacheInvalidateRelcache(event_relation);
92  /* Close rel, but keep lock till commit... */
93  table_close(event_relation, NoLock);
94 }
bool IsSystemRelation(Relation relation)
Definition: catalog.c:73
int errcode(int sqlerrcode)
Definition: elog.c:857
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define ereport(elevel,...)
Definition: elog.h:149
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:596
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:503
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:384
bool allowSystemTableMods
Definition: globals.c:127
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
Definition: htup_details.h:653
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:365
void CacheInvalidateRelcache(Relation relation)
Definition: inval.c:1360
#define NoLock
Definition: lockdefs.h:34
#define AccessExclusiveLock
Definition: lockdefs.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
FormData_pg_rewrite * Form_pg_rewrite
Definition: pg_rewrite.h:52
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
unsigned int Oid
Definition: postgres_ext.h:31
#define RelationGetRelationName(relation)
Definition: rel.h:539
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define BTEqualStrategyNumber
Definition: stratnum.h:31
ItemPointerData t_self
Definition: htup.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40

References AccessExclusiveLock, allowSystemTableMods, BTEqualStrategyNumber, CacheInvalidateRelcache(), CatalogTupleDelete(), elog, ereport, errcode(), errmsg(), ERROR, GETSTRUCT, HeapTupleIsValid, IsSystemRelation(), NoLock, ObjectIdGetDatum(), RelationGetRelationName, RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by doDeletion().