1 /*-------------------------------------------------------------------------
4 * This file contains routines to support indexes defined on system
7 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
14 *-------------------------------------------------------------------------
18 #include "access/genam.h"
19 #include "catalog/index.h"
20 #include "catalog/indexing.h"
21 #include "executor/executor.h"
25 * CatalogOpenIndexes - open the indexes on a system catalog.
27 * When inserting or updating tuples in a system catalog, call this
28 * to prepare to update the indexes for the catalog.
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.
38 CatalogOpenIndexes(Relation heapRel
)
40 ResultRelInfo
*resultRelInfo
;
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 */
47 ExecOpenIndices(resultRelInfo
);
53 * CatalogCloseIndexes - clean up resources allocated by CatalogOpenIndexes
56 CatalogCloseIndexes(CatalogIndexState indstate
)
58 ExecCloseIndices(indstate
);
63 * CatalogIndexInsert - insert index entries for one catalog tuple
65 * This should be called for each inserted or updated catalog tuple.
67 * This is effectively a cut-down version of ExecInsertIndexTuples.
70 CatalogIndexInsert(CatalogIndexState indstate
, HeapTuple heapTuple
)
74 RelationPtr relationDescs
;
75 Relation heapRelation
;
77 IndexInfo
**indexInfoArray
;
78 Datum values
[INDEX_MAX_KEYS
];
79 bool isnull
[INDEX_MAX_KEYS
];
81 /* HOT update does not require index inserts */
82 if (HeapTupleIsHeapOnly(heapTuple
))
86 * Get information from the state structure. Fall out if nothing to do.
88 numIndexes
= indstate
->ri_NumIndices
;
91 relationDescs
= indstate
->ri_IndexRelationDescs
;
92 indexInfoArray
= indstate
->ri_IndexRelationInfo
;
93 heapRelation
= indstate
->ri_RelationDesc
;
95 /* Need a slot to hold the tuple being examined */
96 slot
= MakeSingleTupleTableSlot(RelationGetDescr(heapRelation
));
97 ExecStoreTuple(heapTuple
, slot
, InvalidBuffer
, false);
100 * for each index, form and insert the index tuple
102 for (i
= 0; i
< numIndexes
; i
++)
104 IndexInfo
*indexInfo
;
106 indexInfo
= indexInfoArray
[i
];
108 /* If the index is marked as read-only, ignore it */
109 if (!indexInfo
->ii_ReadyForInserts
)
113 * Expressional and partial indexes on system catalogs are not
116 Assert(indexInfo
->ii_Expressions
== NIL
);
117 Assert(indexInfo
->ii_Predicate
== NIL
);
120 * FormIndexDatum fills in its values and isnull parameters with the
121 * appropriate values for the column(s) of the index.
123 FormIndexDatum(indexInfo
,
125 NULL
, /* no expression eval to do */
130 * The index AM does the rest.
132 index_insert(relationDescs
[i
], /* index relation */
133 values
, /* array of index Datums */
134 isnull
, /* is-null flags */
135 &(heapTuple
->t_self
), /* tid of heap tuple */
137 relationDescs
[i
]->rd_index
->indisunique
);
140 ExecDropSingleTupleTableSlot(slot
);
144 * CatalogUpdateIndexes - do all the indexing work for a new catalog tuple
146 * This is a convenience routine for the common case where we only need
147 * to insert or update a single tuple in a system catalog. Avoid using it for
148 * multiple tuples, since opening the indexes and building the index info
149 * structures is moderately expensive.
152 CatalogUpdateIndexes(Relation heapRel
, HeapTuple heapTuple
)
154 CatalogIndexState indstate
;
156 indstate
= CatalogOpenIndexes(heapRel
);
157 CatalogIndexInsert(indstate
, heapTuple
);
158 CatalogCloseIndexes(indstate
);