diff options
author | Robert Haas <rhaas@postgresql.org> | 2010-08-05 15:25:36 +0000 |
---|---|---|
committer | Robert Haas <rhaas@postgresql.org> | 2010-08-05 15:25:36 +0000 |
commit | fd1843ff8979c0461fb3f1a9eab61140c977e32d (patch) | |
tree | 9a4201b5ddc009b89ce7385470b7b7fa9eacf87d /src/backend/rewrite/rewriteSupport.c | |
parent | 2a6ef3445c73473edb222abf108b323fb7f002dc (diff) | |
download | postgresql-fd1843ff8979c0461fb3f1a9eab61140c977e32d.tar.gz postgresql-fd1843ff8979c0461fb3f1a9eab61140c977e32d.zip |
Standardize get_whatever_oid functions for other object types.
- Rename TSParserGetPrsid to get_ts_parser_oid.
- Rename TSDictionaryGetDictid to get_ts_dict_oid.
- Rename TSTemplateGetTmplid to get_ts_template_oid.
- Rename TSConfigGetCfgid to get_ts_config_oid.
- Rename FindConversionByName to get_conversion_oid.
- Rename GetConstraintName to get_constraint_oid.
- Add new functions get_opclass_oid, get_opfamily_oid, get_rewrite_oid,
get_rewrite_oid_without_relid, get_trigger_oid, and get_cast_oid.
The name of each function matches the corresponding catalog.
Thanks to KaiGai Kohei for the review.
Diffstat (limited to 'src/backend/rewrite/rewriteSupport.c')
-rw-r--r-- | src/backend/rewrite/rewriteSupport.c | 86 |
1 files changed, 85 insertions, 1 deletions
diff --git a/src/backend/rewrite/rewriteSupport.c b/src/backend/rewrite/rewriteSupport.c index 8f668f5791e..0e39b060735 100644 --- a/src/backend/rewrite/rewriteSupport.c +++ b/src/backend/rewrite/rewriteSupport.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/rewrite/rewriteSupport.c,v 1.69 2010/02/14 18:42:15 rhaas Exp $ + * $PostgreSQL: pgsql/src/backend/rewrite/rewriteSupport.c,v 1.70 2010/08/05 15:25:35 rhaas Exp $ * *------------------------------------------------------------------------- */ @@ -17,9 +17,14 @@ #include "access/heapam.h" #include "catalog/indexing.h" #include "catalog/pg_class.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" /* @@ -86,3 +91,82 @@ SetRelationRuleStatus(Oid relationId, bool relHasRules, heap_freetuple(tuple); heap_close(relationRelation, RowExclusiveLock); } + +/* + * Find rule oid. + * + * If missing_ok is false, throw an error if rule name not found. If + * true, just return InvalidOid. + */ +Oid +get_rewrite_oid(Oid relid, const char *rulename, bool missing_ok) +{ + HeapTuple tuple; + Oid ruleoid; + + /* Find the rule's pg_rewrite tuple, get its OID */ + tuple = SearchSysCache2(RULERELNAME, + ObjectIdGetDatum(relid), + PointerGetDatum(rulename)); + if (!HeapTupleIsValid(tuple)) + { + if (missing_ok) + return InvalidOid; + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_OBJECT), + errmsg("rule \"%s\" for relation \"%s\" does not exist", + rulename, get_rel_name(relid)))); + } + Assert(relid == ((Form_pg_rewrite) GETSTRUCT(tuple))->ev_class); + ruleoid = HeapTupleGetOid(tuple); + ReleaseSysCache(tuple); + return ruleoid; +} + +/* + * Find rule oid, given only a rule name but no rel OID. + * + * If there's more than one, it's an error. If there aren't any, that's an + * error, too. In general, this should be avoided - it is provided to support + * syntax that is compatible with pre-7.3 versions of PG, where rule names + * were unique across the entire database. + */ +Oid +get_rewrite_oid_without_relid(const char *rulename, Oid *reloid) +{ + Relation RewriteRelation; + HeapScanDesc scanDesc; + ScanKeyData scanKeyData; + HeapTuple htup; + Oid ruleoid; + + /* Search pg_rewrite for such a rule */ + ScanKeyInit(&scanKeyData, + Anum_pg_rewrite_rulename, + BTEqualStrategyNumber, F_NAMEEQ, + CStringGetDatum(rulename)); + + RewriteRelation = heap_open(RewriteRelationId, AccessShareLock); + scanDesc = heap_beginscan(RewriteRelation, SnapshotNow, 1, &scanKeyData); + + htup = heap_getnext(scanDesc, ForwardScanDirection); + if (!HeapTupleIsValid(htup)) + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_OBJECT), + errmsg("rule \"%s\" does not exist", rulename))); + + ruleoid = HeapTupleGetOid(htup); + if (reloid != NULL) + *reloid = ((Form_pg_rewrite) GETSTRUCT(htup))->ev_class; + + if (HeapTupleIsValid(htup = heap_getnext(scanDesc, ForwardScanDirection))) + ereport(ERROR, + (errcode(ERRCODE_DUPLICATE_OBJECT), + errmsg("there are multiple rules named \"%s\"", rulename), + errhint("Specify a relation name as well as a rule name."))); + + heap_endscan(scanDesc); + heap_close(RewriteRelation, AccessShareLock); + + return ruleoid; +} |