1 /*-------------------------------------------------------------------------
2  *
3  * index.h
4  *	  prototypes for catalog/index.c.
5  *
6  *
7  * Portions Copyright (c) 1996-2020, 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 #define DEFAULT_INDEX_TYPE	"btree"
22 
23 /* Action code for index_set_state_flags */
24 typedef enum
25 {
26 	INDEX_CREATE_SET_READY,
27 	INDEX_CREATE_SET_VALID,
28 	INDEX_DROP_CLEAR_VALID,
29 	INDEX_DROP_SET_DEAD
30 } IndexStateFlagsAction;
31 
32 /* state info for validate_index bulkdelete callback */
33 typedef struct ValidateIndexState
34 {
35 	Tuplesortstate *tuplesort;	/* for sorting the index TIDs */
36 	/* statistics (for debug purposes only): */
37 	double		htups,
38 				itups,
39 				tups_inserted;
40 } ValidateIndexState;
41 
42 extern void index_check_primary_key(Relation heapRel,
43 									IndexInfo *indexInfo,
44 									bool is_alter_table,
45 									IndexStmt *stmt);
46 
47 #define	INDEX_CREATE_IS_PRIMARY				(1 << 0)
48 #define	INDEX_CREATE_ADD_CONSTRAINT			(1 << 1)
49 #define	INDEX_CREATE_SKIP_BUILD				(1 << 2)
50 #define	INDEX_CREATE_CONCURRENT				(1 << 3)
51 #define	INDEX_CREATE_IF_NOT_EXISTS			(1 << 4)
52 #define	INDEX_CREATE_PARTITIONED			(1 << 5)
53 #define INDEX_CREATE_INVALID				(1 << 6)
54 
55 extern Oid	index_create(Relation heapRelation,
56 						 const char *indexRelationName,
57 						 Oid indexRelationId,
58 						 Oid parentIndexRelid,
59 						 Oid parentConstraintId,
60 						 Oid relFileNode,
61 						 IndexInfo *indexInfo,
62 						 List *indexColNames,
63 						 Oid accessMethodObjectId,
64 						 Oid tableSpaceId,
65 						 Oid *collationObjectId,
66 						 Oid *classObjectId,
67 						 int16 *coloptions,
68 						 Datum reloptions,
69 						 bits16 flags,
70 						 bits16 constr_flags,
71 						 bool allow_system_table_mods,
72 						 bool is_internal,
73 						 Oid *constraintId);
74 
75 #define	INDEX_CONSTR_CREATE_MARK_AS_PRIMARY	(1 << 0)
76 #define	INDEX_CONSTR_CREATE_DEFERRABLE		(1 << 1)
77 #define	INDEX_CONSTR_CREATE_INIT_DEFERRED	(1 << 2)
78 #define	INDEX_CONSTR_CREATE_UPDATE_INDEX	(1 << 3)
79 #define	INDEX_CONSTR_CREATE_REMOVE_OLD_DEPS	(1 << 4)
80 
81 extern Oid	index_concurrently_create_copy(Relation heapRelation,
82 										   Oid oldIndexId,
83 										   const char *newName);
84 
85 extern void index_concurrently_build(Oid heapRelationId,
86 									 Oid indexRelationId);
87 
88 extern void index_concurrently_swap(Oid newIndexId,
89 									Oid oldIndexId,
90 									const char *oldName);
91 
92 extern void index_concurrently_set_dead(Oid heapId,
93 										Oid indexId);
94 
95 extern ObjectAddress index_constraint_create(Relation heapRelation,
96 											 Oid indexRelationId,
97 											 Oid parentConstraintId,
98 											 IndexInfo *indexInfo,
99 											 const char *constraintName,
100 											 char constraintType,
101 											 bits16 constr_flags,
102 											 bool allow_system_table_mods,
103 											 bool is_internal);
104 
105 extern void index_drop(Oid indexId, bool concurrent, bool concurrent_lock_mode);
106 
107 extern IndexInfo *BuildIndexInfo(Relation index);
108 
109 extern IndexInfo *BuildDummyIndexInfo(Relation index);
110 
111 extern bool CompareIndexInfo(IndexInfo *info1, IndexInfo *info2,
112 							 Oid *collations1, Oid *collations2,
113 							 Oid *opfamilies1, Oid *opfamilies2,
114 							 AttrMap *attmap);
115 
116 extern void BuildSpeculativeIndexInfo(Relation index, IndexInfo *ii);
117 
118 extern void FormIndexDatum(IndexInfo *indexInfo,
119 						   TupleTableSlot *slot,
120 						   EState *estate,
121 						   Datum *values,
122 						   bool *isnull);
123 
124 extern void index_build(Relation heapRelation,
125 						Relation indexRelation,
126 						IndexInfo *indexInfo,
127 						bool isreindex,
128 						bool parallel);
129 
130 extern void validate_index(Oid heapId, Oid indexId, Snapshot snapshot);
131 
132 extern void index_set_state_flags(Oid indexId, IndexStateFlagsAction action);
133 
134 extern Oid	IndexGetRelation(Oid indexId, bool missing_ok);
135 
136 extern void reindex_index(Oid indexId, bool skip_constraint_checks,
137 						  char relpersistence, int options);
138 
139 /* Flag bits for reindex_relation(): */
140 #define REINDEX_REL_PROCESS_TOAST			0x01
141 #define REINDEX_REL_SUPPRESS_INDEX_USE		0x02
142 #define REINDEX_REL_CHECK_CONSTRAINTS		0x04
143 #define REINDEX_REL_FORCE_INDEXES_UNLOGGED	0x08
144 #define REINDEX_REL_FORCE_INDEXES_PERMANENT 0x10
145 
146 extern bool reindex_relation(Oid relid, int flags, int options);
147 
148 extern bool ReindexIsProcessingHeap(Oid heapOid);
149 extern bool ReindexIsProcessingIndex(Oid indexOid);
150 
151 extern void ResetReindexState(int nestLevel);
152 extern Size EstimateReindexStateSpace(void);
153 extern void SerializeReindexState(Size maxsize, char *start_address);
154 extern void RestoreReindexState(void *reindexstate);
155 
156 extern void IndexSetParentIndex(Relation idx, Oid parentOid);
157 
158 
159 /*
160  * itemptr_encode - Encode ItemPointer as int64/int8
161  *
162  * This representation must produce values encoded as int64 that sort in the
163  * same order as their corresponding original TID values would (using the
164  * default int8 opclass to produce a result equivalent to the default TID
165  * opclass).
166  *
167  * As noted in validate_index(), this can be significantly faster.
168  */
169 static inline int64
170 itemptr_encode(ItemPointer itemptr)
171 {
172 	BlockNumber block = ItemPointerGetBlockNumber(itemptr);
173 	OffsetNumber offset = ItemPointerGetOffsetNumber(itemptr);
174 	int64		encoded;
175 
176 	/*
177 	 * Use the 16 least significant bits for the offset.  32 adjacent bits are
178 	 * used for the block number.  Since remaining bits are unused, there
179 	 * cannot be negative encoded values (We assume a two's complement
180 	 * representation).
181 	 */
182 	encoded = ((uint64) block << 16) | (uint16) offset;
183 
184 	return encoded;
185 }
186 
187 /*
188  * itemptr_decode - Decode int64/int8 representation back to ItemPointer
189  */
190 static inline void
191 itemptr_decode(ItemPointer itemptr, int64 encoded)
192 {
193 	BlockNumber block = (BlockNumber) (encoded >> 16);
194 	OffsetNumber offset = (OffsetNumber) (encoded & 0xFFFF);
195 
196 	ItemPointerSet(itemptr, block, offset);
197 }
198 
199 #endif							/* INDEX_H */
200