Header And Logo

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

Defines | Functions

rewriteDefine.h File Reference

#include "nodes/parsenodes.h"
#include "utils/relcache.h"
Include dependency graph for rewriteDefine.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define RULE_FIRES_ON_ORIGIN   'O'
#define RULE_FIRES_ALWAYS   'A'
#define RULE_FIRES_ON_REPLICA   'R'
#define RULE_DISABLED   'D'

Functions

Oid DefineRule (RuleStmt *stmt, const char *queryString)
Oid DefineQueryRewrite (char *rulename, Oid event_relid, Node *event_qual, CmdType event_type, bool is_instead, bool replace, List *action)
Oid RenameRewriteRule (RangeVar *relation, const char *oldName, const char *newName)
void setRuleCheckAsUser (Node *node, Oid userid)
void EnableDisableRule (Relation rel, const char *rulename, char fires_when)

Define Documentation

#define RULE_DISABLED   'D'

Definition at line 23 of file rewriteDefine.h.

Referenced by ATExecCmd(), and matchLocks().

#define RULE_FIRES_ALWAYS   'A'

Definition at line 21 of file rewriteDefine.h.

Referenced by ATExecCmd().

#define RULE_FIRES_ON_ORIGIN   'O'

Definition at line 20 of file rewriteDefine.h.

Referenced by ATExecCmd(), InsertRule(), and matchLocks().

#define RULE_FIRES_ON_REPLICA   'R'

Definition at line 22 of file rewriteDefine.h.

Referenced by ATExecCmd(), and matchLocks().


Function Documentation

Oid DefineQueryRewrite ( char *  rulename,
Oid  event_relid,
Node event_qual,
CmdType  event_type,
bool  is_instead,
bool  replace,
List action 
)

Definition at line 231 of file rewriteDefine.c.

References AccessExclusiveLock, ACL_KIND_CLASS, aclcheck_error(), ACLCHECK_NOT_OWNER, allowSystemTableMods, CatalogUpdateIndexes(), checkRuleResultList(), ObjectAddress::classId, CMD_SELECT, CommandCounterIncrement(), Query::commandType, deleteDependencyRecordsFor(), DeleteSystemAttributeTuples(), DROP_RESTRICT, elog, ereport, errcode(), errhint(), errmsg(), ERROR, RewriteRule::event, ForwardScanDirection, getInsertSelectQuery(), GETSTRUCT, GetUserId(), Query::hasModifyingCTE, heap_beginscan(), heap_close, heap_endscan(), heap_freetuple(), heap_getnext(), heap_open(), HeapTupleIsValid, i, InsertRule(), IsSystemRelation(), lfirst, linitial, list_length(), NAMEDATALEN, NIL, NoLock, NULL, RuleLock::numLocks, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, OidIsValid, PERFORM_DELETION_INTERNAL, performDeletion(), pg_class_ownercheck(), PRS2_NEW_VARNO, PRS2_OLD_VARNO, pstrdup(), RelationData::rd_rel, RelationData::rd_rules, RelationDropStorage(), RelationGetDescr, RelationGetRelationName, RelationRelationId, RELKIND_MATVIEW, RELKIND_RELATION, RELKIND_VIEW, RELOID, Query::resultRelation, Query::returningList, RowExclusiveLock, RuleLock::rules, SearchSysCacheCopy1, SetRelationRuleStatus(), simple_heap_update(), SnapshotNow, HeapTupleData::t_self, Query::targetList, Query::utilityStmt, and ViewSelectRuleName.

Referenced by DefineRule(), and DefineViewRules().

{
    Relation    event_relation;
    int         event_attno;
    ListCell   *l;
    Query      *query;
    bool        RelisBecomingView = false;
    Oid         ruleId = InvalidOid;

    /*
     * If we are installing an ON SELECT rule, we had better grab
     * AccessExclusiveLock to ensure no SELECTs are currently running on the
     * event relation. For other types of rules, it would be sufficient to
     * grab ShareRowExclusiveLock to lock out insert/update/delete actions and
     * to ensure that we lock out current CREATE RULE statements; but because
     * of race conditions in access to catalog entries, we can't do that yet.
     *
     * Note that this lock level should match the one used in DefineRule.
     */
    event_relation = heap_open(event_relid, AccessExclusiveLock);

    /*
     * Verify relation is of a type that rules can sensibly be applied to.
     */
    if (event_relation->rd_rel->relkind != RELKIND_RELATION &&
        event_relation->rd_rel->relkind != RELKIND_MATVIEW &&
        event_relation->rd_rel->relkind != RELKIND_VIEW)
        ereport(ERROR,
                (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                 errmsg("\"%s\" is not a table or view",
                        RelationGetRelationName(event_relation))));

    if (!allowSystemTableMods && IsSystemRelation(event_relation))
        ereport(ERROR,
                (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
                 errmsg("permission denied: \"%s\" is a system catalog",
                        RelationGetRelationName(event_relation))));

    /*
     * Check user has permission to apply rules to this relation.
     */
    if (!pg_class_ownercheck(event_relid, GetUserId()))
        aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CLASS,
                       RelationGetRelationName(event_relation));

    /*
     * No rule actions that modify OLD or NEW
     */
    foreach(l, action)
    {
        query = (Query *) lfirst(l);
        if (query->resultRelation == 0)
            continue;
        /* Don't be fooled by INSERT/SELECT */
        if (query != getInsertSelectQuery(query, NULL))
            continue;
        if (query->resultRelation == PRS2_OLD_VARNO)
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("rule actions on OLD are not implemented"),
                     errhint("Use views or triggers instead.")));
        if (query->resultRelation == PRS2_NEW_VARNO)
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("rule actions on NEW are not implemented"),
                     errhint("Use triggers instead.")));
    }

    if (event_type == CMD_SELECT)
    {
        /*
         * Rules ON SELECT are restricted to view definitions
         *
         * So there cannot be INSTEAD NOTHING, ...
         */
        if (list_length(action) == 0)
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
               errmsg("INSTEAD NOTHING rules on SELECT are not implemented"),
                     errhint("Use views instead.")));

        /*
         * ... there cannot be multiple actions, ...
         */
        if (list_length(action) > 1)
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("multiple actions for rules on SELECT are not implemented")));

        /*
         * ... the one action must be a SELECT, ...
         */
        query = (Query *) linitial(action);
        if (!is_instead ||
            query->commandType != CMD_SELECT ||
            query->utilityStmt != NULL)
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("rules on SELECT must have action INSTEAD SELECT")));

        /*
         * ... it cannot contain data-modifying WITH ...
         */
        if (query->hasModifyingCTE)
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("rules on SELECT must not contain data-modifying statements in WITH")));

        /*
         * ... there can be no rule qual, ...
         */
        if (event_qual != NULL)
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("event qualifications are not implemented for rules on SELECT")));

        /*
         * ... the targetlist of the SELECT action must exactly match the
         * event relation, ...
         */
        checkRuleResultList(query->targetList,
                            RelationGetDescr(event_relation),
                            true);

        /*
         * ... there must not be another ON SELECT rule already ...
         */
        if (!replace && event_relation->rd_rules != NULL)
        {
            int         i;

            for (i = 0; i < event_relation->rd_rules->numLocks; i++)
            {
                RewriteRule *rule;

                rule = event_relation->rd_rules->rules[i];
                if (rule->event == CMD_SELECT)
                    ereport(ERROR,
                          (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                           errmsg("\"%s\" is already a view",
                                  RelationGetRelationName(event_relation))));
            }
        }

        /*
         * ... and finally the rule must be named _RETURN.
         */
        if (strcmp(rulename, ViewSelectRuleName) != 0)
        {
            /*
             * In versions before 7.3, the expected name was _RETviewname. For
             * backwards compatibility with old pg_dump output, accept that
             * and silently change it to _RETURN.  Since this is just a quick
             * backwards-compatibility hack, limit the number of characters
             * checked to a few less than NAMEDATALEN; this saves having to
             * worry about where a multibyte character might have gotten
             * truncated.
             */
            if (strncmp(rulename, "_RET", 4) != 0 ||
                strncmp(rulename + 4, RelationGetRelationName(event_relation),
                        NAMEDATALEN - 4 - 4) != 0)
                ereport(ERROR,
                        (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                         errmsg("view rule for \"%s\" must be named \"%s\"",
                                RelationGetRelationName(event_relation),
                                ViewSelectRuleName)));
            rulename = pstrdup(ViewSelectRuleName);
        }

        /*
         * Are we converting a relation to a view?
         *
         * If so, check that the relation is empty because the storage for the
         * relation is going to be deleted.  Also insist that the rel not have
         * any triggers, indexes, or child tables.  (Note: these tests are too
         * strict, because they will reject relations that once had such but
         * don't anymore.  But we don't really care, because this whole
         * business of converting relations to views is just a kluge to allow
         * dump/reload of views that participate in circular dependencies.)
         */
        if (event_relation->rd_rel->relkind != RELKIND_VIEW &&
            event_relation->rd_rel->relkind != RELKIND_MATVIEW)
        {
            HeapScanDesc scanDesc;

            scanDesc = heap_beginscan(event_relation, SnapshotNow, 0, NULL);
            if (heap_getnext(scanDesc, ForwardScanDirection) != NULL)
                ereport(ERROR,
                        (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                         errmsg("could not convert table \"%s\" to a view because it is not empty",
                                RelationGetRelationName(event_relation))));
            heap_endscan(scanDesc);

            if (event_relation->rd_rel->relhastriggers)
                ereport(ERROR,
                        (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                         errmsg("could not convert table \"%s\" to a view because it has triggers",
                                RelationGetRelationName(event_relation)),
                         errhint("In particular, the table cannot be involved in any foreign key relationships.")));

            if (event_relation->rd_rel->relhasindex)
                ereport(ERROR,
                        (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                         errmsg("could not convert table \"%s\" to a view because it has indexes",
                                RelationGetRelationName(event_relation))));

            if (event_relation->rd_rel->relhassubclass)
                ereport(ERROR,
                        (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                         errmsg("could not convert table \"%s\" to a view because it has child tables",
                                RelationGetRelationName(event_relation))));

            RelisBecomingView = true;
        }
    }
    else
    {
        /*
         * For non-SELECT rules, a RETURNING list can appear in at most one of
         * the actions ... and there can't be any RETURNING list at all in a
         * conditional or non-INSTEAD rule.  (Actually, there can be at most
         * one RETURNING list across all rules on the same event, but it seems
         * best to enforce that at rule expansion time.)  If there is a
         * RETURNING list, it must match the event relation.
         */
        bool        haveReturning = false;

        foreach(l, action)
        {
            query = (Query *) lfirst(l);

            if (!query->returningList)
                continue;
            if (haveReturning)
                ereport(ERROR,
                        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                  errmsg("cannot have multiple RETURNING lists in a rule")));
            haveReturning = true;
            if (event_qual != NULL)
                ereport(ERROR,
                        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                         errmsg("RETURNING lists are not supported in conditional rules")));
            if (!is_instead)
                ereport(ERROR,
                        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                         errmsg("RETURNING lists are not supported in non-INSTEAD rules")));
            checkRuleResultList(query->returningList,
                                RelationGetDescr(event_relation),
                                false);
        }
    }

    /*
     * This rule is allowed - prepare to install it.
     */
    event_attno = -1;

    /* discard rule if it's null action and not INSTEAD; it's a no-op */
    if (action != NIL || is_instead)
    {
        ruleId = InsertRule(rulename,
                            event_type,
                            event_relid,
                            event_attno,
                            is_instead,
                            event_qual,
                            action,
                            replace);

        /*
         * Set pg_class 'relhasrules' field TRUE for event relation.
         *
         * Important side effect: an SI notice is broadcast to force all
         * backends (including me!) to update relcache entries with the new
         * rule.
         */
        SetRelationRuleStatus(event_relid, true);
    }

    /* ---------------------------------------------------------------------
     * If the relation is becoming a view:
     * - delete the associated storage files
     * - get rid of any system attributes in pg_attribute; a view shouldn't
     *   have any of those
     * - remove the toast table; there is no need for it anymore, and its
     *   presence would make vacuum slightly more complicated
     * - set relkind to RELKIND_VIEW, and adjust other pg_class fields
     *   to be appropriate for a view
     *
     * NB: we had better have AccessExclusiveLock to do this ...
     * ---------------------------------------------------------------------
     */
    if (RelisBecomingView)
    {
        Relation    relationRelation;
        Oid         toastrelid;
        HeapTuple   classTup;
        Form_pg_class classForm;

        relationRelation = heap_open(RelationRelationId, RowExclusiveLock);
        toastrelid = event_relation->rd_rel->reltoastrelid;

        /* drop storage while table still looks like a table  */
        RelationDropStorage(event_relation);
        DeleteSystemAttributeTuples(event_relid);

        /*
         * Drop the toast table if any.  (This won't take care of updating
         * the toast fields in the relation's own pg_class entry; we handle
         * that below.)
         */
        if (OidIsValid(toastrelid))
        {
            ObjectAddress toastobject;

            /*
             * Delete the dependency of the toast relation on the main
             * relation so we can drop the former without dropping the latter.
             */
            deleteDependencyRecordsFor(RelationRelationId, toastrelid,
                                       false);

            /* Make deletion of dependency record visible */
            CommandCounterIncrement();

            /* Now drop toast table, including its index */
            toastobject.classId = RelationRelationId;
            toastobject.objectId = toastrelid;
            toastobject.objectSubId = 0;
            performDeletion(&toastobject, DROP_RESTRICT,
                            PERFORM_DELETION_INTERNAL);
        }

        /*
         * SetRelationRuleStatus may have updated the pg_class row, so we must
         * advance the command counter before trying to update it again.
         */
        CommandCounterIncrement();

        /*
         * Fix pg_class entry to look like a normal view's, including setting
         * the correct relkind and removal of reltoastrelid/reltoastidxid of
         * the toast table we potentially removed above.
         */
        classTup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(event_relid));
        if (!HeapTupleIsValid(classTup))
            elog(ERROR, "cache lookup failed for relation %u", event_relid);
        classForm = (Form_pg_class) GETSTRUCT(classTup);

        classForm->reltablespace = InvalidOid;
        classForm->relpages = 0;
        classForm->reltuples = 0;
        classForm->relallvisible = 0;
        classForm->reltoastrelid = InvalidOid;
        classForm->reltoastidxid = InvalidOid;
        classForm->relhasindex = false;
        classForm->relkind = RELKIND_VIEW;
        classForm->relhasoids = false;
        classForm->relhaspkey = false;
        classForm->relfrozenxid = InvalidTransactionId;
        classForm->relminmxid = InvalidMultiXactId;

        simple_heap_update(relationRelation, &classTup->t_self, classTup);
        CatalogUpdateIndexes(relationRelation, classTup);

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

    /* Close rel, but keep lock till commit... */
    heap_close(event_relation, NoLock);

    return ruleId;
}

Oid DefineRule ( RuleStmt stmt,
const char *  queryString 
)

Definition at line 197 of file rewriteDefine.c.

References AccessExclusiveLock, DefineQueryRewrite(), RuleStmt::event, RuleStmt::instead, RangeVarGetRelid, RuleStmt::relation, RuleStmt::replace, RuleStmt::rulename, and transformRuleStmt().

Referenced by ProcessUtilitySlow().

{
    List       *actions;
    Node       *whereClause;
    Oid         relId;

    /* Parse analysis. */
    transformRuleStmt(stmt, queryString, &actions, &whereClause);

    /*
     * Find and lock the relation.  Lock level should match
     * DefineQueryRewrite.
     */
    relId = RangeVarGetRelid(stmt->relation, AccessExclusiveLock, false);

    /* ... and execute */
    return DefineQueryRewrite(stmt->rulename,
                              relId,
                              whereClause,
                              stmt->event,
                              stmt->instead,
                              stmt->replace,
                              actions);
}

void EnableDisableRule ( Relation  rel,
const char *  rulename,
char  fires_when 
)

Definition at line 771 of file rewriteDefine.c.

References ACL_KIND_CLASS, aclcheck_error(), ACLCHECK_NOT_OWNER, Assert, CacheInvalidateRelcache(), CatalogUpdateIndexes(), CharGetDatum, DatumGetChar, ereport, errcode(), errmsg(), ERROR, get_rel_name(), GETSTRUCT, GetUserId(), heap_close, heap_freetuple(), heap_open(), HeapTupleGetOid, HeapTupleIsValid, InvokeObjectPostAlterHook, ObjectIdGetDatum, pg_class_ownercheck(), PointerGetDatum, RelationGetRelid, RewriteRelationId, RowExclusiveLock, RULERELNAME, SearchSysCacheCopy2, simple_heap_update(), and HeapTupleData::t_self.

Referenced by ATExecEnableDisableRule().

{
    Relation    pg_rewrite_desc;
    Oid         owningRel = RelationGetRelid(rel);
    Oid         eventRelationOid;
    HeapTuple   ruletup;
    bool        changed = false;

    /*
     * Find the rule tuple to change.
     */
    pg_rewrite_desc = heap_open(RewriteRelationId, RowExclusiveLock);
    ruletup = SearchSysCacheCopy2(RULERELNAME,
                                  ObjectIdGetDatum(owningRel),
                                  PointerGetDatum(rulename));
    if (!HeapTupleIsValid(ruletup))
        ereport(ERROR,
                (errcode(ERRCODE_UNDEFINED_OBJECT),
                 errmsg("rule \"%s\" for relation \"%s\" does not exist",
                        rulename, get_rel_name(owningRel))));

    /*
     * Verify that the user has appropriate permissions.
     */
    eventRelationOid = ((Form_pg_rewrite) GETSTRUCT(ruletup))->ev_class;
    Assert(eventRelationOid == owningRel);
    if (!pg_class_ownercheck(eventRelationOid, GetUserId()))
        aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CLASS,
                       get_rel_name(eventRelationOid));

    /*
     * Change ev_enabled if it is different from the desired new state.
     */
    if (DatumGetChar(((Form_pg_rewrite) GETSTRUCT(ruletup))->ev_enabled) !=
        fires_when)
    {
        ((Form_pg_rewrite) GETSTRUCT(ruletup))->ev_enabled =
            CharGetDatum(fires_when);
        simple_heap_update(pg_rewrite_desc, &ruletup->t_self, ruletup);

        /* keep system catalog indexes current */
        CatalogUpdateIndexes(pg_rewrite_desc, ruletup);

        changed = true;
    }

    InvokeObjectPostAlterHook(RewriteRelationId,
                              HeapTupleGetOid(ruletup), 0);

    heap_freetuple(ruletup);
    heap_close(pg_rewrite_desc, RowExclusiveLock);

    /*
     * If we changed anything, broadcast a SI inval message to force each
     * backend (including our own!) to rebuild relation's relcache entry.
     * Otherwise they will fail to apply the change promptly.
     */
    if (changed)
        CacheInvalidateRelcache(rel);
}

Oid RenameRewriteRule ( RangeVar relation,
const char *  oldName,
const char *  newName 
)

Definition at line 872 of file rewriteDefine.c.

References AccessExclusiveLock, CacheInvalidateRelcache(), CatalogUpdateIndexes(), CMD_SELECT, ereport, errcode(), errmsg(), ERROR, GETSTRUCT, heap_close, heap_freetuple(), heap_open(), HeapTupleGetOid, HeapTupleIsValid, IsDefinedRewriteRule(), namestrcpy(), NoLock, NULL, ObjectIdGetDatum, PointerGetDatum, RangeVarCallbackForRenameRule(), RangeVarGetRelidExtended(), relation_close(), relation_open(), RelationGetRelationName, RewriteRelationId, RowExclusiveLock, RULERELNAME, SearchSysCacheCopy2, simple_heap_update(), and HeapTupleData::t_self.

Referenced by ExecRenameStmt().

{
    Oid         relid;
    Relation    targetrel;
    Relation    pg_rewrite_desc;
    HeapTuple   ruletup;
    Form_pg_rewrite ruleform;
    Oid         ruleOid;

    /*
     * Look up name, check permissions, and acquire lock (which we will NOT
     * release until end of transaction).
     */
    relid = RangeVarGetRelidExtended(relation, AccessExclusiveLock,
                                     false, false,
                                     RangeVarCallbackForRenameRule,
                                     NULL);

    /* Have lock already, so just need to build relcache entry. */
    targetrel = relation_open(relid, NoLock);

    /* Prepare to modify pg_rewrite */
    pg_rewrite_desc = heap_open(RewriteRelationId, RowExclusiveLock);

    /* Fetch the rule's entry (it had better exist) */
    ruletup = SearchSysCacheCopy2(RULERELNAME,
                                  ObjectIdGetDatum(relid),
                                  PointerGetDatum(oldName));
    if (!HeapTupleIsValid(ruletup))
        ereport(ERROR,
                (errcode(ERRCODE_UNDEFINED_OBJECT),
                 errmsg("rule \"%s\" for relation \"%s\" does not exist",
                        oldName, RelationGetRelationName(targetrel))));
    ruleform = (Form_pg_rewrite) GETSTRUCT(ruletup);
    ruleOid = HeapTupleGetOid(ruletup);

    /* rule with the new name should not already exist */
    if (IsDefinedRewriteRule(relid, newName))
        ereport(ERROR,
                (errcode(ERRCODE_DUPLICATE_OBJECT),
                 errmsg("rule \"%s\" for relation \"%s\" already exists",
                        newName, RelationGetRelationName(targetrel))));

    /*
     * We disallow renaming ON SELECT rules, because they should always be
     * named "_RETURN".
     */
    if (ruleform->ev_type == CMD_SELECT + '0')
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                 errmsg("renaming an ON SELECT rule is not allowed")));

    /* OK, do the update */
    namestrcpy(&(ruleform->rulename), newName);

    simple_heap_update(pg_rewrite_desc, &ruletup->t_self, ruletup);

    /* keep system catalog indexes current */
    CatalogUpdateIndexes(pg_rewrite_desc, ruletup);

    heap_freetuple(ruletup);
    heap_close(pg_rewrite_desc, RowExclusiveLock);

    /*
     * Invalidate relation's relcache entry so that other backends (and this
     * one too!) are sent SI message to make them rebuild relcache entries.
     * (Ideally this should happen automatically...)
     */
    CacheInvalidateRelcache(targetrel);

    /*
     * Close rel, but keep exclusive lock!
     */
    relation_close(targetrel, NoLock);

    return ruleOid;
}

void setRuleCheckAsUser ( Node node,
Oid  userid 
)

Definition at line 714 of file rewriteDefine.c.

References setRuleCheckAsUser_walker().

Referenced by RelationBuildRuleLock().

{
    (void) setRuleCheckAsUser_walker(node, &userid);
}