~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/backend/catalog/indexing.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
 * indexing.c
 
4
 *        This file contains routines to support indexes defined on system
 
5
 *        catalogs.
 
6
 *
 
7
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
 
8
 * Portions Copyright (c) 1994, Regents of the University of California
 
9
 *
 
10
 *
 
11
 * IDENTIFICATION
 
12
 *        $PostgreSQL: pgsql/src/backend/catalog/indexing.c,v 1.107 2004-12-31 21:59:38 pgsql Exp $
 
13
 *
 
14
 *-------------------------------------------------------------------------
 
15
 */
 
16
#include "postgres.h"
 
17
 
 
18
#include "access/genam.h"
 
19
#include "catalog/index.h"
 
20
#include "catalog/indexing.h"
 
21
#include "executor/executor.h"
 
22
 
 
23
 
 
24
/*
 
25
 * CatalogOpenIndexes - open the indexes on a system catalog.
 
26
 *
 
27
 * When inserting or updating tuples in a system catalog, call this
 
28
 * to prepare to update the indexes for the catalog.
 
29
 *
 
30
 * In the current implementation, we share code for opening/closing the
 
31
 * indexes with execUtils.c.  But we do not use ExecInsertIndexTuples,
 
32
 * because we don't want to create an EState.  This implies that we
 
33
 * do not support partial or expressional indexes on system catalogs.
 
34
 * This could be fixed with localized changes here if we wanted to pay
 
35
 * the extra overhead of building an EState.
 
36
 */
 
37
CatalogIndexState
 
38
CatalogOpenIndexes(Relation heapRel)
 
39
{
 
40
        ResultRelInfo *resultRelInfo;
 
41
 
 
42
        resultRelInfo = makeNode(ResultRelInfo);
 
43
        resultRelInfo->ri_RangeTableIndex = 1;          /* dummy */
 
44
        resultRelInfo->ri_RelationDesc = heapRel;
 
45
        resultRelInfo->ri_TrigDesc = NULL;      /* we don't fire triggers */
 
46
 
 
47
        ExecOpenIndices(resultRelInfo);
 
48
 
 
49
        return resultRelInfo;
 
50
}
 
51
 
 
52
/*
 
53
 * CatalogCloseIndexes - clean up resources allocated by CatalogOpenIndexes
 
54
 */
 
55
void
 
56
CatalogCloseIndexes(CatalogIndexState indstate)
 
57
{
 
58
        ExecCloseIndices(indstate);
 
59
        pfree(indstate);
 
60
}
 
61
 
 
62
/*
 
63
 * CatalogIndexInsert - insert index entries for one catalog tuple
 
64
 *
 
65
 * This should be called for each inserted or updated catalog tuple.
 
66
 *
 
67
 * This is effectively a cut-down version of ExecInsertIndexTuples.
 
68
 */
 
69
void
 
70
CatalogIndexInsert(CatalogIndexState indstate, HeapTuple heapTuple)
 
71
{
 
72
        int                     i;
 
73
        int                     numIndexes;
 
74
        RelationPtr relationDescs;
 
75
        Relation        heapRelation;
 
76
        TupleDesc       heapDescriptor;
 
77
        IndexInfo **indexInfoArray;
 
78
        Datum           datum[INDEX_MAX_KEYS];
 
79
        char            nullv[INDEX_MAX_KEYS];
 
80
 
 
81
        /*
 
82
         * Get information from the state structure.
 
83
         */
 
84
        numIndexes = indstate->ri_NumIndices;
 
85
        relationDescs = indstate->ri_IndexRelationDescs;
 
86
        indexInfoArray = indstate->ri_IndexRelationInfo;
 
87
        heapRelation = indstate->ri_RelationDesc;
 
88
        heapDescriptor = RelationGetDescr(heapRelation);
 
89
 
 
90
        /*
 
91
         * for each index, form and insert the index tuple
 
92
         */
 
93
        for (i = 0; i < numIndexes; i++)
 
94
        {
 
95
                IndexInfo  *indexInfo;
 
96
                InsertIndexResult result;
 
97
 
 
98
                indexInfo = indexInfoArray[i];
 
99
 
 
100
                /*
 
101
                 * Expressional and partial indexes on system catalogs are not
 
102
                 * supported
 
103
                 */
 
104
                Assert(indexInfo->ii_Expressions == NIL);
 
105
                Assert(indexInfo->ii_Predicate == NIL);
 
106
 
 
107
                /*
 
108
                 * FormIndexDatum fills in its datum and null parameters with
 
109
                 * attribute information taken from the given heap tuple.
 
110
                 */
 
111
                FormIndexDatum(indexInfo,
 
112
                                           heapTuple,
 
113
                                           heapDescriptor,
 
114
                                           NULL,        /* no expression eval to do */
 
115
                                           datum,
 
116
                                           nullv);
 
117
 
 
118
                /*
 
119
                 * The index AM does the rest.
 
120
                 */
 
121
                result = index_insert(relationDescs[i], /* index relation */
 
122
                                                          datum,        /* array of heaptuple Datums */
 
123
                                                          nullv,        /* info on nulls */
 
124
                                                          &(heapTuple->t_self),         /* tid of heap tuple */
 
125
                                                          heapRelation,
 
126
                                                          relationDescs[i]->rd_index->indisunique);
 
127
 
 
128
                if (result)
 
129
                        pfree(result);
 
130
        }
 
131
}
 
132
 
 
133
/*
 
134
 * CatalogUpdateIndexes - do all the indexing work for a new catalog tuple
 
135
 *
 
136
 * This is a convenience routine for the common case where we only need
 
137
 * to insert or update a single tuple in a system catalog.      Avoid using it for
 
138
 * multiple tuples, since opening the indexes and building the index info
 
139
 * structures is moderately expensive.
 
140
 */
 
141
void
 
142
CatalogUpdateIndexes(Relation heapRel, HeapTuple heapTuple)
 
143
{
 
144
        CatalogIndexState indstate;
 
145
 
 
146
        indstate = CatalogOpenIndexes(heapRel);
 
147
        CatalogIndexInsert(indstate, heapTuple);
 
148
        CatalogCloseIndexes(indstate);
 
149
}