2
0

index.h 6.4 KB

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