Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * index.h
4 : * prototypes for catalog/index.c.
5 : *
6 : *
7 : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 : * Portions Copyright (c) 1994, Regents of the University of California
9 : *
10 : * src/include/catalog/index.h
11 : *
12 : *-------------------------------------------------------------------------
13 : */
14 : #ifndef INDEX_H
15 : #define INDEX_H
16 :
17 : #include "catalog/objectaddress.h"
18 : #include "nodes/execnodes.h"
19 :
20 :
21 : /*
22 : * forward references in this file
23 : */
24 : typedef struct AttrMap AttrMap;
25 :
26 :
27 : #define DEFAULT_INDEX_TYPE "btree"
28 :
29 : /* Action code for index_set_state_flags */
30 : typedef enum
31 : {
32 : INDEX_CREATE_SET_READY,
33 : INDEX_CREATE_SET_VALID,
34 : INDEX_DROP_CLEAR_VALID,
35 : INDEX_DROP_SET_DEAD,
36 : } IndexStateFlagsAction;
37 :
38 : /* options for REINDEX */
39 : typedef struct ReindexParams
40 : {
41 : uint32 options; /* bitmask of REINDEXOPT_* */
42 : Oid tablespaceOid; /* New tablespace to move indexes to.
43 : * InvalidOid to do nothing. */
44 : } ReindexParams;
45 :
46 : /* flag bits for ReindexParams->flags */
47 : #define REINDEXOPT_VERBOSE 0x01 /* print progress info */
48 : #define REINDEXOPT_REPORT_PROGRESS 0x02 /* report pgstat progress */
49 : #define REINDEXOPT_MISSING_OK 0x04 /* skip missing relations */
50 : #define REINDEXOPT_CONCURRENTLY 0x08 /* concurrent mode */
51 :
52 : /* state info for validate_index bulkdelete callback */
53 : typedef struct ValidateIndexState
54 : {
55 : Tuplesortstate *tuplesort; /* for sorting the index TIDs */
56 : /* statistics (for debug purposes only): */
57 : double htups,
58 : itups,
59 : tups_inserted;
60 : } ValidateIndexState;
61 :
62 : extern void index_check_primary_key(Relation heapRel,
63 : const IndexInfo *indexInfo,
64 : bool is_alter_table,
65 : const IndexStmt *stmt);
66 :
67 : #define INDEX_CREATE_IS_PRIMARY (1 << 0)
68 : #define INDEX_CREATE_ADD_CONSTRAINT (1 << 1)
69 : #define INDEX_CREATE_SKIP_BUILD (1 << 2)
70 : #define INDEX_CREATE_CONCURRENT (1 << 3)
71 : #define INDEX_CREATE_IF_NOT_EXISTS (1 << 4)
72 : #define INDEX_CREATE_PARTITIONED (1 << 5)
73 : #define INDEX_CREATE_INVALID (1 << 6)
74 : #define INDEX_CREATE_SUPPRESS_PROGRESS (1 << 7)
75 : #define INDEX_CREATE_DEFERRABLE (1 << 8)
76 :
77 : extern Oid index_create(Relation heapRelation,
78 : const char *indexRelationName,
79 : Oid indexRelationId,
80 : Oid parentIndexRelid,
81 : Oid parentConstraintId,
82 : RelFileNumber relFileNumber,
83 : IndexInfo *indexInfo,
84 : const List *indexColNames,
85 : Oid accessMethodId,
86 : Oid tableSpaceId,
87 : const Oid *collationIds,
88 : const Oid *opclassIds,
89 : const Datum *opclassOptions,
90 : const int16 *coloptions,
91 : const NullableDatum *stattargets,
92 : Datum reloptions,
93 : uint16 flags,
94 : uint16 constr_flags,
95 : bool allow_system_table_mods,
96 : bool is_internal,
97 : Oid *constraintId);
98 :
99 : #define INDEX_CONSTR_CREATE_MARK_AS_PRIMARY (1 << 0)
100 : #define INDEX_CONSTR_CREATE_DEFERRABLE (1 << 1)
101 : #define INDEX_CONSTR_CREATE_INIT_DEFERRED (1 << 2)
102 : #define INDEX_CONSTR_CREATE_UPDATE_INDEX (1 << 3)
103 : #define INDEX_CONSTR_CREATE_REMOVE_OLD_DEPS (1 << 4)
104 : #define INDEX_CONSTR_CREATE_WITHOUT_OVERLAPS (1 << 5)
105 :
106 : extern Oid index_create_copy(Relation heapRelation, uint16 flags,
107 : Oid oldIndexId, Oid tablespaceOid,
108 : const char *newName);
109 :
110 : extern void index_concurrently_build(Oid heapRelationId,
111 : Oid indexRelationId);
112 :
113 : extern void index_concurrently_swap(Oid newIndexId,
114 : Oid oldIndexId,
115 : const char *oldName);
116 :
117 : extern void index_concurrently_set_dead(Oid heapId,
118 : Oid indexId);
119 :
120 : extern ObjectAddress index_constraint_create(Relation heapRelation,
121 : Oid indexRelationId,
122 : Oid parentConstraintId,
123 : const IndexInfo *indexInfo,
124 : const char *constraintName,
125 : char constraintType,
126 : uint16 constr_flags,
127 : bool allow_system_table_mods,
128 : bool is_internal);
129 :
130 : extern void index_drop(Oid indexId, bool concurrent, bool concurrent_lock_mode);
131 :
132 : extern IndexInfo *BuildIndexInfo(Relation index);
133 :
134 : extern IndexInfo *BuildDummyIndexInfo(Relation index);
135 :
136 : extern bool CompareIndexInfo(const IndexInfo *info1, const IndexInfo *info2,
137 : const Oid *collations1, const Oid *collations2,
138 : const Oid *opfamilies1, const Oid *opfamilies2,
139 : const AttrMap *attmap);
140 :
141 : extern void BuildSpeculativeIndexInfo(Relation index, IndexInfo *ii);
142 :
143 : extern void FormIndexDatum(IndexInfo *indexInfo,
144 : TupleTableSlot *slot,
145 : EState *estate,
146 : Datum *values,
147 : bool *isnull);
148 :
149 : extern void index_build(Relation heapRelation,
150 : Relation indexRelation,
151 : IndexInfo *indexInfo,
152 : bool isreindex,
153 : bool parallel,
154 : bool progress);
155 :
156 : extern void validate_index(Oid heapId, Oid indexId, Snapshot snapshot);
157 :
158 : extern void index_set_state_flags(Oid indexId, IndexStateFlagsAction action);
159 :
160 : extern Oid IndexGetRelation(Oid indexId, bool missing_ok);
161 :
162 : extern void reindex_index(const ReindexStmt *stmt, Oid indexId,
163 : bool skip_constraint_checks, char persistence,
164 : const ReindexParams *params);
165 :
166 : /* Flag bits for reindex_relation(): */
167 : #define REINDEX_REL_PROCESS_TOAST 0x01
168 : #define REINDEX_REL_SUPPRESS_INDEX_USE 0x02
169 : #define REINDEX_REL_CHECK_CONSTRAINTS 0x04
170 : #define REINDEX_REL_FORCE_INDEXES_UNLOGGED 0x08
171 : #define REINDEX_REL_FORCE_INDEXES_PERMANENT 0x10
172 :
173 : extern bool reindex_relation(const ReindexStmt *stmt, Oid relid, int flags,
174 : const ReindexParams *params);
175 :
176 : extern bool ReindexIsProcessingHeap(Oid heapOid);
177 : extern bool ReindexIsProcessingIndex(Oid indexOid);
178 :
179 : extern void ResetReindexState(int nestLevel);
180 : extern Size EstimateReindexStateSpace(void);
181 : extern void SerializeReindexState(Size maxsize, char *start_address);
182 : extern void RestoreReindexState(const void *reindexstate);
183 :
184 : extern void IndexSetParentIndex(Relation partitionIdx, Oid parentOid);
185 :
186 :
187 : /*
188 : * itemptr_encode - Encode ItemPointer as int64/int8
189 : *
190 : * This representation must produce values encoded as int64 that sort in the
191 : * same order as their corresponding original TID values would (using the
192 : * default int8 opclass to produce a result equivalent to the default TID
193 : * opclass).
194 : *
195 : * As noted in validate_index(), this can be significantly faster.
196 : */
197 : static inline int64
198 180809 : itemptr_encode(const ItemPointerData *itemptr)
199 : {
200 180809 : BlockNumber block = ItemPointerGetBlockNumber(itemptr);
201 180809 : OffsetNumber offset = ItemPointerGetOffsetNumber(itemptr);
202 : int64 encoded;
203 :
204 : /*
205 : * Use the 16 least significant bits for the offset. 32 adjacent bits are
206 : * used for the block number. Since remaining bits are unused, there
207 : * cannot be negative encoded values (We assume a two's complement
208 : * representation).
209 : */
210 180809 : encoded = ((uint64) block << 16) | (uint16) offset;
211 :
212 180809 : return encoded;
213 : }
214 :
215 : /*
216 : * itemptr_decode - Decode int64/int8 representation back to ItemPointer
217 : */
218 : static inline void
219 180630 : itemptr_decode(ItemPointer itemptr, int64 encoded)
220 : {
221 180630 : BlockNumber block = (BlockNumber) (encoded >> 16);
222 180630 : OffsetNumber offset = (OffsetNumber) (encoded & 0xFFFF);
223 :
224 180630 : ItemPointerSet(itemptr, block, offset);
225 180630 : }
226 :
227 : #endif /* INDEX_H */
|