~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/backend/rewrite/rewriteSupport.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * rewriteSupport.c
 
4
 *
 
5
 *
 
6
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
 
7
 * Portions Copyright (c) 1994, Regents of the University of California
 
8
 *
 
9
 *
 
10
 * IDENTIFICATION
 
11
 *        $PostgreSQL: pgsql/src/backend/rewrite/rewriteSupport.c,v 1.60 2004-12-31 22:00:46 pgsql Exp $
 
12
 *
 
13
 *-------------------------------------------------------------------------
 
14
 */
 
15
#include "postgres.h"
 
16
 
 
17
#include "access/heapam.h"
 
18
#include "catalog/catname.h"
 
19
#include "catalog/indexing.h"
 
20
#include "rewrite/rewriteSupport.h"
 
21
#include "utils/inval.h"
 
22
#include "utils/syscache.h"
 
23
 
 
24
 
 
25
/*
 
26
 * Is there a rule by the given name?
 
27
 */
 
28
bool
 
29
IsDefinedRewriteRule(Oid owningRel, const char *ruleName)
 
30
{
 
31
        return SearchSysCacheExists(RULERELNAME,
 
32
                                                                ObjectIdGetDatum(owningRel),
 
33
                                                                PointerGetDatum(ruleName),
 
34
                                                                0, 0);
 
35
}
 
36
 
 
37
 
 
38
/*
 
39
 * SetRelationRuleStatus
 
40
 *              Set the value of the relation's relhasrules field in pg_class;
 
41
 *              if the relation is becoming a view, also adjust its relkind.
 
42
 *
 
43
 * NOTE: caller must be holding an appropriate lock on the relation.
 
44
 *
 
45
 * NOTE: an important side-effect of this operation is that an SI invalidation
 
46
 * message is sent out to all backends --- including me --- causing relcache
 
47
 * entries to be flushed or updated with the new set of rules for the table.
 
48
 * This must happen even if we find that no change is needed in the pg_class
 
49
 * row.
 
50
 */
 
51
void
 
52
SetRelationRuleStatus(Oid relationId, bool relHasRules,
 
53
                                          bool relIsBecomingView)
 
54
{
 
55
        Relation        relationRelation;
 
56
        HeapTuple       tuple;
 
57
        Form_pg_class classForm;
 
58
 
 
59
        /*
 
60
         * Find the tuple to update in pg_class, using syscache for the
 
61
         * lookup.
 
62
         */
 
63
        relationRelation = heap_openr(RelationRelationName, RowExclusiveLock);
 
64
        tuple = SearchSysCacheCopy(RELOID,
 
65
                                                           ObjectIdGetDatum(relationId),
 
66
                                                           0, 0, 0);
 
67
        if (!HeapTupleIsValid(tuple))
 
68
                elog(ERROR, "cache lookup failed for relation %u", relationId);
 
69
        classForm = (Form_pg_class) GETSTRUCT(tuple);
 
70
 
 
71
        if (classForm->relhasrules != relHasRules ||
 
72
                (relIsBecomingView && classForm->relkind != RELKIND_VIEW))
 
73
        {
 
74
                /* Do the update */
 
75
                classForm->relhasrules = relHasRules;
 
76
                if (relIsBecomingView)
 
77
                        classForm->relkind = RELKIND_VIEW;
 
78
 
 
79
                simple_heap_update(relationRelation, &tuple->t_self, tuple);
 
80
 
 
81
                /* Keep the catalog indexes up to date */
 
82
                CatalogUpdateIndexes(relationRelation, tuple);
 
83
        }
 
84
        else
 
85
        {
 
86
                /* no need to change tuple, but force relcache rebuild anyway */
 
87
                CacheInvalidateRelcacheByTuple(tuple);
 
88
        }
 
89
 
 
90
        heap_freetuple(tuple);
 
91
        heap_close(relationRelation, RowExclusiveLock);
 
92
}