Header And Logo

PostgreSQL
| The world's most advanced open source database.

Functions

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.

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 *reloid, bool missing_ok)

Function Documentation

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

Definition at line 97 of file rewriteSupport.c.

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

Referenced by get_object_address_relobject().

{
    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;
}

Oid get_rewrite_oid_without_relid ( const char *  rulename,
Oid reloid,
bool  missing_ok 
)

Definition at line 130 of file rewriteSupport.c.

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

Referenced by get_object_address_relobject().

{
    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))
    {
        if (!missing_ok)
            ereport(ERROR,
                    (errcode(ERRCODE_UNDEFINED_OBJECT),
                     errmsg("rule \"%s\" does not exist", rulename)));
        ruleoid = InvalidOid;
    }
    else
    {
        ruleoid = HeapTupleGetOid(htup);
        if (reloid != NULL)
            *reloid = ((Form_pg_rewrite) GETSTRUCT(htup))->ev_class;

        htup = heap_getnext(scanDesc, ForwardScanDirection);
        if (HeapTupleIsValid(htup))
            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;
}

bool IsDefinedRewriteRule ( Oid  owningRel,
const char *  ruleName 
)

Definition at line 34 of file rewriteSupport.c.

References ObjectIdGetDatum, PointerGetDatum, RULERELNAME, and SearchSysCacheExists2.

Referenced by RenameRewriteRule().

void SetRelationRuleStatus ( Oid  relationId,
bool  relHasRules 
)

Definition at line 55 of file rewriteSupport.c.

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

Referenced by DefineQueryRewrite().

{
    Relation    relationRelation;
    HeapTuple   tuple;
    Form_pg_class classForm;

    /*
     * Find the tuple to update in pg_class, using syscache for the lookup.
     */
    relationRelation = heap_open(RelationRelationId, RowExclusiveLock);
    tuple = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relationId));
    if (!HeapTupleIsValid(tuple))
        elog(ERROR, "cache lookup failed for relation %u", relationId);
    classForm = (Form_pg_class) GETSTRUCT(tuple);

    if (classForm->relhasrules != relHasRules)
    {
        /* Do the update */
        classForm->relhasrules = relHasRules;

        simple_heap_update(relationRelation, &tuple->t_self, tuple);

        /* Keep the catalog indexes up to date */
        CatalogUpdateIndexes(relationRelation, tuple);
    }
    else
    {
        /* no need to change tuple, but force relcache rebuild anyway */
        CacheInvalidateRelcacheByTuple(tuple);
    }

    heap_freetuple(tuple);
    heap_close(relationRelation, RowExclusiveLock);
}