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

Go to the source code of this file.

Functions

void RemoveRewriteRuleById (Oid ruleOid)
 

Function Documentation

◆ RemoveRewriteRuleById()

void RemoveRewriteRuleById ( Oid  ruleOid)

Definition at line 39 of file rewriteRemove.c.

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

Referenced by doDeletion().

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