PostgreSQL Source Code git master
Loading...
Searching...
No Matches
index.h File Reference
Include dependency graph for index.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ReindexParams
 
struct  ValidateIndexState
 

Macros

#define DEFAULT_INDEX_TYPE   "btree"
 
#define REINDEXOPT_VERBOSE   0x01 /* print progress info */
 
#define REINDEXOPT_REPORT_PROGRESS   0x02 /* report pgstat progress */
 
#define REINDEXOPT_MISSING_OK   0x04 /* skip missing relations */
 
#define REINDEXOPT_CONCURRENTLY   0x08 /* concurrent mode */
 
#define INDEX_CREATE_IS_PRIMARY   (1 << 0)
 
#define INDEX_CREATE_ADD_CONSTRAINT   (1 << 1)
 
#define INDEX_CREATE_SKIP_BUILD   (1 << 2)
 
#define INDEX_CREATE_CONCURRENT   (1 << 3)
 
#define INDEX_CREATE_IF_NOT_EXISTS   (1 << 4)
 
#define INDEX_CREATE_PARTITIONED   (1 << 5)
 
#define INDEX_CREATE_INVALID   (1 << 6)
 
#define INDEX_CREATE_SUPPRESS_PROGRESS   (1 << 7)
 
#define INDEX_CONSTR_CREATE_MARK_AS_PRIMARY   (1 << 0)
 
#define INDEX_CONSTR_CREATE_DEFERRABLE   (1 << 1)
 
#define INDEX_CONSTR_CREATE_INIT_DEFERRED   (1 << 2)
 
#define INDEX_CONSTR_CREATE_UPDATE_INDEX   (1 << 3)
 
#define INDEX_CONSTR_CREATE_REMOVE_OLD_DEPS   (1 << 4)
 
#define INDEX_CONSTR_CREATE_WITHOUT_OVERLAPS   (1 << 5)
 
#define REINDEX_REL_PROCESS_TOAST   0x01
 
#define REINDEX_REL_SUPPRESS_INDEX_USE   0x02
 
#define REINDEX_REL_CHECK_CONSTRAINTS   0x04
 
#define REINDEX_REL_FORCE_INDEXES_UNLOGGED   0x08
 
#define REINDEX_REL_FORCE_INDEXES_PERMANENT   0x10
 

Typedefs

typedef struct AttrMap AttrMap
 
typedef struct ReindexParams ReindexParams
 
typedef struct ValidateIndexState ValidateIndexState
 

Enumerations

enum  IndexStateFlagsAction { INDEX_CREATE_SET_READY , INDEX_CREATE_SET_VALID , INDEX_DROP_CLEAR_VALID , INDEX_DROP_SET_DEAD }
 

Functions

void index_check_primary_key (Relation heapRel, const IndexInfo *indexInfo, bool is_alter_table, const IndexStmt *stmt)
 
Oid index_create (Relation heapRelation, const char *indexRelationName, Oid indexRelationId, Oid parentIndexRelid, Oid parentConstraintId, RelFileNumber relFileNumber, IndexInfo *indexInfo, const List *indexColNames, Oid accessMethodId, Oid tableSpaceId, const Oid *collationIds, const Oid *opclassIds, const Datum *opclassOptions, const int16 *coloptions, const NullableDatum *stattargets, Datum reloptions, uint16 flags, uint16 constr_flags, bool allow_system_table_mods, bool is_internal, Oid *constraintId)
 
Oid index_create_copy (Relation heapRelation, uint16 flags, Oid oldIndexId, Oid tablespaceOid, const char *newName)
 
void index_concurrently_build (Oid heapRelationId, Oid indexRelationId)
 
void index_concurrently_swap (Oid newIndexId, Oid oldIndexId, const char *oldName)
 
void index_concurrently_set_dead (Oid heapId, Oid indexId)
 
ObjectAddress index_constraint_create (Relation heapRelation, Oid indexRelationId, Oid parentConstraintId, const IndexInfo *indexInfo, const char *constraintName, char constraintType, uint16 constr_flags, bool allow_system_table_mods, bool is_internal)
 
void index_drop (Oid indexId, bool concurrent, bool concurrent_lock_mode)
 
IndexInfo * BuildIndexInfo (Relation index)
 
IndexInfo * BuildDummyIndexInfo (Relation index)
 
bool CompareIndexInfo (const IndexInfo *info1, const IndexInfo *info2, const Oid *collations1, const Oid *collations2, const Oid *opfamilies1, const Oid *opfamilies2, const AttrMap *attmap)
 
void BuildSpeculativeIndexInfo (Relation index, IndexInfo *ii)
 
void FormIndexDatum (IndexInfo *indexInfo, TupleTableSlot *slot, EState *estate, Datum *values, bool *isnull)
 
void index_build (Relation heapRelation, Relation indexRelation, IndexInfo *indexInfo, bool isreindex, bool parallel, bool progress)
 
void validate_index (Oid heapId, Oid indexId, Snapshot snapshot)
 
void index_set_state_flags (Oid indexId, IndexStateFlagsAction action)
 
Oid IndexGetRelation (Oid indexId, bool missing_ok)
 
void reindex_index (const ReindexStmt *stmt, Oid indexId, bool skip_constraint_checks, char persistence, const ReindexParams *params)
 
bool reindex_relation (const ReindexStmt *stmt, Oid relid, int flags, const ReindexParams *params)
 
bool ReindexIsProcessingHeap (Oid heapOid)
 
bool ReindexIsProcessingIndex (Oid indexOid)
 
void ResetReindexState (int nestLevel)
 
Size EstimateReindexStateSpace (void)
 
void SerializeReindexState (Size maxsize, char *start_address)
 
void RestoreReindexState (const void *reindexstate)
 
void IndexSetParentIndex (Relation partitionIdx, Oid parentOid)
 
static int64 itemptr_encode (const ItemPointerData *itemptr)
 
static void itemptr_decode (ItemPointer itemptr, int64 encoded)
 

Macro Definition Documentation

◆ DEFAULT_INDEX_TYPE

#define DEFAULT_INDEX_TYPE   "btree"

Definition at line 27 of file index.h.

◆ INDEX_CONSTR_CREATE_DEFERRABLE

#define INDEX_CONSTR_CREATE_DEFERRABLE   (1 << 1)

Definition at line 99 of file index.h.

◆ INDEX_CONSTR_CREATE_INIT_DEFERRED

#define INDEX_CONSTR_CREATE_INIT_DEFERRED   (1 << 2)

Definition at line 100 of file index.h.

◆ INDEX_CONSTR_CREATE_MARK_AS_PRIMARY

#define INDEX_CONSTR_CREATE_MARK_AS_PRIMARY   (1 << 0)

Definition at line 98 of file index.h.

◆ INDEX_CONSTR_CREATE_REMOVE_OLD_DEPS

#define INDEX_CONSTR_CREATE_REMOVE_OLD_DEPS   (1 << 4)

Definition at line 102 of file index.h.

◆ INDEX_CONSTR_CREATE_UPDATE_INDEX

#define INDEX_CONSTR_CREATE_UPDATE_INDEX   (1 << 3)

Definition at line 101 of file index.h.

◆ INDEX_CONSTR_CREATE_WITHOUT_OVERLAPS

#define INDEX_CONSTR_CREATE_WITHOUT_OVERLAPS   (1 << 5)

Definition at line 103 of file index.h.

◆ INDEX_CREATE_ADD_CONSTRAINT

#define INDEX_CREATE_ADD_CONSTRAINT   (1 << 1)

Definition at line 68 of file index.h.

◆ INDEX_CREATE_CONCURRENT

#define INDEX_CREATE_CONCURRENT   (1 << 3)

Definition at line 70 of file index.h.

◆ INDEX_CREATE_IF_NOT_EXISTS

#define INDEX_CREATE_IF_NOT_EXISTS   (1 << 4)

Definition at line 71 of file index.h.

◆ INDEX_CREATE_INVALID

#define INDEX_CREATE_INVALID   (1 << 6)

Definition at line 73 of file index.h.

◆ INDEX_CREATE_IS_PRIMARY

#define INDEX_CREATE_IS_PRIMARY   (1 << 0)

Definition at line 67 of file index.h.

◆ INDEX_CREATE_PARTITIONED

#define INDEX_CREATE_PARTITIONED   (1 << 5)

Definition at line 72 of file index.h.

◆ INDEX_CREATE_SKIP_BUILD

#define INDEX_CREATE_SKIP_BUILD   (1 << 2)

Definition at line 69 of file index.h.

◆ INDEX_CREATE_SUPPRESS_PROGRESS

#define INDEX_CREATE_SUPPRESS_PROGRESS   (1 << 7)

Definition at line 74 of file index.h.

◆ REINDEX_REL_CHECK_CONSTRAINTS

#define REINDEX_REL_CHECK_CONSTRAINTS   0x04

Definition at line 168 of file index.h.

◆ REINDEX_REL_FORCE_INDEXES_PERMANENT

#define REINDEX_REL_FORCE_INDEXES_PERMANENT   0x10

Definition at line 170 of file index.h.

◆ REINDEX_REL_FORCE_INDEXES_UNLOGGED

#define REINDEX_REL_FORCE_INDEXES_UNLOGGED   0x08

Definition at line 169 of file index.h.

◆ REINDEX_REL_PROCESS_TOAST

#define REINDEX_REL_PROCESS_TOAST   0x01

Definition at line 166 of file index.h.

◆ REINDEX_REL_SUPPRESS_INDEX_USE

#define REINDEX_REL_SUPPRESS_INDEX_USE   0x02

Definition at line 167 of file index.h.

◆ REINDEXOPT_CONCURRENTLY

#define REINDEXOPT_CONCURRENTLY   0x08 /* concurrent mode */

Definition at line 50 of file index.h.

◆ REINDEXOPT_MISSING_OK

#define REINDEXOPT_MISSING_OK   0x04 /* skip missing relations */

Definition at line 49 of file index.h.

◆ REINDEXOPT_REPORT_PROGRESS

#define REINDEXOPT_REPORT_PROGRESS   0x02 /* report pgstat progress */

Definition at line 48 of file index.h.

◆ REINDEXOPT_VERBOSE

#define REINDEXOPT_VERBOSE   0x01 /* print progress info */

Definition at line 47 of file index.h.

Typedef Documentation

◆ AttrMap

Definition at line 24 of file index.h.

◆ ReindexParams

◆ ValidateIndexState

Enumeration Type Documentation

◆ IndexStateFlagsAction

Enumerator
INDEX_CREATE_SET_READY 
INDEX_CREATE_SET_VALID 
INDEX_DROP_CLEAR_VALID 
INDEX_DROP_SET_DEAD 

Definition at line 30 of file index.h.

31{
IndexStateFlagsAction
Definition index.h:31
@ INDEX_CREATE_SET_VALID
Definition index.h:33
@ INDEX_DROP_CLEAR_VALID
Definition index.h:34
@ INDEX_DROP_SET_DEAD
Definition index.h:35
@ INDEX_CREATE_SET_READY
Definition index.h:32

Function Documentation

◆ BuildDummyIndexInfo()

IndexInfo * BuildDummyIndexInfo ( Relation  index)
extern

Definition at line 2506 of file index.c.

2507{
2508 IndexInfo *ii;
2509 Form_pg_index indexStruct = index->rd_index;
2510 int i;
2511 int numAtts;
2512
2513 /* check the number of keys, and copy attr numbers into the IndexInfo */
2514 numAtts = indexStruct->indnatts;
2516 elog(ERROR, "invalid indnatts %d for index %u",
2518
2519 /*
2520 * Create the node, using dummy index expressions, and pretending there is
2521 * no predicate.
2522 */
2523 ii = makeIndexInfo(indexStruct->indnatts,
2524 indexStruct->indnkeyatts,
2525 index->rd_rel->relam,
2527 NIL,
2528 indexStruct->indisunique,
2529 indexStruct->indnullsnotdistinct,
2530 indexStruct->indisready,
2531 false,
2532 index->rd_indam->amsummarizing,
2533 indexStruct->indisexclusion && indexStruct->indisunique);
2534
2535 /* fill in attribute numbers */
2536 for (i = 0; i < numAtts; i++)
2537 ii->ii_IndexAttrNumbers[i] = indexStruct->indkey.values[i];
2538
2539 /* We ignore the exclusion constraint if any */
2540
2541 return ii;
2542}
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
int i
Definition isn.c:77
IndexInfo * makeIndexInfo(int numattrs, int numkeyattrs, Oid amoid, List *expressions, List *predicates, bool unique, bool nulls_not_distinct, bool isready, bool concurrent, bool summarizing, bool withoutoverlaps)
Definition makefuncs.c:834
#define INDEX_MAX_KEYS
END_CATALOG_STRUCT typedef FormData_pg_index * Form_pg_index
Definition pg_index.h:74
#define NIL
Definition pg_list.h:68
static int fb(int x)
#define RelationGetRelid(relation)
Definition rel.h:516
List * RelationGetDummyIndexExpressions(Relation relation)
Definition relcache.c:5168
AttrNumber ii_IndexAttrNumbers[INDEX_MAX_KEYS]
Definition execnodes.h:189
Definition type.h:97

References elog, ERROR, fb(), Form_pg_index, i, IndexInfo::ii_IndexAttrNumbers, INDEX_MAX_KEYS, makeIndexInfo(), NIL, RelationGetDummyIndexExpressions(), and RelationGetRelid.

Referenced by RelationTruncateIndexes().

◆ BuildIndexInfo()

IndexInfo * BuildIndexInfo ( Relation  index)
extern

Definition at line 2446 of file index.c.

2447{
2448 IndexInfo *ii;
2449 Form_pg_index indexStruct = index->rd_index;
2450 int i;
2451 int numAtts;
2452
2453 /* check the number of keys, and copy attr numbers into the IndexInfo */
2454 numAtts = indexStruct->indnatts;
2456 elog(ERROR, "invalid indnatts %d for index %u",
2458
2459 /*
2460 * Create the node, fetching any expressions needed for expressional
2461 * indexes and index predicate if any.
2462 */
2463 ii = makeIndexInfo(indexStruct->indnatts,
2464 indexStruct->indnkeyatts,
2465 index->rd_rel->relam,
2468 indexStruct->indisunique,
2469 indexStruct->indnullsnotdistinct,
2470 indexStruct->indisready,
2471 false,
2472 index->rd_indam->amsummarizing,
2473 indexStruct->indisexclusion && indexStruct->indisunique);
2474
2475 /* fill in attribute numbers */
2476 for (i = 0; i < numAtts; i++)
2477 ii->ii_IndexAttrNumbers[i] = indexStruct->indkey.values[i];
2478
2479 /* fetch exclusion constraint info if any */
2480 if (indexStruct->indisexclusion)
2481 {
2483 &ii->ii_ExclusionOps,
2484 &ii->ii_ExclusionProcs,
2485 &ii->ii_ExclusionStrats);
2486 }
2487
2488 return ii;
2489}
List * RelationGetIndexPredicate(Relation relation)
Definition relcache.c:5222
List * RelationGetIndexExpressions(Relation relation)
Definition relcache.c:5109
void RelationGetExclusionInfo(Relation indexRelation, Oid **operators, Oid **procs, uint16 **strategies)
Definition relcache.c:5665
uint16 * ii_ExclusionStrats
Definition execnodes.h:206
Oid * ii_ExclusionOps
Definition execnodes.h:202
Oid * ii_ExclusionProcs
Definition execnodes.h:204

References elog, ERROR, fb(), Form_pg_index, i, IndexInfo::ii_ExclusionOps, IndexInfo::ii_ExclusionProcs, IndexInfo::ii_ExclusionStrats, IndexInfo::ii_IndexAttrNumbers, INDEX_MAX_KEYS, makeIndexInfo(), RelationGetExclusionInfo(), RelationGetIndexExpressions(), RelationGetIndexPredicate(), and RelationGetRelid.

Referenced by _brin_parallel_scan_and_build(), _bt_parallel_scan_and_sort(), _gin_parallel_scan_and_build(), ATExecAddIndexConstraint(), ATExecAttachPartitionIdx(), AttachPartitionEnsureIndexes(), brinsummarize(), bt_check_every_level(), build_index_value_desc(), DefineIndex(), do_analyze_rel(), ExecOpenIndices(), index_concurrently_build(), index_create_copy(), recheck_matched_pk_tuple(), reindex_index(), tuplesort_begin_cluster(), unique_key_recheck(), and validate_index().

◆ BuildSpeculativeIndexInfo()

void BuildSpeculativeIndexInfo ( Relation  index,
IndexInfo *  ii 
)
extern

Definition at line 2699 of file index.c.

2700{
2701 int indnkeyatts;
2702 int i;
2703
2705
2706 /*
2707 * fetch info for checking unique indexes
2708 */
2709 Assert(ii->ii_Unique);
2710
2714
2715 /*
2716 * We have to look up the operator's strategy number. This provides a
2717 * cross-check that the operator does match the index.
2718 */
2719 /* We need the func OIDs and strategy numbers too */
2720 for (i = 0; i < indnkeyatts; i++)
2721 {
2722 ii->ii_UniqueStrats[i] =
2724 index->rd_rel->relam,
2725 index->rd_opfamily[i],
2726 false);
2727 ii->ii_UniqueOps[i] =
2728 get_opfamily_member(index->rd_opfamily[i],
2729 index->rd_opcintype[i],
2730 index->rd_opcintype[i],
2731 ii->ii_UniqueStrats[i]);
2732 if (!OidIsValid(ii->ii_UniqueOps[i]))
2733 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
2734 ii->ii_UniqueStrats[i], index->rd_opcintype[i],
2735 index->rd_opcintype[i], index->rd_opfamily[i]);
2737 }
2738}
StrategyNumber IndexAmTranslateCompareType(CompareType cmptype, Oid amoid, Oid opfamily, bool missing_ok)
Definition amapi.c:161
#define Assert(condition)
Definition c.h:1002
uint16_t uint16
Definition c.h:682
#define OidIsValid(objectId)
Definition c.h:917
@ COMPARE_EQ
Definition cmptype.h:36
#define palloc_array(type, count)
Definition fe_memutils.h:91
RegProcedure get_opcode(Oid opno)
Definition lsyscache.c:1585
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition lsyscache.c:170
unsigned int Oid
#define IndexRelationGetNumberOfKeyAttributes(relation)
Definition rel.h:535
bool ii_Unique
Definition execnodes.h:214
Oid * ii_UniqueOps
Definition execnodes.h:209
uint16 * ii_UniqueStrats
Definition execnodes.h:211
Oid * ii_UniqueProcs
Definition execnodes.h:210

References Assert, COMPARE_EQ, elog, ERROR, fb(), get_opcode(), get_opfamily_member(), i, IndexInfo::ii_Unique, IndexInfo::ii_UniqueOps, IndexInfo::ii_UniqueProcs, IndexInfo::ii_UniqueStrats, IndexAmTranslateCompareType(), IndexRelationGetNumberOfKeyAttributes, OidIsValid, and palloc_array.

Referenced by BuildConflictIndexInfo(), and ExecOpenIndices().

◆ CompareIndexInfo()

bool CompareIndexInfo ( const IndexInfo *  info1,
const IndexInfo *  info2,
const Oid *  collations1,
const Oid *  collations2,
const Oid *  opfamilies1,
const Oid *  opfamilies2,
const AttrMap *  attmap 
)
extern

Definition at line 2555 of file index.c.

2559{
2560 int i;
2561
2562 if (info1->ii_Unique != info2->ii_Unique)
2563 return false;
2564
2565 if (info1->ii_NullsNotDistinct != info2->ii_NullsNotDistinct)
2566 return false;
2567
2568 /* indexes are only equivalent if they have the same access method */
2569 if (info1->ii_Am != info2->ii_Am)
2570 return false;
2571
2572 /* and same number of attributes */
2573 if (info1->ii_NumIndexAttrs != info2->ii_NumIndexAttrs)
2574 return false;
2575
2576 /* and same number of key attributes */
2577 if (info1->ii_NumIndexKeyAttrs != info2->ii_NumIndexKeyAttrs)
2578 return false;
2579
2580 /*
2581 * and columns match through the attribute map (actual attribute numbers
2582 * might differ!) Note that this checks that index columns that are
2583 * expressions appear in the same positions. We will next compare the
2584 * expressions themselves.
2585 */
2586 for (i = 0; i < info1->ii_NumIndexAttrs; i++)
2587 {
2588 if (attmap->maplen < info2->ii_IndexAttrNumbers[i])
2589 elog(ERROR, "incorrect attribute map");
2590
2591 /* ignore expressions for now (but check their collation/opfamily) */
2592 if (!(info1->ii_IndexAttrNumbers[i] == InvalidAttrNumber &&
2593 info2->ii_IndexAttrNumbers[i] == InvalidAttrNumber))
2594 {
2595 /* fail if just one index has an expression in this column */
2596 if (info1->ii_IndexAttrNumbers[i] == InvalidAttrNumber ||
2597 info2->ii_IndexAttrNumbers[i] == InvalidAttrNumber)
2598 return false;
2599
2600 /* both are columns, so check for match after mapping */
2601 if (attmap->attnums[info2->ii_IndexAttrNumbers[i] - 1] !=
2602 info1->ii_IndexAttrNumbers[i])
2603 return false;
2604 }
2605
2606 /* collation and opfamily are not valid for included columns */
2607 if (i >= info1->ii_NumIndexKeyAttrs)
2608 continue;
2609
2610 if (collations1[i] != collations2[i])
2611 return false;
2612 if (opfamilies1[i] != opfamilies2[i])
2613 return false;
2614 }
2615
2616 /*
2617 * For expression indexes: either both are expression indexes, or neither
2618 * is; if they are, make sure the expressions match.
2619 */
2620 if ((info1->ii_Expressions != NIL) != (info2->ii_Expressions != NIL))
2621 return false;
2622 if (info1->ii_Expressions != NIL)
2623 {
2624 bool found_whole_row;
2625 Node *mapped;
2626
2627 mapped = map_variable_attnos((Node *) info2->ii_Expressions,
2628 1, 0, attmap,
2629 InvalidOid, &found_whole_row);
2630 if (found_whole_row)
2631 {
2632 /*
2633 * we could throw an error here, but seems out of scope for this
2634 * routine.
2635 */
2636 return false;
2637 }
2638
2639 if (!equal(info1->ii_Expressions, mapped))
2640 return false;
2641 }
2642
2643 /* Partial index predicates must be identical, if they exist */
2644 if ((info1->ii_Predicate == NULL) != (info2->ii_Predicate == NULL))
2645 return false;
2646 if (info1->ii_Predicate != NULL)
2647 {
2648 bool found_whole_row;
2649 Node *mapped;
2650
2651 mapped = map_variable_attnos((Node *) info2->ii_Predicate,
2652 1, 0, attmap,
2653 InvalidOid, &found_whole_row);
2654 if (found_whole_row)
2655 {
2656 /*
2657 * we could throw an error here, but seems out of scope for this
2658 * routine.
2659 */
2660 return false;
2661 }
2662 if (!equal(info1->ii_Predicate, mapped))
2663 return false;
2664 }
2665
2666 /* If they're exclusion indexes, their properties must be identical */
2667 if ((info1->ii_ExclusionOps == NULL) != (info2->ii_ExclusionOps == NULL))
2668 return false;
2669 if (info1->ii_ExclusionOps != NULL)
2670 {
2671 for (i = 0; i < info1->ii_NumIndexKeyAttrs; i++)
2672 {
2673 if (info1->ii_ExclusionOps[i] != info2->ii_ExclusionOps[i])
2674 return false;
2675 if (info1->ii_ExclusionProcs[i] != info2->ii_ExclusionProcs[i])
2676 return false;
2677 if (info1->ii_ExclusionStrats[i] != info2->ii_ExclusionStrats[i])
2678 return false;
2679 }
2680 }
2681
2682 return true;
2683}
#define InvalidAttrNumber
Definition attnum.h:23
bool equal(const void *a, const void *b)
Definition equalfuncs.c:223
#define InvalidOid
Node * map_variable_attnos(Node *node, int target_varno, int sublevels_up, const AttrMap *attno_map, Oid to_rowtype, bool *found_whole_row)
Definition nodes.h:133

References elog, equal(), ERROR, fb(), i, InvalidAttrNumber, InvalidOid, map_variable_attnos(), and NIL.

Referenced by ATExecAttachPartitionIdx(), AttachPartitionEnsureIndexes(), and DefineIndex().

◆ EstimateReindexStateSpace()

Size EstimateReindexStateSpace ( void  )
extern

Definition at line 4275 of file index.c.

4276{
4279}
static List * pendingReindexedIndexes
Definition index.c:4144
Size mul_size(Size s1, Size s2)
Definition mcxt.c:1752
static int list_length(const List *l)
Definition pg_list.h:152

References fb(), list_length(), mul_size(), and pendingReindexedIndexes.

Referenced by InitializeParallelDSM().

◆ FormIndexDatum()

void FormIndexDatum ( IndexInfo *  indexInfo,
TupleTableSlot *  slot,
EState *  estate,
Datum *  values,
bool *  isnull 
)
extern

Definition at line 2760 of file index.c.

2765{
2767 int i;
2768
2769 if (indexInfo->ii_Expressions != NIL &&
2770 indexInfo->ii_ExpressionsState == NIL)
2771 {
2772 /* First time through, set up expression evaluation state */
2773 indexInfo->ii_ExpressionsState =
2774 ExecPrepareExprList(indexInfo->ii_Expressions, estate);
2775 /* Check caller has set up context correctly */
2776 Assert(GetPerTupleExprContext(estate)->ecxt_scantuple == slot);
2777 }
2779
2780 for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++)
2781 {
2782 int keycol = indexInfo->ii_IndexAttrNumbers[i];
2783 Datum iDatum;
2784 bool isNull;
2785
2786 if (keycol < 0)
2787 iDatum = slot_getsysattr(slot, keycol, &isNull);
2788 else if (keycol != 0)
2789 {
2790 /*
2791 * Plain index column; get the value we need directly from the
2792 * heap tuple.
2793 */
2794 iDatum = slot_getattr(slot, keycol, &isNull);
2795 }
2796 else
2797 {
2798 /*
2799 * Index expression --- need to evaluate it.
2800 */
2801 if (indexpr_item == NULL)
2802 elog(ERROR, "wrong number of index expressions");
2804 GetPerTupleExprContext(estate),
2805 &isNull);
2807 }
2808 values[i] = iDatum;
2809 isnull[i] = isNull;
2810 }
2811
2812 if (indexpr_item != NULL)
2813 elog(ERROR, "wrong number of index expressions");
2814}
static Datum values[MAXATTR]
Definition bootstrap.c:190
List * ExecPrepareExprList(List *nodes, EState *estate)
Definition execExpr.c:839
#define GetPerTupleExprContext(estate)
Definition executor.h:665
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition executor.h:444
#define lfirst(lc)
Definition pg_list.h:172
static ListCell * list_head(const List *l)
Definition pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition pg_list.h:375
uint64_t Datum
Definition postgres.h:70
int ii_NumIndexAttrs
Definition execnodes.h:181
List * ii_ExpressionsState
Definition execnodes.h:194
List * ii_Expressions
Definition execnodes.h:192
static Datum slot_getsysattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition tuptable.h:438
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition tuptable.h:417

References Assert, elog, ERROR, ExecEvalExprSwitchContext(), ExecPrepareExprList(), fb(), GetPerTupleExprContext, i, IndexInfo::ii_Expressions, IndexInfo::ii_ExpressionsState, IndexInfo::ii_IndexAttrNumbers, IndexInfo::ii_NumIndexAttrs, lfirst, list_head(), lnext(), NIL, slot_getattr(), slot_getsysattr(), and values.

Referenced by build_index_value_desc(), CatalogIndexInsert(), check_exclusion_or_unique_constraint(), comparetup_cluster_tiebreak(), compute_index_stats(), ExecCheckIndexConstraints(), ExecInsertIndexTuples(), heapam_index_build_range_scan(), heapam_index_validate_scan(), IndexCheckExclusion(), recheck_matched_pk_tuple(), and unique_key_recheck().

◆ index_build()

void index_build ( Relation  heapRelation,
Relation  indexRelation,
IndexInfo *  indexInfo,
bool  isreindex,
bool  parallel,
bool  progress 
)
extern

Definition at line 3033 of file index.c.

3039{
3040 IndexBuildResult *stats;
3041 Oid save_userid;
3042 int save_sec_context;
3043 int save_nestlevel;
3044
3045 /*
3046 * sanity checks
3047 */
3048 Assert(RelationIsValid(indexRelation));
3049 Assert(indexRelation->rd_indam);
3050 Assert(indexRelation->rd_indam->ambuild);
3051 Assert(indexRelation->rd_indam->ambuildempty);
3052
3053 /*
3054 * Determine worker process details for parallel CREATE INDEX. Currently,
3055 * only btree, GIN, and BRIN have support for parallel builds.
3056 *
3057 * Note that planner considers parallel safety for us.
3058 */
3060 indexRelation->rd_indam->amcanbuildparallel)
3061 indexInfo->ii_ParallelWorkers =
3063 RelationGetRelid(indexRelation));
3064
3065 if (indexInfo->ii_ParallelWorkers == 0)
3067 (errmsg_internal("building index \"%s\" on table \"%s\" serially",
3068 RelationGetRelationName(indexRelation),
3069 RelationGetRelationName(heapRelation))));
3070 else
3072 (errmsg_internal("building index \"%s\" on table \"%s\" with request for %d parallel workers",
3073 RelationGetRelationName(indexRelation),
3074 RelationGetRelationName(heapRelation),
3075 indexInfo->ii_ParallelWorkers)));
3076
3077 /*
3078 * Switch to the table owner's userid, so that any index functions are run
3079 * as that user. Also lock down security-restricted operations and
3080 * arrange to make GUC variable changes local to this command.
3081 */
3082 GetUserIdAndSecContext(&save_userid, &save_sec_context);
3083 SetUserIdAndSecContext(heapRelation->rd_rel->relowner,
3084 save_sec_context | SECURITY_RESTRICTED_OPERATION);
3085 save_nestlevel = NewGUCNestLevel();
3087
3088 /* Set up initial progress report status */
3089 if (progress)
3090 {
3091 const int progress_index[] = {
3098 };
3099 const int64 progress_vals[] = {
3102 0, 0, 0, 0
3103 };
3104
3106 }
3107
3108 /*
3109 * Call the access method's build procedure
3110 */
3111 stats = indexRelation->rd_indam->ambuild(heapRelation, indexRelation,
3112 indexInfo);
3113 Assert(stats);
3114
3115 /*
3116 * If this is an unlogged index, we may need to write out an init fork for
3117 * it -- but we must first check whether one already exists. If, for
3118 * example, an unlogged relation is truncated in the transaction that
3119 * created it, or truncated twice in a subsequent transaction, the
3120 * relfilenumber won't change, and nothing needs to be done here.
3121 */
3122 if (indexRelation->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED &&
3123 !smgrexists(RelationGetSmgr(indexRelation), INIT_FORKNUM))
3124 {
3125 smgrcreate(RelationGetSmgr(indexRelation), INIT_FORKNUM, false);
3126 log_smgrcreate(&indexRelation->rd_locator, INIT_FORKNUM);
3127 indexRelation->rd_indam->ambuildempty(indexRelation);
3128 }
3129
3130 /*
3131 * If we found any potentially broken HOT chains, mark the index as not
3132 * being usable until the current transaction is below the event horizon.
3133 * See src/backend/access/heap/README.HOT for discussion. While it might
3134 * become safe to use the index earlier based on actual cleanup activity
3135 * and other active transactions, the test for that would be much more
3136 * complex and would require some form of blocking, so keep it simple and
3137 * fast by just using the current transaction.
3138 *
3139 * However, when reindexing an existing index, we should do nothing here.
3140 * Any HOT chains that are broken with respect to the index must predate
3141 * the index's original creation, so there is no need to change the
3142 * index's usability horizon. Moreover, we *must not* try to change the
3143 * index's pg_index entry while reindexing pg_index itself, and this
3144 * optimization nicely prevents that. The more complex rules needed for a
3145 * reindex are handled separately after this function returns.
3146 *
3147 * We also need not set indcheckxmin during a concurrent index build,
3148 * because we won't set indisvalid true until all transactions that care
3149 * about the broken HOT chains are gone.
3150 *
3151 * Therefore, this code path can only be taken during non-concurrent
3152 * CREATE INDEX. Thus the fact that heap_update will set the pg_index
3153 * tuple's xmin doesn't matter, because that tuple was created in the
3154 * current transaction anyway. That also means we don't need to worry
3155 * about any concurrent readers of the tuple; no other transaction can see
3156 * it yet.
3157 */
3158 if (indexInfo->ii_BrokenHotChain &&
3159 !isreindex &&
3160 !indexInfo->ii_Concurrent)
3161 {
3162 Oid indexId = RelationGetRelid(indexRelation);
3166
3168
3172 elog(ERROR, "cache lookup failed for index %u", indexId);
3174
3175 /* If it's a new index, indcheckxmin shouldn't be set ... */
3176 Assert(!indexForm->indcheckxmin);
3177
3178 indexForm->indcheckxmin = true;
3180
3183 }
3184
3185 /*
3186 * Update heap and index pg_class rows
3187 */
3188 index_update_stats(heapRelation,
3189 true,
3190 stats->heap_tuples);
3191
3192 index_update_stats(indexRelation,
3193 false,
3194 stats->index_tuples);
3195
3196 /* Make the updated catalog row versions visible */
3198
3199 /*
3200 * If it's for an exclusion constraint, make a second pass over the heap
3201 * to verify that the constraint is satisfied. We must not do this until
3202 * the index is fully valid. (Broken HOT chains shouldn't matter, though;
3203 * see comments for IndexCheckExclusion.)
3204 */
3205 if (indexInfo->ii_ExclusionOps != NULL)
3206 IndexCheckExclusion(heapRelation, indexRelation, indexInfo);
3207
3208 /* Roll back any GUC changes executed by index functions */
3209 AtEOXact_GUC(false, save_nestlevel);
3210
3211 /* Restore userid and security context */
3212 SetUserIdAndSecContext(save_userid, save_sec_context);
3213}
void pgstat_progress_update_multi_param(int nparam, const int *index, const int64 *val)
int64_t int64
Definition c.h:680
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define DEBUG1
Definition elog.h:31
#define ereport(elevel,...)
Definition elog.h:152
int NewGUCNestLevel(void)
Definition guc.c:2142
void RestrictSearchPath(void)
Definition guc.c:2153
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition guc.c:2169
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1372
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
static void index_update_stats(Relation rel, bool hasindex, double reltuples)
Definition index.c:2839
static void IndexCheckExclusion(Relation heapRelation, Relation indexRelation, IndexInfo *indexInfo)
Definition index.c:3228
void CatalogTupleUpdate(Relation heapRel, const ItemPointerData *otid, HeapTuple tup)
Definition indexing.c:313
#define RowExclusiveLock
Definition lockdefs.h:38
#define SECURITY_RESTRICTED_OPERATION
Definition miscadmin.h:322
#define IsNormalProcessingMode()
Definition miscadmin.h:488
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition miscinit.c:613
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition miscinit.c:620
static int progress
Definition pgbench.c:262
int plan_create_index_workers(Oid tableOid, Oid indexOid)
Definition planner.c:7212
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
#define PROGRESS_CREATEIDX_PHASE_BUILD
Definition progress.h:123
#define PROGRESS_CREATEIDX_TUPLES_TOTAL
Definition progress.h:115
#define PROGRESS_SCAN_BLOCKS_DONE
Definition progress.h:151
#define PROGRESS_CREATEIDX_TUPLES_DONE
Definition progress.h:116
#define PROGRESS_CREATEIDX_SUBPHASE
Definition progress.h:114
#define PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE
Definition progress.h:135
#define PROGRESS_CREATEIDX_PHASE
Definition progress.h:113
#define PROGRESS_SCAN_BLOCKS_TOTAL
Definition progress.h:150
static SMgrRelation RelationGetSmgr(Relation rel)
Definition rel.h:578
#define RelationGetRelationName(relation)
Definition rel.h:550
#define RelationIsValid(relation)
Definition rel.h:491
@ INIT_FORKNUM
Definition relpath.h:61
void smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition smgr.c:481
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Definition smgr.c:462
void log_smgrcreate(const RelFileLocator *rlocator, ForkNumber forkNum)
Definition storage.c:187
ambuildempty_function ambuildempty
Definition amapi.h:297
ambuild_function ambuild
Definition amapi.h:296
bool amcanbuildparallel
Definition amapi.h:277
double heap_tuples
Definition genam.h:40
double index_tuples
Definition genam.h:41
bool ii_BrokenHotChain
Definition execnodes.h:226
int ii_ParallelWorkers
Definition execnodes.h:232
bool ii_Concurrent
Definition execnodes.h:224
const struct IndexAmRoutine * rd_indam
Definition rel.h:206
RelFileLocator rd_locator
Definition rel.h:57
Form_pg_class rd_rel
Definition rel.h:111
#define SearchSysCacheCopy1(cacheId, key1)
Definition syscache.h:91
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40
void CommandCounterIncrement(void)
Definition xact.c:1130

References IndexAmRoutine::ambuild, IndexAmRoutine::ambuildempty, IndexAmRoutine::amcanbuildparallel, Assert, AtEOXact_GUC(), CatalogTupleUpdate(), CommandCounterIncrement(), DEBUG1, elog, ereport, errmsg_internal(), ERROR, fb(), Form_pg_index, GETSTRUCT(), GetUserIdAndSecContext(), heap_freetuple(), IndexBuildResult::heap_tuples, HeapTupleIsValid, IndexInfo::ii_BrokenHotChain, IndexInfo::ii_Concurrent, IndexInfo::ii_ExclusionOps, IndexInfo::ii_ParallelWorkers, IndexBuildResult::index_tuples, index_update_stats(), IndexCheckExclusion(), INIT_FORKNUM, IsNormalProcessingMode, log_smgrcreate(), NewGUCNestLevel(), ObjectIdGetDatum(), pgstat_progress_update_multi_param(), plan_create_index_workers(), progress, PROGRESS_CREATEIDX_PHASE, PROGRESS_CREATEIDX_PHASE_BUILD, PROGRESS_CREATEIDX_SUBPHASE, PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE, PROGRESS_CREATEIDX_TUPLES_DONE, PROGRESS_CREATEIDX_TUPLES_TOTAL, PROGRESS_SCAN_BLOCKS_DONE, PROGRESS_SCAN_BLOCKS_TOTAL, RelationData::rd_indam, RelationData::rd_locator, RelationData::rd_rel, RelationGetRelationName, RelationGetRelid, RelationGetSmgr(), RelationIsValid, RestrictSearchPath(), RowExclusiveLock, SearchSysCacheCopy1, SECURITY_RESTRICTED_OPERATION, SetUserIdAndSecContext(), smgrcreate(), smgrexists(), table_close(), and table_open().

Referenced by build_indices(), index_concurrently_build(), index_create(), reindex_index(), and RelationTruncateIndexes().

◆ index_check_primary_key()

void index_check_primary_key ( Relation  heapRel,
const IndexInfo *  indexInfo,
bool  is_alter_table,
const IndexStmt *  stmt 
)
extern

Definition at line 203 of file index.c.

207{
208 int i;
209
210 /*
211 * If ALTER TABLE or CREATE TABLE .. PARTITION OF, check that there isn't
212 * already a PRIMARY KEY. In CREATE TABLE for an ordinary relation, we
213 * have faith that the parser rejected multiple pkey clauses; and CREATE
214 * INDEX doesn't have a way to say PRIMARY KEY, so it's no problem either.
215 */
216 if ((is_alter_table || heapRel->rd_rel->relispartition) &&
217 relationHasPrimaryKey(heapRel))
218 {
221 errmsg("multiple primary keys for table \"%s\" are not allowed",
222 RelationGetRelationName(heapRel))));
223 }
224
225 /*
226 * Indexes created with NULLS NOT DISTINCT cannot be used for primary key
227 * constraints. While there is no direct syntax to reach here, it can be
228 * done by creating a separate index and attaching it via ALTER TABLE ..
229 * USING INDEX.
230 */
231 if (indexInfo->ii_NullsNotDistinct)
232 {
235 errmsg("primary keys cannot use NULLS NOT DISTINCT indexes")));
236 }
237
238 /*
239 * Check that all of the attributes in a primary key are marked as not
240 * null. (We don't really expect to see that; it'd mean the parser messed
241 * up. But it seems wise to check anyway.)
242 */
243 for (i = 0; i < indexInfo->ii_NumIndexKeyAttrs; i++)
244 {
248
249 if (attnum == 0)
252 errmsg("primary keys cannot be expressions")));
253
254 /* System attributes are never null, so no need to check */
255 if (attnum < 0)
256 continue;
257
262 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
263 attnum, RelationGetRelid(heapRel));
265
266 if (!attform->attnotnull)
269 errmsg("primary key column \"%s\" is not marked NOT NULL",
270 NameStr(attform->attname))));
271
273 }
274}
int16 AttrNumber
Definition attnum.h:21
#define NameStr(name)
Definition c.h:894
int errcode(int sqlerrcode)
Definition elog.c:875
static bool relationHasPrimaryKey(Relation rel)
Definition index.c:149
static char * errmsg
int16 attnum
FormData_pg_attribute * Form_pg_attribute
static Datum Int16GetDatum(int16 X)
Definition postgres.h:172
bool ii_NullsNotDistinct
Definition execnodes.h:216
int ii_NumIndexKeyAttrs
Definition execnodes.h:183
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:265
HeapTuple SearchSysCache2(SysCacheIdentifier cacheId, Datum key1, Datum key2)
Definition syscache.c:231

References attnum, elog, ereport, errcode(), errmsg, ERROR, fb(), GETSTRUCT(), HeapTupleIsValid, i, IndexInfo::ii_IndexAttrNumbers, IndexInfo::ii_NullsNotDistinct, IndexInfo::ii_NumIndexKeyAttrs, Int16GetDatum(), NameStr, ObjectIdGetDatum(), RelationData::rd_rel, RelationGetRelationName, RelationGetRelid, relationHasPrimaryKey(), ReleaseSysCache(), and SearchSysCache2().

Referenced by ATExecAddIndexConstraint(), and DefineIndex().

◆ index_concurrently_build()

void index_concurrently_build ( Oid  heapRelationId,
Oid  indexRelationId 
)
extern

Definition at line 1503 of file index.c.

1505{
1506 Relation heapRel;
1507 Oid save_userid;
1508 int save_sec_context;
1509 int save_nestlevel;
1510 Relation indexRelation;
1511 IndexInfo *indexInfo;
1512
1513 /* This had better make sure that a snapshot is active */
1515
1516 /* Open and lock the parent heap relation */
1518
1519 /*
1520 * Switch to the table owner's userid, so that any index functions are run
1521 * as that user. Also lock down security-restricted operations and
1522 * arrange to make GUC variable changes local to this command.
1523 */
1524 GetUserIdAndSecContext(&save_userid, &save_sec_context);
1525 SetUserIdAndSecContext(heapRel->rd_rel->relowner,
1526 save_sec_context | SECURITY_RESTRICTED_OPERATION);
1527 save_nestlevel = NewGUCNestLevel();
1529
1531
1532 /*
1533 * We have to re-build the IndexInfo struct, since it was lost in the
1534 * commit of the transaction where this concurrent index was created at
1535 * the catalog level.
1536 */
1537 indexInfo = BuildIndexInfo(indexRelation);
1538 Assert(!indexInfo->ii_ReadyForInserts);
1539 indexInfo->ii_Concurrent = true;
1540 indexInfo->ii_BrokenHotChain = false;
1541
1542 /* Now build the index */
1543 index_build(heapRel, indexRelation, indexInfo, false, true, true);
1544
1545 /* Roll back any GUC changes executed by index functions */
1546 AtEOXact_GUC(false, save_nestlevel);
1547
1548 /* Restore userid and security context */
1549 SetUserIdAndSecContext(save_userid, save_sec_context);
1550
1551 /* Close both the relations, but keep the locks */
1552 table_close(heapRel, NoLock);
1553 index_close(indexRelation, NoLock);
1554
1555 /*
1556 * Update the pg_index row to mark the index as ready for inserts. Once we
1557 * commit this transaction, any new transactions that open the table must
1558 * insert new entries into the index for insertions and non-HOT updates.
1559 */
1561}
void index_build(Relation heapRelation, Relation indexRelation, IndexInfo *indexInfo, bool isreindex, bool parallel, bool progress)
Definition index.c:3033
void index_set_state_flags(Oid indexId, IndexStateFlagsAction action)
Definition index.c:3536
IndexInfo * BuildIndexInfo(Relation index)
Definition index.c:2446
void index_close(Relation relation, LOCKMODE lockmode)
Definition indexam.c:178
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition indexam.c:134
#define NoLock
Definition lockdefs.h:34
#define ShareUpdateExclusiveLock
Definition lockdefs.h:39
bool ActiveSnapshotSet(void)
Definition snapmgr.c:812
bool ii_ReadyForInserts
Definition execnodes.h:218

References ActiveSnapshotSet(), Assert, AtEOXact_GUC(), BuildIndexInfo(), fb(), GetUserIdAndSecContext(), IndexInfo::ii_BrokenHotChain, IndexInfo::ii_Concurrent, IndexInfo::ii_ReadyForInserts, index_build(), index_close(), INDEX_CREATE_SET_READY, index_open(), index_set_state_flags(), NewGUCNestLevel(), NoLock, RelationData::rd_rel, RestrictSearchPath(), RowExclusiveLock, SECURITY_RESTRICTED_OPERATION, SetUserIdAndSecContext(), ShareUpdateExclusiveLock, table_close(), and table_open().

Referenced by DefineIndex(), and ReindexRelationConcurrently().

◆ index_concurrently_set_dead()

void index_concurrently_set_dead ( Oid  heapId,
Oid  indexId 
)
extern

Definition at line 1841 of file index.c.

1842{
1845
1846 /*
1847 * No more predicate locks will be acquired on this index, and we're about
1848 * to stop doing inserts into the index which could show conflicts with
1849 * existing predicate locks, so now is the time to move them to the heap
1850 * relation.
1851 */
1855
1856 /*
1857 * Now we are sure that nobody uses the index for queries; they just might
1858 * have it open for updating it. So now we can unset indisready and
1859 * indislive, then wait till nobody could be using it at all anymore.
1860 */
1862
1863 /*
1864 * Invalidate the relcache for the table, so that after this commit all
1865 * sessions will refresh the table's index list. Forgetting just the
1866 * index's relcache entry is not enough.
1867 */
1869
1870 /*
1871 * Close the relations again, though still holding session lock.
1872 */
1875}
void CacheInvalidateRelcache(Relation relation)
Definition inval.c:1632
void TransferPredicateLocksToHeapRelation(Relation relation)
Definition predicate.c:3052

References CacheInvalidateRelcache(), fb(), index_close(), INDEX_DROP_SET_DEAD, index_open(), index_set_state_flags(), NoLock, ShareUpdateExclusiveLock, table_close(), table_open(), and TransferPredicateLocksToHeapRelation().

Referenced by index_drop(), and ReindexRelationConcurrently().

◆ index_concurrently_swap()

void index_concurrently_swap ( Oid  newIndexId,
Oid  oldIndexId,
const char *  oldName 
)
extern

Definition at line 1570 of file index.c.

1571{
1573 pg_index,
1575 pg_trigger;
1586 bool isPartition;
1589 ListCell *lc;
1590
1591 /*
1592 * Take a necessary lock on the old and new index before swapping them.
1593 */
1596
1597 /* Now swap names and dependencies of those indexes */
1599
1603 elog(ERROR, "could not find tuple for relation %u", oldIndexId);
1607 elog(ERROR, "could not find tuple for relation %u", newIndexId);
1608
1611
1612 /* Swap the names */
1613 namestrcpy(&newClassForm->relname, NameStr(oldClassForm->relname));
1614 namestrcpy(&oldClassForm->relname, oldName);
1615
1616 /* Swap the partition flags to track inheritance properly */
1617 isPartition = newClassForm->relispartition;
1618 newClassForm->relispartition = oldClassForm->relispartition;
1619 oldClassForm->relispartition = isPartition;
1620
1623
1626
1627 /* Now swap index info */
1629
1633 elog(ERROR, "could not find tuple for relation %u", oldIndexId);
1637 elog(ERROR, "could not find tuple for relation %u", newIndexId);
1638
1641
1642 /*
1643 * Copy constraint flags from the old index. This is safe because the old
1644 * index guaranteed uniqueness.
1645 */
1646 newIndexForm->indisprimary = oldIndexForm->indisprimary;
1647 oldIndexForm->indisprimary = false;
1648 newIndexForm->indisexclusion = oldIndexForm->indisexclusion;
1649 oldIndexForm->indisexclusion = false;
1650 newIndexForm->indimmediate = oldIndexForm->indimmediate;
1651 oldIndexForm->indimmediate = true;
1652
1653 /* Preserve indisreplident in the new index */
1654 newIndexForm->indisreplident = oldIndexForm->indisreplident;
1655
1656 /* Preserve indisclustered in the new index */
1657 newIndexForm->indisclustered = oldIndexForm->indisclustered;
1658
1659 /*
1660 * Mark the new index as valid, and the old index as invalid similarly to
1661 * what index_set_state_flags() does.
1662 */
1663 newIndexForm->indisvalid = true;
1664 oldIndexForm->indisvalid = false;
1665 oldIndexForm->indisclustered = false;
1666 oldIndexForm->indisreplident = false;
1667
1670
1673
1674 /*
1675 * Move constraints and triggers over to the new index
1676 */
1677
1679
1681
1684
1687
1688 foreach(lc, constraintOids)
1689 {
1693 ScanKeyData key[1];
1694 SysScanDesc scan;
1696
1697 /* Move the constraint from the old to the new index */
1701 elog(ERROR, "could not find tuple for constraint %u", constraintOid);
1702
1704
1705 if (conForm->conindid == oldIndexId)
1706 {
1707 conForm->conindid = newIndexId;
1708
1710 }
1711
1713
1714 /* Search for trigger records */
1715 ScanKeyInit(&key[0],
1719
1721 NULL, 1, key);
1722
1724 {
1726
1727 if (tgForm->tgconstrindid != oldIndexId)
1728 continue;
1729
1730 /* Make a modifiable copy */
1733
1734 tgForm->tgconstrindid = newIndexId;
1735
1737
1739 }
1740
1741 systable_endscan(scan);
1742 }
1743
1744 /*
1745 * Move comment if any
1746 */
1747 {
1749 ScanKeyData skey[3];
1751 HeapTuple tuple;
1753 bool nulls[Natts_pg_description] = {0};
1754 bool replaces[Natts_pg_description] = {0};
1755
1758
1759 ScanKeyInit(&skey[0],
1763 ScanKeyInit(&skey[1],
1767 ScanKeyInit(&skey[2],
1770 Int32GetDatum(0));
1771
1773
1775 NULL, 3, skey);
1776
1777 while ((tuple = systable_getnext(sd)) != NULL)
1778 {
1780 values, nulls, replaces);
1781 CatalogTupleUpdate(description, &tuple->t_self, tuple);
1782
1783 break; /* Assume there can be only one match */
1784 }
1785
1788 }
1789
1790 /*
1791 * Swap inheritance relationship with parent index
1792 */
1794 {
1796 Oid parentIndexRelid = linitial_oid(ancestors);
1797
1800
1801 list_free(ancestors);
1802 }
1803
1804 /*
1805 * Swap all dependencies of and on the old index to the new one, and
1806 * vice-versa. Note that a call to CommandCounterIncrement() would cause
1807 * duplicate entries in pg_depend, so this should not be done.
1808 */
1811
1814
1815 /* copy over statistics from old to new index */
1817
1818 /* Copy data of pg_statistic from the old index to the new one */
1820
1821 /* Close relations */
1826
1827 /* The lock taken previously is not released until the end of transaction */
1830}
void systable_endscan(SysScanDesc sysscan)
Definition genam.c:604
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition genam.c:515
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition genam.c:388
void CopyStatistics(Oid fromrelid, Oid torelid)
Definition heap.c:3462
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition heaptuple.c:1118
HeapTuple heap_copytuple(HeapTuple tuple)
Definition heaptuple.c:686
List * lappend_oid(List *list, Oid datum)
Definition list.c:375
void list_free(List *list)
Definition list.c:1546
bool get_rel_relispartition(Oid relid)
Definition lsyscache.c:2341
void namestrcpy(Name name, const char *str)
Definition name.c:233
List * get_partition_ancestors(Oid relid)
Definition partition.c:134
FormData_pg_class * Form_pg_class
Definition pg_class.h:160
END_CATALOG_STRUCT typedef FormData_pg_constraint * Form_pg_constraint
long changeDependenciesOf(Oid classId, Oid oldObjectId, Oid newObjectId)
Definition pg_depend.c:585
List * get_index_ref_constraints(Oid indexId)
Definition pg_depend.c:1248
long changeDependenciesOn(Oid refClassId, Oid oldRefObjectId, Oid newRefObjectId)
Definition pg_depend.c:641
Oid get_index_constraint(Oid indexId)
Definition pg_depend.c:1192
bool DeleteInheritsTuple(Oid inhrelid, Oid inhparent, bool expect_detach_pending, const char *childname)
void StoreSingleInheritance(Oid relationId, Oid parentOid, int32 seqNumber)
#define linitial_oid(l)
Definition pg_list.h:180
#define lfirst_oid(lc)
Definition pg_list.h:174
END_CATALOG_STRUCT typedef FormData_pg_trigger * Form_pg_trigger
Definition pg_trigger.h:84
void pgstat_copy_relation_stats(Relation dst, Relation src)
static Datum Int32GetDatum(int32 X)
Definition postgres.h:212
#define RelationGetDescr(relation)
Definition rel.h:542
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition scankey.c:76
void relation_close(Relation relation, LOCKMODE lockmode)
Definition relation.c:206
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition relation.c:48
#define BTEqualStrategyNumber
Definition stratnum.h:31
ItemPointerData t_self
Definition htup.h:65
Definition pg_list.h:54
const char * description

References BTEqualStrategyNumber, CatalogTupleUpdate(), changeDependenciesOf(), changeDependenciesOn(), CopyStatistics(), DeleteInheritsTuple(), description, elog, ERROR, fb(), Form_pg_constraint, Form_pg_index, Form_pg_trigger, get_index_constraint(), get_index_ref_constraints(), get_partition_ancestors(), get_rel_relispartition(), GETSTRUCT(), heap_copytuple(), heap_freetuple(), heap_modify_tuple(), HeapTupleIsValid, Int32GetDatum(), lappend_oid(), lfirst_oid, linitial_oid, list_free(), NameStr, namestrcpy(), NIL, NoLock, ObjectIdGetDatum(), OidIsValid, pgstat_copy_relation_stats(), relation_close(), relation_open(), RelationGetDescr, RowExclusiveLock, ScanKeyInit(), SearchSysCacheCopy1, ShareUpdateExclusiveLock, StoreSingleInheritance(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), table_open(), and values.

Referenced by ReindexRelationConcurrently().

◆ index_constraint_create()

ObjectAddress index_constraint_create ( Relation  heapRelation,
Oid  indexRelationId,
Oid  parentConstraintId,
const IndexInfo *  indexInfo,
const char *  constraintName,
char  constraintType,
uint16  constr_flags,
bool  allow_system_table_mods,
bool  is_internal 
)
extern

Definition at line 1903 of file index.c.

1912{
1913 Oid namespaceId = RelationGetNamespace(heapRelation);
1915 idxaddr;
1916 Oid conOid;
1917 bool deferrable;
1918 bool initdeferred;
1919 bool mark_as_primary;
1920 bool islocal;
1921 bool noinherit;
1923 int16 inhcount;
1924
1925 deferrable = (constr_flags & INDEX_CONSTR_CREATE_DEFERRABLE) != 0;
1926 initdeferred = (constr_flags & INDEX_CONSTR_CREATE_INIT_DEFERRED) != 0;
1929
1930 /* constraint creation support doesn't work while bootstrapping */
1932
1933 /* enforce system-table restriction */
1935 IsSystemRelation(heapRelation) &&
1937 ereport(ERROR,
1939 errmsg("user-defined indexes on system catalog tables are not supported")));
1940
1941 /* primary/unique constraints shouldn't have any expressions */
1942 if (indexInfo->ii_Expressions &&
1944 elog(ERROR, "constraints cannot have index expressions");
1945
1946 /*
1947 * If we're manufacturing a constraint for a pre-existing index, we need
1948 * to get rid of the existing auto dependencies for the index (the ones
1949 * that index_create() would have made instead of calling this function).
1950 *
1951 * Note: this code would not necessarily do the right thing if the index
1952 * has any expressions or predicate, but we'd never be turning such an
1953 * index into a UNIQUE or PRIMARY KEY constraint.
1954 */
1958
1960 {
1961 islocal = false;
1962 inhcount = 1;
1963 noinherit = false;
1964 }
1965 else
1966 {
1967 islocal = true;
1968 inhcount = 0;
1969 noinherit = true;
1970 }
1971
1972 /*
1973 * Construct a pg_constraint entry.
1974 */
1978 deferrable,
1979 initdeferred,
1980 true, /* Is Enforced */
1981 true,
1983 RelationGetRelid(heapRelation),
1984 indexInfo->ii_IndexAttrNumbers,
1985 indexInfo->ii_NumIndexKeyAttrs,
1986 indexInfo->ii_NumIndexAttrs,
1987 InvalidOid, /* no domain */
1988 indexRelationId, /* index OID */
1989 InvalidOid, /* no foreign key */
1990 NULL,
1991 NULL,
1992 NULL,
1993 NULL,
1994 0,
1995 ' ',
1996 ' ',
1997 NULL,
1998 0,
1999 ' ',
2000 indexInfo->ii_ExclusionOps,
2001 NULL, /* no check constraint */
2002 NULL,
2003 islocal,
2004 inhcount,
2005 noinherit,
2007 is_internal);
2008
2009 /*
2010 * Register the index as internally dependent on the constraint.
2011 *
2012 * Note that the constraint has a dependency on the table, so we don't
2013 * need (or want) any direct dependency from the index to the table.
2014 */
2018
2019 /*
2020 * Also, if this is a constraint on a partition, give it partition-type
2021 * dependencies on the parent constraint as well as the table.
2022 */
2024 {
2026
2030 RelationGetRelid(heapRelation));
2032 }
2033
2034 /*
2035 * If the constraint is deferrable, create the deferred uniqueness
2036 * checking trigger. (The trigger will be given an internal dependency on
2037 * the constraint by CreateTrigger.)
2038 */
2039 if (deferrable)
2040 {
2042
2043 trigger->replace = false;
2044 trigger->isconstraint = true;
2045 trigger->trigname = (constraintType == CONSTRAINT_PRIMARY) ?
2046 "PK_ConstraintTrigger" :
2047 "Unique_ConstraintTrigger";
2048 trigger->relation = NULL;
2049 trigger->funcname = SystemFuncName("unique_key_recheck");
2050 trigger->args = NIL;
2051 trigger->row = true;
2052 trigger->timing = TRIGGER_TYPE_AFTER;
2054 trigger->columns = NIL;
2055 trigger->whenClause = NULL;
2056 trigger->transitionRels = NIL;
2057 trigger->deferrable = true;
2058 trigger->initdeferred = initdeferred;
2059 trigger->constrrel = NULL;
2060
2063 InvalidOid, NULL, true, false);
2064 }
2065
2066 /*
2067 * If needed, mark the index as primary and/or deferred in pg_index.
2068 *
2069 * Note: When making an existing index into a constraint, caller must have
2070 * a table lock that prevents concurrent table updates; otherwise, there
2071 * is a risk that concurrent readers of the table will miss seeing this
2072 * index at all.
2073 */
2075 (mark_as_primary || deferrable))
2076 {
2080 bool dirty = false;
2081 bool marked_as_primary = false;
2082
2084
2088 elog(ERROR, "cache lookup failed for index %u", indexRelationId);
2090
2091 if (mark_as_primary && !indexForm->indisprimary)
2092 {
2093 indexForm->indisprimary = true;
2094 dirty = true;
2095 marked_as_primary = true;
2096 }
2097
2098 if (deferrable && indexForm->indimmediate)
2099 {
2100 indexForm->indimmediate = false;
2101 dirty = true;
2102 }
2103
2104 if (dirty)
2105 {
2107
2108 /*
2109 * When we mark an existing index as primary, force a relcache
2110 * flush on its parent table, so that all sessions will become
2111 * aware that the table now has a primary key. This is important
2112 * because it affects some replication behaviors.
2113 */
2115 CacheInvalidateRelcache(heapRelation);
2116
2118 InvalidOid, is_internal);
2119 }
2120
2123 }
2124
2125 return myself;
2126}
int16_t int16
Definition c.h:678
bool IsSystemRelation(Relation relation)
Definition catalog.c:74
@ DEPENDENCY_AUTO
Definition dependency.h:34
@ DEPENDENCY_INTERNAL
Definition dependency.h:35
@ DEPENDENCY_PARTITION_PRI
Definition dependency.h:36
@ DEPENDENCY_PARTITION_SEC
Definition dependency.h:37
#define INDEX_CONSTR_CREATE_WITHOUT_OVERLAPS
Definition index.h:103
#define INDEX_CONSTR_CREATE_UPDATE_INDEX
Definition index.h:101
#define INDEX_CONSTR_CREATE_REMOVE_OLD_DEPS
Definition index.h:102
#define INDEX_CONSTR_CREATE_DEFERRABLE
Definition index.h:99
#define INDEX_CONSTR_CREATE_MARK_AS_PRIMARY
Definition index.h:98
#define INDEX_CONSTR_CREATE_INIT_DEFERRED
Definition index.h:100
#define IsBootstrapProcessingMode()
Definition miscadmin.h:486
#define makeNode(_type_)
Definition nodes.h:159
#define InvokeObjectPostAlterHookArg(classId, objectId, subId, auxiliaryId, is_internal)
#define ObjectAddressSet(addr, class_id, object_id)
List * SystemFuncName(char *name)
Oid CreateConstraintEntry(const char *constraintName, Oid constraintNamespace, char constraintType, bool isDeferrable, bool isDeferred, bool isEnforced, bool isValidated, Oid parentConstrId, Oid relId, const int16 *constraintKey, int constraintNKeys, int constraintNTotalKeys, Oid domainId, Oid indexRelId, Oid foreignRelId, const int16 *foreignKey, const Oid *pfEqOp, const Oid *ppEqOp, const Oid *ffEqOp, int foreignNKeys, char foreignUpdateType, char foreignDeleteType, const int16 *fkDeleteSetCols, int numFkDeleteSetCols, char foreignMatchType, const Oid *exclOp, Node *conExpr, const char *conBin, bool conIsLocal, int16 conInhCount, bool conNoInherit, bool conPeriod, bool is_internal)
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition pg_depend.c:51
long deleteDependencyRecordsForClass(Oid classId, Oid objectId, Oid refclassId, char deptype)
Definition pg_depend.c:364
#define RelationGetNamespace(relation)
Definition rel.h:557
ObjectAddress CreateTrigger(const CreateTrigStmt *stmt, const char *queryString, Oid relOid, Oid refRelOid, Oid constraintOid, Oid indexOid, Oid funcoid, Oid parentTriggerOid, Node *whenClause, bool isInternal, bool in_partition)
Definition trigger.c:162

References Assert, CacheInvalidateRelcache(), CatalogTupleUpdate(), CreateConstraintEntry(), CreateTrigger(), deleteDependencyRecordsForClass(), DEPENDENCY_AUTO, DEPENDENCY_INTERNAL, DEPENDENCY_PARTITION_PRI, DEPENDENCY_PARTITION_SEC, elog, ereport, errcode(), errmsg, ERROR, fb(), Form_pg_index, GETSTRUCT(), heap_freetuple(), HeapTupleIsValid, IndexInfo::ii_ExclusionOps, IndexInfo::ii_Expressions, IndexInfo::ii_IndexAttrNumbers, IndexInfo::ii_NumIndexAttrs, IndexInfo::ii_NumIndexKeyAttrs, INDEX_CONSTR_CREATE_DEFERRABLE, INDEX_CONSTR_CREATE_INIT_DEFERRED, INDEX_CONSTR_CREATE_MARK_AS_PRIMARY, INDEX_CONSTR_CREATE_REMOVE_OLD_DEPS, INDEX_CONSTR_CREATE_UPDATE_INDEX, INDEX_CONSTR_CREATE_WITHOUT_OVERLAPS, InvalidOid, InvokeObjectPostAlterHookArg, IsBootstrapProcessingMode, IsNormalProcessingMode, IsSystemRelation(), makeNode, NIL, ObjectAddressSet, ObjectIdGetDatum(), OidIsValid, recordDependencyOn(), RelationGetNamespace, RelationGetRelid, RowExclusiveLock, SearchSysCacheCopy1, SystemFuncName(), table_close(), and table_open().

Referenced by ATExecAddIndexConstraint(), and index_create().

◆ index_create()

Oid index_create ( Relation  heapRelation,
const char *  indexRelationName,
Oid  indexRelationId,
Oid  parentIndexRelid,
Oid  parentConstraintId,
RelFileNumber  relFileNumber,
IndexInfo *  indexInfo,
const List *  indexColNames,
Oid  accessMethodId,
Oid  tableSpaceId,
const Oid *  collationIds,
const Oid *  opclassIds,
const Datum *  opclassOptions,
const int16 *  coloptions,
const NullableDatum *  stattargets,
Datum  reloptions,
uint16  flags,
uint16  constr_flags,
bool  allow_system_table_mods,
bool  is_internal,
Oid *  constraintId 
)
extern

Definition at line 730 of file index.c.

751{
752 Oid heapRelationId = RelationGetRelid(heapRelation);
754 Relation indexRelation;
756 bool shared_relation;
757 bool mapped_relation;
758 bool is_exclusion;
760 int i;
761 char relpersistence;
762 bool isprimary = (flags & INDEX_CREATE_IS_PRIMARY) != 0;
763 bool invalid = (flags & INDEX_CREATE_INVALID) != 0;
764 bool concurrent = (flags & INDEX_CREATE_CONCURRENT) != 0;
765 bool partitioned = (flags & INDEX_CREATE_PARTITIONED) != 0;
766 bool progress = (flags & INDEX_CREATE_SUPPRESS_PROGRESS) == 0;
767 char relkind;
768 TransactionId relfrozenxid;
769 MultiXactId relminmxid;
771
772 /* constraint flags can only be set when a constraint is requested */
773 Assert((constr_flags == 0) ||
774 ((flags & INDEX_CREATE_ADD_CONSTRAINT) != 0));
775 /* partitioned indexes must never be "built" by themselves */
777
779 is_exclusion = (indexInfo->ii_ExclusionOps != NULL);
780
782
783 /*
784 * The index will be in the same namespace as its parent table, and is
785 * shared across databases if and only if the parent is. Likewise, it
786 * will use the relfilenumber map if and only if the parent does; and it
787 * inherits the parent's relpersistence.
788 */
789 namespaceId = RelationGetNamespace(heapRelation);
790 shared_relation = heapRelation->rd_rel->relisshared;
791 mapped_relation = RelationIsMapped(heapRelation);
792 relpersistence = heapRelation->rd_rel->relpersistence;
793
794 /*
795 * check parameters
796 */
797 if (indexInfo->ii_NumIndexAttrs < 1)
798 elog(ERROR, "must index at least one column");
799
801 IsSystemRelation(heapRelation) &&
805 errmsg("user-defined indexes on system catalog tables are not supported")));
806
807 /*
808 * Btree text_pattern_ops uses texteq as the equality operator, which is
809 * fine as long as the collation is deterministic; texteq then reduces to
810 * bitwise equality and so it is semantically compatible with the other
811 * operators and functions in that opclass. But with a nondeterministic
812 * collation, texteq could yield results that are incompatible with the
813 * actual behavior of the index (which is determined by the opclass's
814 * comparison function). We prevent such problems by refusing creation of
815 * an index with that opclass and a nondeterministic collation.
816 *
817 * The same applies to varchar_pattern_ops and bpchar_pattern_ops. If we
818 * find more cases, we might decide to create a real mechanism for marking
819 * opclasses as incompatible with nondeterminism; but for now, this small
820 * hack suffices.
821 *
822 * Another solution is to use a special operator, not texteq, as the
823 * equality opclass member; but that is undesirable because it would
824 * prevent index usage in many queries that work fine today.
825 */
826 for (i = 0; i < indexInfo->ii_NumIndexKeyAttrs; i++)
827 {
828 Oid collation = collationIds[i];
829 Oid opclass = opclassIds[i];
830
831 if (collation)
832 {
833 if ((opclass == TEXT_BTREE_PATTERN_OPS_OID ||
835 opclass == BPCHAR_BTREE_PATTERN_OPS_OID) &&
837 {
839
842 elog(ERROR, "cache lookup failed for operator class %u", opclass);
845 errmsg("nondeterministic collations are not supported for operator class \"%s\"",
848 }
849 }
850 }
851
852 /*
853 * Concurrent index build on a system catalog is unsafe because we tend to
854 * release locks before committing in catalogs.
855 */
856 if (concurrent &&
857 IsCatalogRelation(heapRelation))
860 errmsg("concurrent index creation on system catalog tables is not supported")));
861
862 /*
863 * This case is currently not supported. There's no way to ask for it in
864 * the grammar with CREATE INDEX, but it can happen with REINDEX.
865 */
866 if (concurrent && is_exclusion)
869 errmsg("concurrent index creation for exclusion constraints is not supported")));
870
871 /*
872 * We cannot allow indexing a shared relation after initdb (because
873 * there's no way to make the entry in other databases' pg_class).
874 */
878 errmsg("shared indexes cannot be created after initdb")));
879
880 /*
881 * Shared relations must be in pg_global, too (last-ditch check)
882 */
884 elog(ERROR, "shared relations must be placed in pg_global tablespace");
885
886 /*
887 * Check for duplicate name (both as to the index, and as to the
888 * associated constraint if any). Such cases would fail on the relevant
889 * catalogs' unique indexes anyway, but we prefer to give a friendlier
890 * error message.
891 */
893 {
894 if ((flags & INDEX_CREATE_IF_NOT_EXISTS) != 0)
895 {
898 errmsg("relation \"%s\" already exists, skipping",
901 return InvalidOid;
902 }
903
906 errmsg("relation \"%s\" already exists",
908 }
909
910 if ((flags & INDEX_CREATE_ADD_CONSTRAINT) != 0 &&
913 {
914 /*
915 * INDEX_CREATE_IF_NOT_EXISTS does not apply here, since the
916 * conflicting constraint is not an index.
917 */
920 errmsg("constraint \"%s\" for relation \"%s\" already exists",
922 }
923
924 /*
925 * construct tuple descriptor for index tuples
926 */
928 indexInfo,
932 opclassIds);
933
934 /*
935 * Allocate an OID for the index, unless we were told what to use.
936 *
937 * The OID will be the relfilenumber as well, so make sure it doesn't
938 * collide with either pg_class OIDs or existing physical files.
939 */
941 {
942 /* Use binary-upgrade override for pg_class.oid and relfilenumber */
943 if (IsBinaryUpgrade)
944 {
948 errmsg("pg_class index OID value not set when in binary upgrade mode")));
949
952
953 /* Override the index relfilenumber */
954 if ((relkind == RELKIND_INDEX) &&
958 errmsg("index relfilenumber value not set when in binary upgrade mode")));
961
962 /*
963 * Note that we want create_storage = true for binary upgrade. The
964 * storage we create here will be replaced later, but we need to
965 * have something on disk in the meanwhile.
966 */
968 }
969 else
970 {
972 GetNewRelFileNumber(tableSpaceId, pg_class, relpersistence);
973 }
974 }
975
976 /*
977 * create the index relation's relcache entry and, if necessary, the
978 * physical disk file. (If we fail further down, it's the smgr's
979 * responsibility to remove the disk file again, if any.)
980 */
981 indexRelation = heap_create(indexRelationName,
988 relkind,
989 relpersistence,
993 &relfrozenxid,
994 &relminmxid,
996
997 Assert(relfrozenxid == InvalidTransactionId);
998 Assert(relminmxid == InvalidMultiXactId);
999 Assert(indexRelationId == RelationGetRelid(indexRelation));
1000
1001 /*
1002 * Obtain exclusive lock on it. Although no other transactions can see it
1003 * until we commit, this prevents deadlock-risk complaints from lock
1004 * manager in cases such as CLUSTER.
1005 */
1006 LockRelation(indexRelation, AccessExclusiveLock);
1007
1008 /*
1009 * Fill in fields of the index's pg_class entry that are not set correctly
1010 * by heap_create.
1011 *
1012 * XXX should have a cleaner way to create cataloged indexes
1013 */
1014 indexRelation->rd_rel->relowner = heapRelation->rd_rel->relowner;
1015 indexRelation->rd_rel->relam = accessMethodId;
1016 indexRelation->rd_rel->relispartition = OidIsValid(parentIndexRelid);
1017
1018 /*
1019 * store index's pg_class entry
1020 */
1021 InsertPgClassTuple(pg_class, indexRelation,
1022 RelationGetRelid(indexRelation),
1023 (Datum) 0,
1024 reloptions);
1025
1026 /* done with pg_class */
1028
1029 /*
1030 * now update the object id's of all the attribute tuple forms in the
1031 * index relation's tuple descriptor
1032 */
1033 InitializeAttributeOids(indexRelation,
1034 indexInfo->ii_NumIndexAttrs,
1036
1037 /*
1038 * append ATTRIBUTE tuples for the index
1039 */
1041
1042 /* ----------------
1043 * update pg_index
1044 * (append INDEX tuple)
1045 *
1046 * Note that this stows away a representation of "predicate".
1047 * (Or, could define a rule to maintain the predicate) --Nels, Feb '92
1048 * ----------------
1049 */
1051 indexInfo,
1055 !concurrent && !invalid,
1056 !concurrent);
1057
1058 /*
1059 * Register relcache invalidation on the indexes' heap relation, to
1060 * maintain consistency of its index list
1061 */
1062 CacheInvalidateRelcache(heapRelation);
1063
1064 /* update pg_inherits and the parent's relhassubclass, if needed */
1066 {
1070 }
1071
1072 /*
1073 * Register constraint and dependencies for the index.
1074 *
1075 * If the index is from a CONSTRAINT clause, construct a pg_constraint
1076 * entry. The index will be linked to the constraint, which in turn is
1077 * linked to the table. If it's not a CONSTRAINT, we need to make a
1078 * dependency directly on the table.
1079 *
1080 * We don't need a dependency on the namespace, because there'll be an
1081 * indirect dependency via our parent table.
1082 *
1083 * During bootstrap we can't register any dependencies, and we don't try
1084 * to make a constraint either.
1085 */
1087 {
1089 referenced;
1090 ObjectAddresses *addrs;
1091
1093
1094 if ((flags & INDEX_CREATE_ADD_CONSTRAINT) != 0)
1095 {
1096 char constraintType;
1098
1099 if (isprimary)
1101 else if (indexInfo->ii_Unique)
1103 else if (is_exclusion)
1105 else
1106 {
1107 elog(ERROR, "constraint must be PRIMARY, UNIQUE or EXCLUDE");
1108 constraintType = 0; /* keep compiler quiet */
1109 }
1110
1111 localaddr = index_constraint_create(heapRelation,
1114 indexInfo,
1119 is_internal);
1120 if (constraintId)
1121 *constraintId = localaddr.objectId;
1122 }
1123 else
1124 {
1125 bool have_simple_col = false;
1126
1127 addrs = new_object_addresses();
1128
1129 /* Create auto dependencies on simply-referenced columns */
1130 for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++)
1131 {
1132 if (indexInfo->ii_IndexAttrNumbers[i] != 0)
1133 {
1136 indexInfo->ii_IndexAttrNumbers[i]);
1138 have_simple_col = true;
1139 }
1140 }
1141
1142 /*
1143 * If there are no simply-referenced columns, give the index an
1144 * auto dependency on the whole table. In most cases, this will
1145 * be redundant, but it might not be if the index expressions and
1146 * predicate contain no Vars or only whole-row Vars.
1147 */
1148 if (!have_simple_col)
1149 {
1153 }
1154
1156 free_object_addresses(addrs);
1157 }
1158
1159 /*
1160 * If this is an index partition, create partition dependencies on
1161 * both the parent index and the table. (Note: these must be *in
1162 * addition to*, not instead of, all other dependencies. Otherwise
1163 * we'll be short some dependencies after DETACH PARTITION.)
1164 */
1166 {
1169
1172 }
1173
1174 /* placeholder for normal dependencies */
1175 addrs = new_object_addresses();
1176
1177 /* Store dependency on collations */
1178
1179 /* The default collation is pinned, so don't bother recording it */
1180 for (i = 0; i < indexInfo->ii_NumIndexKeyAttrs; i++)
1181 {
1183 {
1186 }
1187 }
1188
1189 /* Store dependency on operator classes */
1190 for (i = 0; i < indexInfo->ii_NumIndexKeyAttrs; i++)
1191 {
1194 }
1195
1197 free_object_addresses(addrs);
1198
1199 /* Store dependencies on anything mentioned in index expressions */
1200 if (indexInfo->ii_Expressions)
1201 {
1203 (Node *) indexInfo->ii_Expressions,
1206 DEPENDENCY_AUTO, false);
1207 }
1208
1209 /* Store dependencies on anything mentioned in predicate */
1210 if (indexInfo->ii_Predicate)
1211 {
1213 (Node *) indexInfo->ii_Predicate,
1216 DEPENDENCY_AUTO, false);
1217 }
1218 }
1219 else
1220 {
1221 /* Bootstrap mode - assert we weren't asked for constraint support */
1222 Assert((flags & INDEX_CREATE_ADD_CONSTRAINT) == 0);
1223 }
1224
1225 /* Post creation hook for new index */
1227 indexRelationId, 0, is_internal);
1228
1229 /*
1230 * Advance the command counter so that we can see the newly-entered
1231 * catalog tuples for the index.
1232 */
1234
1235 /*
1236 * In bootstrap mode, we have to fill in the index strategy structure with
1237 * information from the catalogs. If we aren't bootstrapping, then the
1238 * relcache entry has already been rebuilt thanks to sinval update during
1239 * CommandCounterIncrement.
1240 */
1242 RelationInitIndexAccessInfo(indexRelation);
1243 else
1244 Assert(indexRelation->rd_indexcxt != NULL);
1245
1246 indexRelation->rd_index->indnkeyatts = indexInfo->ii_NumIndexKeyAttrs;
1247
1248 /* Validate opclass-specific options */
1249 if (opclassOptions)
1250 for (i = 0; i < indexInfo->ii_NumIndexKeyAttrs; i++)
1251 (void) index_opclass_options(indexRelation, i + 1,
1253 true);
1254
1255 /*
1256 * If this is bootstrap (initdb) time, then we don't actually fill in the
1257 * index yet. We'll be creating more indexes and classes later, so we
1258 * delay filling them in until just before we're done with bootstrapping.
1259 * Similarly, if the caller specified to skip the build then filling the
1260 * index is delayed till later (ALTER TABLE can save work in some cases
1261 * with this). Otherwise, we call the AM routine that constructs the
1262 * index.
1263 */
1265 {
1267 }
1268 else if ((flags & INDEX_CREATE_SKIP_BUILD) != 0)
1269 {
1270 /*
1271 * Caller is responsible for filling the index later on. However,
1272 * we'd better make sure that the heap relation is correctly marked as
1273 * having an index.
1274 */
1275 index_update_stats(heapRelation,
1276 true,
1277 -1.0);
1278 /* Make the above update visible */
1280 }
1281 else
1282 {
1283 index_build(heapRelation, indexRelation, indexInfo, false, true,
1284 progress);
1285 }
1286
1287 /*
1288 * Close the index; but we keep the lock that we acquired above until end
1289 * of transaction. Closing the heap is caller's responsibility.
1290 */
1291 index_close(indexRelation, NoLock);
1292
1293 return indexRelationId;
1294}
void index_register(Oid heap, Oid ind, const IndexInfo *indexInfo)
Definition bootstrap.c:1130
TransactionId MultiXactId
Definition c.h:805
uint32 TransactionId
Definition c.h:795
RelFileNumber GetNewRelFileNumber(Oid reltablespace, Relation pg_class, char relpersistence)
Definition catalog.c:584
bool IsCatalogRelation(Relation relation)
Definition catalog.c:106
void record_object_address_dependencies(const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
void recordDependencyOnSingleRelExpr(const ObjectAddress *depender, Node *expr, Oid relId, DependencyType behavior, DependencyType self_behavior, bool reverse_self)
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
ObjectAddresses * new_object_addresses(void)
void free_object_addresses(ObjectAddresses *addrs)
@ DEPENDENCY_NORMAL
Definition dependency.h:33
#define NOTICE
Definition elog.h:36
bool IsBinaryUpgrade
Definition globals.c:123
void InsertPgClassTuple(Relation pg_class_desc, Relation new_rel_desc, Oid new_rel_oid, Datum relacl, Datum reloptions)
Definition heap.c:928
Relation heap_create(const char *relname, Oid relnamespace, Oid reltablespace, Oid relid, RelFileNumber relfilenumber, Oid accessmtd, TupleDesc tupDesc, char relkind, char relpersistence, bool shared_relation, bool mapped_relation, bool allow_system_table_mods, TransactionId *relfrozenxid, MultiXactId *relminmxid, bool create_storage)
Definition heap.c:286
RelFileNumber binary_upgrade_next_index_pg_class_relfilenumber
Definition index.c:87
ObjectAddress index_constraint_create(Relation heapRelation, Oid indexRelationId, Oid parentConstraintId, const IndexInfo *indexInfo, const char *constraintName, char constraintType, uint16 constr_flags, bool allow_system_table_mods, bool is_internal)
Definition index.c:1903
static void InitializeAttributeOids(Relation indexRelation, int numatts, Oid indexoid)
Definition index.c:495
Oid binary_upgrade_next_index_pg_class_oid
Definition index.c:86
static void AppendAttributeTuples(Relation indexRelation, const Datum *attopts, const NullableDatum *stattargets)
Definition index.c:513
static TupleDesc ConstructTupleDescriptor(Relation heapRelation, const IndexInfo *indexInfo, const List *indexColNames, Oid accessMethodId, const Oid *collationIds, const Oid *opclassIds)
Definition index.c:282
static void UpdateIndexRelation(Oid indexoid, Oid heapoid, Oid parentIndexId, const IndexInfo *indexInfo, const Oid *collationOids, const Oid *opclassOids, const int16 *coloptions, bool primary, bool isexclusion, bool immediate, bool isvalid, bool isready)
Definition index.c:564
#define INDEX_CREATE_SUPPRESS_PROGRESS
Definition index.h:74
#define INDEX_CREATE_IS_PRIMARY
Definition index.h:67
#define INDEX_CREATE_IF_NOT_EXISTS
Definition index.h:71
#define INDEX_CREATE_PARTITIONED
Definition index.h:72
#define INDEX_CREATE_INVALID
Definition index.h:73
#define INDEX_CREATE_ADD_CONSTRAINT
Definition index.h:68
#define INDEX_CREATE_SKIP_BUILD
Definition index.h:69
#define INDEX_CREATE_CONCURRENT
Definition index.h:70
bytea * index_opclass_options(Relation indrel, AttrNumber attnum, Datum attoptions, bool validate)
Definition indexam.c:1016
invalidindex index d is invalid
Definition isn.c:138
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition lmgr.c:107
void LockRelation(Relation relation, LOCKMODE lockmode)
Definition lmgr.c:246
#define AccessExclusiveLock
Definition lockdefs.h:43
bool get_collation_isdeterministic(Oid colloid)
Definition lsyscache.c:1280
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition lsyscache.c:2199
#define InvalidMultiXactId
Definition multixact.h:25
#define InvokeObjectPostCreateHookArg(classId, objectId, subId, is_internal)
#define ObjectAddressSubSet(addr, class_id, object_id, object_sub_id)
bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId, const char *conname)
@ CONSTRAINT_RELATION
END_CATALOG_STRUCT typedef FormData_pg_opclass * Form_pg_opclass
Definition pg_opclass.h:87
#define RelationIsMapped(relation)
Definition rel.h:565
void RelationInitIndexAccessInfo(Relation relation)
Definition relcache.c:1440
#define InvalidRelFileNumber
Definition relpath.h:26
#define RelFileNumberIsValid(relnumber)
Definition relpath.h:27
#define ERRCODE_DUPLICATE_OBJECT
Definition streamutil.c:30
List * ii_Predicate
Definition execnodes.h:197
Form_pg_index rd_index
Definition rel.h:192
MemoryContext rd_indexcxt
Definition rel.h:204
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:221
void SetRelationHasSubclass(Oid relationId, bool relhassubclass)
Definition tablecmds.c:3726
#define InvalidTransactionId
Definition transam.h:31

References AccessExclusiveLock, add_exact_object_address(), AppendAttributeTuples(), Assert, binary_upgrade_next_index_pg_class_oid, binary_upgrade_next_index_pg_class_relfilenumber, CacheInvalidateRelcache(), CommandCounterIncrement(), CONSTRAINT_RELATION, ConstraintNameIsUsed(), ConstructTupleDescriptor(), DEPENDENCY_AUTO, DEPENDENCY_NORMAL, DEPENDENCY_PARTITION_PRI, DEPENDENCY_PARTITION_SEC, elog, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg, ERROR, fb(), Form_pg_opclass, free_object_addresses(), get_collation_isdeterministic(), get_relname_relid(), GetNewRelFileNumber(), GETSTRUCT(), heap_create(), HeapTupleIsValid, i, IndexInfo::ii_ExclusionOps, IndexInfo::ii_Expressions, IndexInfo::ii_IndexAttrNumbers, IndexInfo::ii_NumIndexAttrs, IndexInfo::ii_NumIndexKeyAttrs, IndexInfo::ii_Predicate, IndexInfo::ii_Unique, index_build(), index_close(), INDEX_CONSTR_CREATE_DEFERRABLE, index_constraint_create(), INDEX_CREATE_ADD_CONSTRAINT, INDEX_CREATE_CONCURRENT, INDEX_CREATE_IF_NOT_EXISTS, INDEX_CREATE_INVALID, INDEX_CREATE_IS_PRIMARY, INDEX_CREATE_PARTITIONED, INDEX_CREATE_SKIP_BUILD, INDEX_CREATE_SUPPRESS_PROGRESS, index_opclass_options(), index_register(), index_update_stats(), InitializeAttributeOids(), InsertPgClassTuple(), invalid, InvalidMultiXactId, InvalidOid, InvalidRelFileNumber, InvalidTransactionId, InvokeObjectPostCreateHookArg, IsBinaryUpgrade, IsBootstrapProcessingMode, IsCatalogRelation(), IsNormalProcessingMode, IsSystemRelation(), LockRelation(), LockRelationOid(), NameStr, new_object_addresses(), NoLock, NOTICE, ObjectAddressSet, ObjectAddressSubSet, ObjectIdGetDatum(), OidIsValid, progress, RelationData::rd_index, RelationData::rd_indexcxt, RelationData::rd_rel, record_object_address_dependencies(), recordDependencyOn(), recordDependencyOnSingleRelExpr(), RelationGetNamespace, RelationGetRelationName, RelationGetRelid, RelationInitIndexAccessInfo(), RelationIsMapped, ReleaseSysCache(), RelFileNumberIsValid, RowExclusiveLock, SearchSysCache1(), SetRelationHasSubclass(), ShareUpdateExclusiveLock, StoreSingleInheritance(), table_close(), table_open(), and UpdateIndexRelation().

Referenced by create_toast_table(), DefineIndex(), and index_create_copy().

◆ index_create_copy()

Oid index_create_copy ( Relation  heapRelation,
uint16  flags,
Oid  oldIndexId,
Oid  tablespaceOid,
const char *  newName 
)
extern

Definition at line 1306 of file index.c.

1308{
1309 Relation indexRelation;
1311 *newInfo;
1313 bool concurrently = (flags & INDEX_CREATE_CONCURRENT) != 0;
1315 classTuple;
1323 bool isnull;
1325 List *indexExprs = NIL;
1326 List *indexPreds = NIL;
1327
1328 indexRelation = index_open(oldIndexId, RowExclusiveLock);
1329
1330 /* The new index needs some information from the old index */
1331 oldInfo = BuildIndexInfo(indexRelation);
1332
1333 /*
1334 * Concurrent build of an index with exclusion constraints is not
1335 * supported.
1336 */
1337 if (oldInfo->ii_ExclusionOps != NULL && concurrently)
1338 ereport(ERROR,
1340 errmsg("concurrent index creation for exclusion constraints is not supported")));
1341
1342 /* Get the array of class and column options IDs from index info */
1345 elog(ERROR, "cache lookup failed for index %u", oldIndexId);
1349
1353
1354 /* Fetch reloptions of index if any */
1357 elog(ERROR, "cache lookup failed for relation %u", oldIndexId);
1359 Anum_pg_class_reloptions, &isnull);
1360
1361 /*
1362 * Fetch the list of expressions and predicates directly from the
1363 * catalogs. This cannot rely on the information from IndexInfo of the
1364 * old index as these have been flattened for the planner.
1365 */
1366 if (oldInfo->ii_Expressions != NIL)
1367 {
1369 char *exprString;
1370
1376 }
1377 if (oldInfo->ii_Predicate != NIL)
1378 {
1380 char *predString;
1381
1386
1387 /* Also convert to implicit-AND format */
1390 }
1391
1392 /*
1393 * Build the index information for the new index.
1394 */
1395 newInfo = makeIndexInfo(oldInfo->ii_NumIndexAttrs,
1396 oldInfo->ii_NumIndexKeyAttrs,
1397 oldInfo->ii_Am,
1398 indexExprs,
1399 indexPreds,
1400 oldInfo->ii_Unique,
1401 oldInfo->ii_NullsNotDistinct,
1402 !concurrently, /* isready */
1403 concurrently, /* concurrent */
1404 indexRelation->rd_indam->amsummarizing,
1405 oldInfo->ii_WithoutOverlaps);
1406
1407 /* fetch exclusion constraint info if any */
1408 if (indexRelation->rd_index->indisexclusion)
1409 {
1410 /*
1411 * XXX Beware: we're making newInfo point to oldInfo-owned memory. It
1412 * would be more orthodox to palloc+memcpy, but we don't need that
1413 * here at present.
1414 */
1415 newInfo->ii_ExclusionOps = oldInfo->ii_ExclusionOps;
1416 newInfo->ii_ExclusionProcs = oldInfo->ii_ExclusionProcs;
1417 newInfo->ii_ExclusionStrats = oldInfo->ii_ExclusionStrats;
1418 }
1419
1420 /*
1421 * Extract the list of column names and the column numbers for the new
1422 * index information. All this information will be used for the index
1423 * creation.
1424 */
1425 for (int i = 0; i < oldInfo->ii_NumIndexAttrs; i++)
1426 {
1427 TupleDesc indexTupDesc = RelationGetDescr(indexRelation);
1429
1430 indexColNames = lappend(indexColNames, NameStr(att->attname));
1431 newInfo->ii_IndexAttrNumbers[i] = oldInfo->ii_IndexAttrNumbers[i];
1432 }
1433
1434 /* Extract opclass options for each attribute */
1435 opclassOptions = palloc0_array(Datum, newInfo->ii_NumIndexAttrs);
1436 for (int i = 0; i < newInfo->ii_NumIndexAttrs; i++)
1438
1439 /* Extract statistic targets for each attribute */
1440 stattargets = palloc0_array(NullableDatum, newInfo->ii_NumIndexAttrs);
1441 for (int i = 0; i < newInfo->ii_NumIndexAttrs; i++)
1442 {
1443 HeapTuple tp;
1444 Datum dat;
1445
1447 if (!HeapTupleIsValid(tp))
1448 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
1449 i + 1, oldIndexId);
1451 ReleaseSysCache(tp);
1452 stattargets[i].value = dat;
1453 stattargets[i].isnull = isnull;
1454 }
1455
1456 /*
1457 * Now create the new index.
1458 *
1459 * For a partition index, we adjust the partition dependency later, to
1460 * ensure a consistent state at all times. That is why parentIndexRelid
1461 * is not set here.
1462 */
1463 newIndexId = index_create(heapRelation,
1464 newName,
1465 InvalidOid, /* indexRelationId */
1466 InvalidOid, /* parentIndexRelid */
1467 InvalidOid, /* parentConstraintId */
1468 InvalidRelFileNumber, /* relFileNumber */
1469 newInfo,
1471 indexRelation->rd_rel->relam,
1472 tablespaceOid,
1473 indexRelation->rd_indcollation,
1474 indclass->values,
1476 indcoloptions->values,
1479 flags,
1480 0,
1481 true, /* allow table to be a system catalog? */
1482 false, /* is_internal? */
1483 NULL);
1484
1485 /* Close the relations used and clean up */
1486 index_close(indexRelation, NoLock);
1489
1490 return newIndexId;
1491}
#define TextDatumGetCString(d)
Definition builtins.h:99
#define palloc0_array(type, count)
Definition fe_memutils.h:92
Oid index_create(Relation heapRelation, const char *indexRelationName, Oid indexRelationId, Oid parentIndexRelid, Oid parentConstraintId, RelFileNumber relFileNumber, IndexInfo *indexInfo, const List *indexColNames, Oid accessMethodId, Oid tableSpaceId, const Oid *collationIds, const Oid *opclassIds, const Datum *opclassOptions, const int16 *coloptions, const NullableDatum *stattargets, Datum reloptions, uint16 flags, uint16 constr_flags, bool allow_system_table_mods, bool is_internal, Oid *constraintId)
Definition index.c:730
List * lappend(List *list, void *datum)
Definition list.c:339
Datum get_attoptions(Oid relid, int16 attnum)
Definition lsyscache.c:1196
List * make_ands_implicit(Expr *clause)
Definition makefuncs.c:810
void pfree(void *pointer)
Definition mcxt.c:1619
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332
void * stringToNode(const char *str)
Definition read.c:90
bool amsummarizing
Definition amapi.h:283
Oid * rd_indcollation
Definition rel.h:217
Definition c.h:874
Datum SysCacheGetAttrNotNull(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition syscache.c:626
Datum SysCacheGetAttr(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition syscache.c:596
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178

References IndexAmRoutine::amsummarizing, BuildIndexInfo(), DatumGetPointer(), elog, ereport, errcode(), errmsg, ERROR, fb(), get_attoptions(), HeapTupleIsValid, i, index_close(), index_create(), INDEX_CREATE_CONCURRENT, index_open(), Int16GetDatum(), InvalidOid, InvalidRelFileNumber, lappend(), make_ands_implicit(), makeIndexInfo(), NameStr, NIL, NoLock, ObjectIdGetDatum(), palloc0_array, pfree(), RelationData::rd_indam, RelationData::rd_indcollation, RelationData::rd_index, RelationData::rd_rel, RelationGetDescr, ReleaseSysCache(), RowExclusiveLock, SearchSysCache1(), SearchSysCache2(), stringToNode(), SysCacheGetAttr(), SysCacheGetAttrNotNull(), TextDatumGetCString, and TupleDescAttr().

Referenced by build_new_indexes(), and ReindexRelationConcurrently().

◆ index_drop()

void index_drop ( Oid  indexId,
bool  concurrent,
bool  concurrent_lock_mode 
)
extern

Definition at line 2140 of file index.c.

2141{
2142 Oid heapId;
2145 Relation indexRelation;
2146 HeapTuple tuple;
2147 bool hasexprs;
2148 LockRelId heaprelid,
2149 indexrelid;
2151 LOCKMODE lockmode;
2152
2153 /*
2154 * A temporary relation uses a non-concurrent DROP. Other backends can't
2155 * access a temporary relation, so there's no harm in grabbing a stronger
2156 * lock (see comments in RemoveRelations), and a non-concurrent DROP is
2157 * more efficient.
2158 */
2160 (!concurrent && !concurrent_lock_mode));
2161
2162 /*
2163 * To drop an index safely, we must grab exclusive lock on its parent
2164 * table. Exclusive lock on the index alone is insufficient because
2165 * another backend might be about to execute a query on the parent table.
2166 * If it relies on a previously cached list of index OIDs, then it could
2167 * attempt to access the just-dropped index. We must therefore take a
2168 * table lock strong enough to prevent all queries on the table from
2169 * proceeding until we commit and send out a shared-cache-inval notice
2170 * that will make them update their index lists.
2171 *
2172 * In the concurrent case we avoid this requirement by disabling index use
2173 * in multiple steps and waiting out any transactions that might be using
2174 * the index, so we don't need exclusive lock on the parent table. Instead
2175 * we take ShareUpdateExclusiveLock, to ensure that two sessions aren't
2176 * doing CREATE/DROP INDEX CONCURRENTLY on the same index. (We will get
2177 * AccessExclusiveLock on the index below, once we're sure nobody else is
2178 * using it.)
2179 */
2182 userHeapRelation = table_open(heapId, lockmode);
2184
2185 /*
2186 * We might still have open queries using it in our own session, which the
2187 * above locking won't prevent, so test explicitly.
2188 */
2190
2191 /*
2192 * Drop Index Concurrently is more or less the reverse process of Create
2193 * Index Concurrently.
2194 *
2195 * First we unset indisvalid so queries starting afterwards don't use the
2196 * index to answer queries anymore. We have to keep indisready = true so
2197 * transactions that are still scanning the index can continue to see
2198 * valid index contents. For instance, if they are using READ COMMITTED
2199 * mode, and another transaction makes changes and commits, they need to
2200 * see those new tuples in the index.
2201 *
2202 * After all transactions that could possibly have used the index for
2203 * queries end, we can unset indisready and indislive, then wait till
2204 * nobody could be touching it anymore. (Note: we need indislive because
2205 * this state must be distinct from the initial state during CREATE INDEX
2206 * CONCURRENTLY, which has indislive true while indisready and indisvalid
2207 * are false. That's because in that state, transactions must examine the
2208 * index for HOT-safety decisions, while in this state we don't want them
2209 * to open it at all.)
2210 *
2211 * Since all predicate locks on the index are about to be made invalid, we
2212 * must promote them to predicate locks on the heap. In the
2213 * non-concurrent case we can just do that now. In the concurrent case
2214 * it's a bit trickier. The predicate locks must be moved when there are
2215 * no index scans in progress on the index and no more can subsequently
2216 * start, so that no new predicate locks can be made on the index. Also,
2217 * they must be moved before heap inserts stop maintaining the index, else
2218 * the conflict with the predicate lock on the index gap could be missed
2219 * before the lock on the heap relation is in place to detect a conflict
2220 * based on the heap tuple insert.
2221 */
2222 if (concurrent)
2223 {
2224 /*
2225 * We must commit our transaction in order to make the first pg_index
2226 * state update visible to other sessions. If the DROP machinery has
2227 * already performed any other actions (removal of other objects,
2228 * pg_depend entries, etc), the commit would make those actions
2229 * permanent, which would leave us with inconsistent catalog state if
2230 * we fail partway through the following sequence. Since DROP INDEX
2231 * CONCURRENTLY is restricted to dropping just one index that has no
2232 * dependencies, we should get here before anything's been done ---
2233 * but let's check that to be sure. We can verify that the current
2234 * transaction has not executed any transactional updates by checking
2235 * that no XID has been assigned.
2236 */
2238 ereport(ERROR,
2240 errmsg("DROP INDEX CONCURRENTLY must be first action in transaction")));
2241
2242 /*
2243 * Mark index invalid by updating its pg_index entry
2244 */
2246
2247 /*
2248 * Invalidate the relcache for the table, so that after this commit
2249 * all sessions will refresh any cached plans that might reference the
2250 * index.
2251 */
2253
2254 /* save lockrelid and locktag for below, then close but keep locks */
2255 heaprelid = userHeapRelation->rd_lockInfo.lockRelId;
2256 SET_LOCKTAG_RELATION(heaplocktag, heaprelid.dbId, heaprelid.relId);
2257 indexrelid = userIndexRelation->rd_lockInfo.lockRelId;
2258
2261
2262 /*
2263 * We must commit our current transaction so that the indisvalid
2264 * update becomes visible to other transactions; then start another.
2265 * Note that any previously-built data structures are lost in the
2266 * commit. The only data we keep past here are the relation IDs.
2267 *
2268 * Before committing, get a session-level lock on the table, to ensure
2269 * that neither it nor the index can be dropped before we finish. This
2270 * cannot block, even if someone else is waiting for access, because
2271 * we already have the same lock within our transaction.
2272 */
2275
2279
2280 /*
2281 * Now we must wait until no running transaction could be using the
2282 * index for a query. Use AccessExclusiveLock here to check for
2283 * running transactions that hold locks of any kind on the table. Note
2284 * we do not need to worry about xacts that open the table for reading
2285 * after this point; they will see the index as invalid when they open
2286 * the relation.
2287 *
2288 * Note: the reason we use actual lock acquisition here, rather than
2289 * just checking the ProcArray and sleeping, is that deadlock is
2290 * possible if one of the transactions in question is blocked trying
2291 * to acquire an exclusive lock on our table. The lock code will
2292 * detect deadlock and error out properly.
2293 *
2294 * Note: we report progress through WaitForLockers() unconditionally
2295 * here, even though it will only be used when we're called by REINDEX
2296 * CONCURRENTLY and not when called by DROP INDEX CONCURRENTLY.
2297 */
2299
2300 /*
2301 * Updating pg_index might involve TOAST table access, so ensure we
2302 * have a valid snapshot.
2303 */
2305
2306 /* Finish invalidation of index and mark it as dead */
2308
2310
2311 /*
2312 * Again, commit the transaction to make the pg_index update visible
2313 * to other sessions.
2314 */
2317
2318 /*
2319 * Wait till every transaction that saw the old index state has
2320 * finished. See above about progress reporting.
2321 */
2323
2324 /*
2325 * Re-open relations to allow us to complete our actions.
2326 *
2327 * At this point, nothing should be accessing the index, but lets
2328 * leave nothing to chance and grab AccessExclusiveLock on the index
2329 * before the physical deletion.
2330 */
2333 }
2334 else
2335 {
2336 /* Not concurrent, so just transfer predicate locks and we're good */
2338 }
2339
2340 /*
2341 * Schedule physical removal of the files (if any)
2342 */
2343 if (RELKIND_HAS_STORAGE(userIndexRelation->rd_rel->relkind))
2345
2346 /* ensure that stats are dropped if transaction commits */
2348
2349 /*
2350 * Close and flush the index's relcache entry, to ensure relcache doesn't
2351 * try to rebuild it while we're deleting catalog entries. We keep the
2352 * lock though.
2353 */
2355
2357
2358 /*
2359 * Updating pg_index might involve TOAST table access, so ensure we have a
2360 * valid snapshot.
2361 */
2363
2364 /*
2365 * fix INDEX relation, and check for expressional index
2366 */
2368
2370 if (!HeapTupleIsValid(tuple))
2371 elog(ERROR, "cache lookup failed for index %u", indexId);
2372
2374 RelationGetDescr(indexRelation));
2375
2376 CatalogTupleDelete(indexRelation, &tuple->t_self);
2377
2378 ReleaseSysCache(tuple);
2379 table_close(indexRelation, RowExclusiveLock);
2380
2382
2383 /*
2384 * if it has any expression columns, we might have stored statistics about
2385 * them.
2386 */
2387 if (hasexprs)
2389
2390 /*
2391 * fix ATTRIBUTE relation
2392 */
2394
2395 /*
2396 * fix RELATION relation
2397 */
2399
2400 /*
2401 * fix INHERITS relation
2402 */
2404
2405 /*
2406 * We are presently too lazy to attempt to compute the new correct value
2407 * of relhasindex (the next VACUUM will fix it if necessary). So there is
2408 * no need to update the pg_class tuple for the owning relation. But we
2409 * must send out a shared-cache-inval notice on the owning relation to
2410 * ensure other backends update their relcache lists of indexes. (In the
2411 * concurrent case, this is redundant but harmless.)
2412 */
2414
2415 /*
2416 * Close owning rel, but keep lock
2417 */
2419
2420 /*
2421 * Release the session locks before we go.
2422 */
2423 if (concurrent)
2424 {
2427 }
2428}
void DeleteRelationTuple(Oid relid)
Definition heap.c:1596
void DeleteAttributeTuples(Oid relid)
Definition heap.c:1625
void RemoveStatistics(Oid relid, AttrNumber attnum)
Definition heap.c:3515
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition heaptuple.c:456
Oid IndexGetRelation(Oid indexId, bool missing_ok)
Definition index.c:3616
void index_concurrently_set_dead(Oid heapId, Oid indexId)
Definition index.c:1841
void CatalogTupleDelete(Relation heapRel, const ItemPointerData *tid)
Definition indexing.c:365
void LockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
Definition lmgr.c:391
void WaitForLockers(LOCKTAG heaplocktag, LOCKMODE lockmode, bool progress)
Definition lmgr.c:989
void UnlockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
Definition lmgr.c:404
int LOCKMODE
Definition lockdefs.h:26
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition locktag.h:81
char get_rel_persistence(Oid relid)
Definition lsyscache.c:2392
void pgstat_drop_relation(Relation rel)
void RelationForgetRelation(Oid rid)
Definition relcache.c:2897
Snapshot GetTransactionSnapshot(void)
Definition snapmgr.c:272
void PushActiveSnapshot(Snapshot snapshot)
Definition snapmgr.c:682
void PopActiveSnapshot(void)
Definition snapmgr.c:775
void RelationDropStorage(Relation rel)
Definition storage.c:207
Oid relId
Definition rel.h:40
Oid dbId
Definition rel.h:41
void CheckTableNotInUse(Relation rel, const char *stmt)
Definition tablecmds.c:4508
TransactionId GetTopTransactionIdIfAny(void)
Definition xact.c:443
void StartTransactionCommand(void)
Definition xact.c:3112
void CommitTransactionCommand(void)
Definition xact.c:3210

References AccessExclusiveLock, Assert, CacheInvalidateRelcache(), CatalogTupleDelete(), CheckTableNotInUse(), CommitTransactionCommand(), LockRelId::dbId, DeleteAttributeTuples(), DeleteInheritsTuple(), DeleteRelationTuple(), elog, ereport, errcode(), errmsg, ERROR, fb(), get_rel_persistence(), GetTopTransactionIdIfAny(), GetTransactionSnapshot(), heap_attisnull(), HeapTupleIsValid, index_close(), index_concurrently_set_dead(), INDEX_DROP_CLEAR_VALID, index_open(), index_set_state_flags(), IndexGetRelation(), InvalidOid, InvalidTransactionId, LockRelationIdForSession(), NoLock, ObjectIdGetDatum(), pgstat_drop_relation(), PopActiveSnapshot(), PushActiveSnapshot(), RelationDropStorage(), RelationForgetRelation(), RelationGetDescr, ReleaseSysCache(), LockRelId::relId, RemoveStatistics(), RowExclusiveLock, SearchSysCache1(), SET_LOCKTAG_RELATION, ShareUpdateExclusiveLock, StartTransactionCommand(), HeapTupleData::t_self, table_close(), table_open(), TransferPredicateLocksToHeapRelation(), UnlockRelationIdForSession(), and WaitForLockers().

Referenced by doDeletion().

◆ index_set_state_flags()

void index_set_state_flags ( Oid  indexId,
IndexStateFlagsAction  action 
)
extern

Definition at line 3536 of file index.c.

3537{
3541
3542 /* Open pg_index and fetch a writable copy of the index's tuple */
3544
3548 elog(ERROR, "cache lookup failed for index %u", indexId);
3550
3551 /* Perform the requested state change on the copy */
3552 switch (action)
3553 {
3555 /* Set indisready during a CREATE INDEX CONCURRENTLY sequence */
3556 Assert(indexForm->indislive);
3557 Assert(!indexForm->indisready);
3558 Assert(!indexForm->indisvalid);
3559 indexForm->indisready = true;
3560 break;
3562 /* Set indisvalid during a CREATE INDEX CONCURRENTLY sequence */
3563 Assert(indexForm->indislive);
3564 Assert(indexForm->indisready);
3565 Assert(!indexForm->indisvalid);
3566 indexForm->indisvalid = true;
3567 break;
3569
3570 /*
3571 * Clear indisvalid during a DROP INDEX CONCURRENTLY sequence
3572 *
3573 * If indisready == true we leave it set so the index still gets
3574 * maintained by active transactions. We only need to ensure that
3575 * indisvalid is false. (We don't assert that either is initially
3576 * true, though, since we want to be able to retry a DROP INDEX
3577 * CONCURRENTLY that failed partway through.)
3578 *
3579 * Note: the CLUSTER logic assumes that indisclustered cannot be
3580 * set on any invalid index, so clear that flag too. For
3581 * cleanliness, also clear indisreplident.
3582 */
3583 indexForm->indisvalid = false;
3584 indexForm->indisclustered = false;
3585 indexForm->indisreplident = false;
3586 break;
3588
3589 /*
3590 * Clear indisready/indislive during DROP INDEX CONCURRENTLY
3591 *
3592 * We clear both indisready and indislive, because we not only
3593 * want to stop updates, we want to prevent sessions from touching
3594 * the index at all.
3595 */
3596 Assert(!indexForm->indisvalid);
3597 Assert(!indexForm->indisclustered);
3598 Assert(!indexForm->indisreplident);
3599 indexForm->indisready = false;
3600 indexForm->indislive = false;
3601 break;
3602 }
3603
3604 /* ... and update it */
3606
3608}

References Assert, CatalogTupleUpdate(), elog, ERROR, fb(), Form_pg_index, GETSTRUCT(), HeapTupleIsValid, INDEX_CREATE_SET_READY, INDEX_CREATE_SET_VALID, INDEX_DROP_CLEAR_VALID, INDEX_DROP_SET_DEAD, ObjectIdGetDatum(), RowExclusiveLock, SearchSysCacheCopy1, table_close(), and table_open().

Referenced by DefineIndex(), index_concurrently_build(), index_concurrently_set_dead(), and index_drop().

◆ IndexGetRelation()

◆ IndexSetParentIndex()

void IndexSetParentIndex ( Relation  partitionIdx,
Oid  parentOid 
)
extern

Definition at line 4601 of file indexcmds.c.

4602{
4604 ScanKeyData key[2];
4605 SysScanDesc scan;
4606 Oid partRelid = RelationGetRelid(partitionIdx);
4607 HeapTuple tuple;
4608 bool fix_dependencies;
4609
4610 /* Make sure this is an index */
4611 Assert(partitionIdx->rd_rel->relkind == RELKIND_INDEX ||
4612 partitionIdx->rd_rel->relkind == RELKIND_PARTITIONED_INDEX);
4613
4614 /*
4615 * Scan pg_inherits for rows linking our index to some parent.
4616 */
4618 ScanKeyInit(&key[0],
4622 ScanKeyInit(&key[1],
4625 Int32GetDatum(1));
4627 NULL, 2, key);
4628 tuple = systable_getnext(scan);
4629
4630 if (!HeapTupleIsValid(tuple))
4631 {
4632 if (parentOid == InvalidOid)
4633 {
4634 /*
4635 * No pg_inherits row, and no parent wanted: nothing to do in this
4636 * case.
4637 */
4638 fix_dependencies = false;
4639 }
4640 else
4641 {
4643 fix_dependencies = true;
4644 }
4645 }
4646 else
4647 {
4649
4650 if (parentOid == InvalidOid)
4651 {
4652 /*
4653 * There exists a pg_inherits row, which we want to clear; do so.
4654 */
4656 fix_dependencies = true;
4657 }
4658 else
4659 {
4660 /*
4661 * A pg_inherits row exists. If it's the same we want, then we're
4662 * good; if it differs, that amounts to a corrupt catalog and
4663 * should not happen.
4664 */
4665 if (inhForm->inhparent != parentOid)
4666 {
4667 /* unexpected: we should not get called in this case */
4668 elog(ERROR, "bogus pg_inherit row: inhrelid %u inhparent %u",
4669 inhForm->inhrelid, inhForm->inhparent);
4670 }
4671
4672 /* already in the right state */
4673 fix_dependencies = false;
4674 }
4675 }
4676
4677 /* done with pg_inherits */
4678 systable_endscan(scan);
4680
4681 /* set relhassubclass if an index partition has been added to the parent */
4682 if (OidIsValid(parentOid))
4683 {
4686 }
4687
4688 /* set relispartition correctly on the partition */
4690
4691 if (fix_dependencies)
4692 {
4693 /*
4694 * Insert/delete pg_depend rows. If setting a parent, add PARTITION
4695 * dependencies on the parent index and the table; if removing a
4696 * parent, delete PARTITION dependencies.
4697 */
4698 if (OidIsValid(parentOid))
4699 {
4701 ObjectAddress parentIdx;
4702 ObjectAddress partitionTbl;
4703
4707 partitionIdx->rd_index->indrelid);
4708 recordDependencyOn(&partIdx, &parentIdx,
4710 recordDependencyOn(&partIdx, &partitionTbl,
4712 }
4713 else
4714 {
4721 }
4722
4723 /* make our updates visible */
4725 }
4726}
static void update_relispartition(Oid relationId, bool newval)
Definition indexcmds.c:4733
static void fix_dependencies(ArchiveHandle *AH)
END_CATALOG_STRUCT typedef FormData_pg_inherits * Form_pg_inherits
Definition pg_inherits.h:49

References Assert, BTEqualStrategyNumber, CatalogTupleDelete(), CommandCounterIncrement(), deleteDependencyRecordsForClass(), DEPENDENCY_PARTITION_PRI, DEPENDENCY_PARTITION_SEC, elog, ERROR, fb(), fix_dependencies(), Form_pg_inherits, GETSTRUCT(), HeapTupleIsValid, Int32GetDatum(), InvalidOid, LockRelationOid(), ObjectAddressSet, ObjectIdGetDatum(), OidIsValid, RelationData::rd_index, RelationData::rd_rel, recordDependencyOn(), relation_close(), relation_open(), RelationGetRelid, RowExclusiveLock, ScanKeyInit(), SetRelationHasSubclass(), ShareUpdateExclusiveLock, StoreSingleInheritance(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, and update_relispartition().

Referenced by ATExecAttachPartitionIdx(), AttachPartitionEnsureIndexes(), DefineIndex(), and DetachPartitionFinalize().

◆ itemptr_decode()

static void itemptr_decode ( ItemPointer  itemptr,
int64  encoded 
)
inlinestatic

Definition at line 218 of file index.h.

219{
220 BlockNumber block = (BlockNumber) (encoded >> 16);
221 OffsetNumber offset = (OffsetNumber) (encoded & 0xFFFF);
222
223 ItemPointerSet(itemptr, block, offset);
224}
uint32 BlockNumber
Definition block.h:31
static void ItemPointerSet(ItemPointerData *pointer, BlockNumber blockNumber, OffsetNumber offNum)
Definition itemptr.h:135
uint16 OffsetNumber
Definition off.h:24

References fb(), and ItemPointerSet().

Referenced by heapam_index_validate_scan().

◆ itemptr_encode()

static int64 itemptr_encode ( const ItemPointerData *  itemptr)
inlinestatic

Definition at line 197 of file index.h.

198{
199 BlockNumber block = ItemPointerGetBlockNumber(itemptr);
202
203 /*
204 * Use the 16 least significant bits for the offset. 32 adjacent bits are
205 * used for the block number. Since remaining bits are unused, there
206 * cannot be negative encoded values (We assume a two's complement
207 * representation).
208 */
209 encoded = ((uint64) block << 16) | (uint16) offset;
210
211 return encoded;
212}
uint64_t uint64
Definition c.h:684
static OffsetNumber ItemPointerGetOffsetNumber(const ItemPointerData *pointer)
Definition itemptr.h:124
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
Definition itemptr.h:103

References fb(), ItemPointerGetBlockNumber(), and ItemPointerGetOffsetNumber().

Referenced by validate_index_callback().

◆ reindex_index()

void reindex_index ( const ReindexStmt *  stmt,
Oid  indexId,
bool  skip_constraint_checks,
char  persistence,
const ReindexParams *  params 
)
extern

Definition at line 3641 of file index.c.

3644{
3645 Relation iRel,
3646 heapRelation;
3647 Oid heapId;
3648 Oid save_userid;
3649 int save_sec_context;
3650 int save_nestlevel;
3651 IndexInfo *indexInfo;
3652 bool skipped_constraint = false;
3653 PGRUsage ru0;
3654 bool progress = ((params->options & REINDEXOPT_REPORT_PROGRESS) != 0);
3655 bool set_tablespace = false;
3656
3658
3659 /*
3660 * Open and lock the parent heap relation. ShareLock is sufficient since
3661 * we only need to be sure no schema or data changes are going on.
3662 */
3664 (params->options & REINDEXOPT_MISSING_OK) != 0);
3665 /* if relation is missing, leave */
3666 if (!OidIsValid(heapId))
3667 return;
3668
3669 if ((params->options & REINDEXOPT_MISSING_OK) != 0)
3670 heapRelation = try_table_open(heapId, ShareLock);
3671 else
3672 heapRelation = table_open(heapId, ShareLock);
3673
3674 /* if relation is gone, leave */
3675 if (!heapRelation)
3676 return;
3677
3678 /*
3679 * Switch to the table owner's userid, so that any index functions are run
3680 * as that user. Also lock down security-restricted operations and
3681 * arrange to make GUC variable changes local to this command.
3682 */
3683 GetUserIdAndSecContext(&save_userid, &save_sec_context);
3684 SetUserIdAndSecContext(heapRelation->rd_rel->relowner,
3685 save_sec_context | SECURITY_RESTRICTED_OPERATION);
3686 save_nestlevel = NewGUCNestLevel();
3688
3689 if (progress)
3690 {
3691 const int progress_cols[] = {
3694 };
3695 const int64 progress_vals[] = {
3697 indexId
3698 };
3699
3701 heapId);
3703 }
3704
3705 /*
3706 * Open the target index relation and get an exclusive lock on it, to
3707 * ensure that no one else is touching this particular index.
3708 */
3709 if ((params->options & REINDEXOPT_MISSING_OK) != 0)
3711 else
3713
3714 /* if index relation is gone, leave */
3715 if (!iRel)
3716 {
3717 /* Roll back any GUC changes */
3718 AtEOXact_GUC(false, save_nestlevel);
3719
3720 /* Restore userid and security context */
3721 SetUserIdAndSecContext(save_userid, save_sec_context);
3722
3723 /* Close parent heap relation, but keep locks */
3724 table_close(heapRelation, NoLock);
3725 return;
3726 }
3727
3728 if (progress)
3730 iRel->rd_rel->relam);
3731
3732 /*
3733 * If a statement is available, telling that this comes from a REINDEX
3734 * command, collect the index for event triggers.
3735 */
3736 if (stmt)
3737 {
3738 ObjectAddress address;
3739
3743 (const Node *) stmt);
3744 }
3745
3746 /*
3747 * Partitioned indexes should never get processed here, as they have no
3748 * physical storage.
3749 */
3750 if (iRel->rd_rel->relkind == RELKIND_PARTITIONED_INDEX)
3751 elog(ERROR, "cannot reindex partitioned index \"%s.%s\"",
3754
3755 /*
3756 * Don't allow reindex on temp tables of other backends ... their local
3757 * buffer manager is not going to cope.
3758 */
3760 ereport(ERROR,
3762 errmsg("cannot reindex temporary tables of other sessions")));
3763
3764 /*
3765 * Don't allow reindex of an invalid index on TOAST table. This is a
3766 * leftover from a failed REINDEX CONCURRENTLY, and if rebuilt it would
3767 * not be possible to drop it anymore.
3768 */
3771 ereport(ERROR,
3773 errmsg("cannot reindex invalid index on TOAST table")));
3774
3775 /*
3776 * System relations cannot be moved even if allow_system_table_mods is
3777 * enabled to keep things consistent with the concurrent case where all
3778 * the indexes of a relation are processed in series, including indexes of
3779 * toast relations.
3780 *
3781 * Note that this check is not part of CheckRelationTableSpaceMove() as it
3782 * gets used for ALTER TABLE SET TABLESPACE that could cascade across
3783 * toast relations.
3784 */
3785 if (OidIsValid(params->tablespaceOid) &&
3787 ereport(ERROR,
3789 errmsg("cannot move system relation \"%s\"",
3791
3792 /* Check if the tablespace of this index needs to be changed */
3793 if (OidIsValid(params->tablespaceOid) &&
3795 set_tablespace = true;
3796
3797 /*
3798 * Also check for active uses of the index in the current transaction; we
3799 * don't want to reindex underneath an open indexscan.
3800 */
3801 CheckTableNotInUse(iRel, "REINDEX INDEX");
3802
3803 /* Set new tablespace, if requested */
3804 if (set_tablespace)
3805 {
3806 /* Update its pg_class row */
3808
3809 /*
3810 * Schedule unlinking of the old index storage at transaction commit.
3811 */
3814
3815 /* Make sure the reltablespace change is visible */
3817 }
3818
3819 /*
3820 * All predicate locks on the index are about to be made invalid. Promote
3821 * them to relation locks on the heap.
3822 */
3824
3825 /* Fetch info needed for index_build */
3826 indexInfo = BuildIndexInfo(iRel);
3827
3828 /* If requested, skip checking uniqueness/exclusion constraints */
3830 {
3831 if (indexInfo->ii_Unique || indexInfo->ii_ExclusionOps != NULL)
3832 skipped_constraint = true;
3833 indexInfo->ii_Unique = false;
3834 indexInfo->ii_ExclusionOps = NULL;
3835 indexInfo->ii_ExclusionProcs = NULL;
3836 indexInfo->ii_ExclusionStrats = NULL;
3837 }
3838
3839 /* Suppress use of the target index while rebuilding it */
3841
3842 /* Create a new physical relation for the index */
3843 RelationSetNewRelfilenumber(iRel, persistence);
3844
3845 /* Initialize the index and rebuild */
3846 /* Note: we do not need to re-establish pkey setting */
3847 index_build(heapRelation, iRel, indexInfo, true, true, progress);
3848
3849 /* Re-allow use of target index */
3851
3852 /*
3853 * If the index is marked invalid/not-ready/dead (ie, it's from a failed
3854 * CREATE INDEX CONCURRENTLY, or a DROP INDEX CONCURRENTLY failed midway),
3855 * and we didn't skip a uniqueness check, we can now mark it valid. This
3856 * allows REINDEX to be used to clean up in such cases.
3857 *
3858 * We can also reset indcheckxmin, because we have now done a
3859 * non-concurrent index build, *except* in the case where index_build
3860 * found some still-broken HOT chains. If it did, and we don't have to
3861 * change any of the other flags, we just leave indcheckxmin alone (note
3862 * that index_build won't have changed it, because this is a reindex).
3863 * This is okay and desirable because not updating the tuple leaves the
3864 * index's usability horizon (recorded as the tuple's xmin value) the same
3865 * as it was.
3866 *
3867 * But, if the index was invalid/not-ready/dead and there were broken HOT
3868 * chains, we had better force indcheckxmin true, because the normal
3869 * argument that the HOT chains couldn't conflict with the index is
3870 * suspect for an invalid index. (A conflict is definitely possible if
3871 * the index was dead. It probably shouldn't happen otherwise, but let's
3872 * be conservative.) In this case advancing the usability horizon is
3873 * appropriate.
3874 *
3875 * Another reason for avoiding unnecessary updates here is that while
3876 * reindexing pg_index itself, we must not try to update tuples in it.
3877 * pg_index's indexes should always have these flags in their clean state,
3878 * so that won't happen.
3879 */
3880 if (!skipped_constraint)
3881 {
3885 bool index_bad;
3886
3888
3892 elog(ERROR, "cache lookup failed for index %u", indexId);
3894
3895 index_bad = (!indexForm->indisvalid ||
3896 !indexForm->indisready ||
3897 !indexForm->indislive);
3898 if (index_bad ||
3899 (indexForm->indcheckxmin && !indexInfo->ii_BrokenHotChain))
3900 {
3901 if (!indexInfo->ii_BrokenHotChain)
3902 indexForm->indcheckxmin = false;
3903 else if (index_bad)
3904 indexForm->indcheckxmin = true;
3905 indexForm->indisvalid = true;
3906 indexForm->indisready = true;
3907 indexForm->indislive = true;
3909
3910 /*
3911 * Invalidate the relcache for the table, so that after we commit
3912 * all sessions will refresh the table's index list. This ensures
3913 * that if anyone misses seeing the pg_index row during this
3914 * update, they'll refresh their list before attempting any update
3915 * on the table.
3916 */
3917 CacheInvalidateRelcache(heapRelation);
3918 }
3919
3921 }
3922
3923 /* Log what we did */
3924 if ((params->options & REINDEXOPT_VERBOSE) != 0)
3925 ereport(INFO,
3926 (errmsg("index \"%s\" was reindexed",
3928 errdetail_internal("%s",
3929 pg_rusage_show(&ru0))));
3930
3931 /* Roll back any GUC changes executed by index functions */
3932 AtEOXact_GUC(false, save_nestlevel);
3933
3934 /* Restore userid and security context */
3935 SetUserIdAndSecContext(save_userid, save_sec_context);
3936
3937 /* Close rels, but keep locks */
3939 table_close(heapRelation, NoLock);
3940
3941 if (progress)
3943}
void pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
void pgstat_progress_update_param(int index, int64 val)
void pgstat_progress_end_command(void)
@ PROGRESS_COMMAND_CREATE_INDEX
bool IsToastNamespace(Oid namespaceId)
Definition catalog.c:277
int int errdetail_internal(const char *fmt,...) pg_attribute_printf(1
#define INFO
Definition elog.h:35
void EventTriggerCollectSimpleCommand(ObjectAddress address, ObjectAddress secondaryObject, const Node *parsetree)
#define stmt
static void ResetReindexProcessing(void)
Definition index.c:4203
static void SetReindexProcessing(Oid heapOid, Oid indexOid)
Definition index.c:4184
#define REINDEXOPT_MISSING_OK
Definition index.h:49
#define REINDEXOPT_REPORT_PROGRESS
Definition index.h:48
#define REINDEXOPT_VERBOSE
Definition index.h:47
Relation try_index_open(Oid relationId, LOCKMODE lockmode)
Definition indexam.c:153
#define ShareLock
Definition lockdefs.h:40
char * get_rel_name(Oid relid)
Definition lsyscache.c:2242
bool get_index_isvalid(Oid index_oid)
Definition lsyscache.c:3939
char * get_namespace_name(Oid nspid)
Definition lsyscache.c:3682
const ObjectAddress InvalidObjectAddress
const char * pg_rusage_show(const PGRUsage *ru0)
Definition pg_rusage.c:40
void pg_rusage_init(PGRUsage *ru0)
Definition pg_rusage.c:27
#define PROGRESS_CREATEIDX_ACCESS_METHOD_OID
Definition progress.h:112
#define PROGRESS_CREATEIDX_COMMAND_REINDEX
Definition progress.h:141
#define PROGRESS_CREATEIDX_INDEX_OID
Definition progress.h:111
#define PROGRESS_CREATEIDX_COMMAND
Definition progress.h:110
#define RELATION_IS_OTHER_TEMP(relation)
Definition rel.h:678
void RelationSetNewRelfilenumber(Relation relation, char persistence)
Definition relcache.c:3779
void RelationAssumeNewRelfilelocator(Relation relation)
Definition relcache.c:3982
Oid tablespaceOid
Definition index.h:42
uint32 options
Definition index.h:41
Relation try_table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:60
bool CheckRelationTableSpaceMove(Relation rel, Oid newTableSpaceId)
Definition tablecmds.c:3772
void SetRelationTableSpace(Relation rel, Oid newTableSpaceId, RelFileNumber newRelFilenumber)
Definition tablecmds.c:3829

References AccessExclusiveLock, AtEOXact_GUC(), BuildIndexInfo(), CacheInvalidateRelcache(), CatalogTupleUpdate(), CheckRelationTableSpaceMove(), CheckTableNotInUse(), CommandCounterIncrement(), elog, ereport, errcode(), errdetail_internal(), errmsg, ERROR, EventTriggerCollectSimpleCommand(), fb(), Form_pg_index, get_index_isvalid(), get_namespace_name(), get_rel_name(), GETSTRUCT(), GetUserIdAndSecContext(), HeapTupleIsValid, IndexInfo::ii_BrokenHotChain, IndexInfo::ii_ExclusionOps, IndexInfo::ii_ExclusionProcs, IndexInfo::ii_ExclusionStrats, IndexInfo::ii_Unique, index_build(), index_close(), index_open(), IndexGetRelation(), INFO, InvalidObjectAddress, InvalidOid, IsSystemRelation(), IsToastNamespace(), NewGUCNestLevel(), NoLock, ObjectAddressSet, ObjectIdGetDatum(), OidIsValid, ReindexParams::options, pg_rusage_init(), pg_rusage_show(), pgstat_progress_end_command(), pgstat_progress_start_command(), pgstat_progress_update_multi_param(), pgstat_progress_update_param(), progress, PROGRESS_COMMAND_CREATE_INDEX, PROGRESS_CREATEIDX_ACCESS_METHOD_OID, PROGRESS_CREATEIDX_COMMAND, PROGRESS_CREATEIDX_COMMAND_REINDEX, PROGRESS_CREATEIDX_INDEX_OID, RelationData::rd_rel, REINDEXOPT_MISSING_OK, REINDEXOPT_REPORT_PROGRESS, REINDEXOPT_VERBOSE, RELATION_IS_OTHER_TEMP, RelationAssumeNewRelfilelocator(), RelationDropStorage(), RelationGetNamespace, RelationGetRelationName, RelationSetNewRelfilenumber(), ResetReindexProcessing(), RestrictSearchPath(), RowExclusiveLock, SearchSysCacheCopy1, SECURITY_RESTRICTED_OPERATION, SetReindexProcessing(), SetRelationTableSpace(), SetUserIdAndSecContext(), ShareLock, stmt, table_close(), table_open(), ReindexParams::tablespaceOid, TransferPredicateLocksToHeapRelation(), try_index_open(), and try_table_open().

Referenced by reindex_relation(), ReindexIndex(), and ReindexMultipleInternal().

◆ reindex_relation()

bool reindex_relation ( const ReindexStmt *  stmt,
Oid  relid,
int  flags,
const ReindexParams *  params 
)
extern

Definition at line 3981 of file index.c.

3983{
3984 Relation rel;
3986 List *indexIds;
3987 char persistence;
3988 bool result = false;
3990 int i;
3991
3992 /*
3993 * Open and lock the relation. ShareLock is sufficient since we only need
3994 * to prevent schema and data changes in it. The lock level used here
3995 * should match ReindexTable().
3996 */
3997 if ((params->options & REINDEXOPT_MISSING_OK) != 0)
3998 rel = try_table_open(relid, ShareLock);
3999 else
4000 rel = table_open(relid, ShareLock);
4001
4002 /* if relation is gone, leave */
4003 if (!rel)
4004 return false;
4005
4006 /*
4007 * Partitioned tables should never get processed here, as they have no
4008 * physical storage.
4009 */
4010 if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
4011 elog(ERROR, "cannot reindex partitioned table \"%s.%s\"",
4014
4015 toast_relid = rel->rd_rel->reltoastrelid;
4016
4017 /*
4018 * Get the list of index OIDs for this relation. (We trust the relcache
4019 * to get this with a sequential scan if ignoring system indexes.)
4020 */
4022
4024 {
4025 /* Suppress use of all the indexes until they are rebuilt */
4027
4028 /*
4029 * Make the new heap contents visible --- now things might be
4030 * inconsistent!
4031 */
4033 }
4034
4035 /*
4036 * Reindex the toast table, if any, before the main table.
4037 *
4038 * This helps in cases where a corruption in the toast table's index would
4039 * otherwise error and stop REINDEX TABLE command when it tries to fetch a
4040 * toasted datum. This way. the toast table's index is rebuilt and fixed
4041 * before it is used for reindexing the main table.
4042 *
4043 * It is critical to call reindex_relation() *after* the call to
4044 * RelationGetIndexList() returning the list of indexes on the relation,
4045 * because reindex_relation() will call CommandCounterIncrement() after
4046 * every reindex_index(). See REINDEX_REL_SUPPRESS_INDEX_USE for more
4047 * details.
4048 */
4050 {
4051 /*
4052 * Note that this should fail if the toast relation is missing, so
4053 * reset REINDEXOPT_MISSING_OK. Even if a new tablespace is set for
4054 * the parent relation, the indexes on its toast table are not moved.
4055 * This rule is enforced by setting tablespaceOid to InvalidOid.
4056 */
4057 ReindexParams newparams = *params;
4058
4060 newparams.tablespaceOid = InvalidOid;
4062 }
4063
4064 /*
4065 * Compute persistence of indexes: same as that of owning rel, unless
4066 * caller specified otherwise.
4067 */
4069 persistence = RELPERSISTENCE_UNLOGGED;
4070 else if (flags & REINDEX_REL_FORCE_INDEXES_PERMANENT)
4071 persistence = RELPERSISTENCE_PERMANENT;
4072 else
4073 persistence = rel->rd_rel->relpersistence;
4074
4075 /* Reindex all the indexes. */
4076 i = 1;
4077 foreach(indexId, indexIds)
4078 {
4079 Oid indexOid = lfirst_oid(indexId);
4081
4082 /*
4083 * Skip any invalid indexes on a TOAST table. These can only be
4084 * duplicate leftovers from a failed REINDEX CONCURRENTLY, and if
4085 * rebuilt it would not be possible to drop them anymore.
4086 */
4088 !get_index_isvalid(indexOid))
4089 {
4092 errmsg("cannot reindex invalid index \"%s.%s\" on TOAST table, skipping",
4094 get_rel_name(indexOid))));
4095
4096 /*
4097 * Remove this invalid toast index from the reindex pending list,
4098 * as it is skipped here due to the hard failure that would happen
4099 * in reindex_index(), should we try to process it.
4100 */
4102 RemoveReindexPending(indexOid);
4103 continue;
4104 }
4105
4106 reindex_index(stmt, indexOid, !(flags & REINDEX_REL_CHECK_CONSTRAINTS),
4107 persistence, params);
4108
4110
4111 /* Index should no longer be in the pending list */
4112 Assert(!ReindexIsProcessingIndex(indexOid));
4113
4114 /* Set index rebuild count */
4116 i);
4117 i++;
4118 }
4119
4120 /*
4121 * Close rel, but continue to hold the lock.
4122 */
4123 table_close(rel, NoLock);
4124
4125 result |= (indexIds != NIL);
4126
4127 return result;
4128}
#define WARNING
Definition elog.h:37
bool ReindexIsProcessingIndex(Oid indexOid)
Definition index.c:4173
static void RemoveReindexPending(Oid indexOid)
Definition index.c:4233
bool reindex_relation(const ReindexStmt *stmt, Oid relid, int flags, const ReindexParams *params)
Definition index.c:3981
static void SetReindexPending(List *indexes)
Definition index.c:4217
void reindex_index(const ReindexStmt *stmt, Oid indexId, bool skip_constraint_checks, char persistence, const ReindexParams *params)
Definition index.c:3641
#define REINDEX_REL_PROCESS_TOAST
Definition index.h:166
#define REINDEX_REL_FORCE_INDEXES_UNLOGGED
Definition index.h:169
#define REINDEX_REL_SUPPRESS_INDEX_USE
Definition index.h:167
#define REINDEX_REL_FORCE_INDEXES_PERMANENT
Definition index.h:170
#define REINDEX_REL_CHECK_CONSTRAINTS
Definition index.h:168
Oid get_rel_namespace(Oid relid)
Definition lsyscache.c:2266
#define PROGRESS_REPACK_INDEX_REBUILD_COUNT
Definition progress.h:94
List * RelationGetIndexList(Relation relation)
Definition relcache.c:4848

References Assert, CommandCounterIncrement(), elog, ereport, errcode(), errmsg, ERROR, fb(), get_index_isvalid(), get_namespace_name(), get_rel_name(), get_rel_namespace(), i, InvalidOid, IsToastNamespace(), lfirst_oid, NIL, NoLock, OidIsValid, ReindexParams::options, pgstat_progress_update_param(), PROGRESS_REPACK_INDEX_REBUILD_COUNT, RelationData::rd_rel, reindex_index(), REINDEX_REL_CHECK_CONSTRAINTS, REINDEX_REL_FORCE_INDEXES_PERMANENT, REINDEX_REL_FORCE_INDEXES_UNLOGGED, REINDEX_REL_PROCESS_TOAST, REINDEX_REL_SUPPRESS_INDEX_USE, reindex_relation(), ReindexIsProcessingIndex(), REINDEXOPT_MISSING_OK, RelationGetIndexList(), RelationGetNamespace, RelationGetRelationName, RemoveReindexPending(), result, SetReindexPending(), ShareLock, stmt, table_close(), table_open(), try_table_open(), and WARNING.

Referenced by ExecuteTruncateGuts(), finish_heap_swap(), reindex_relation(), ReindexMultipleInternal(), and ReindexTable().

◆ ReindexIsProcessingHeap()

bool ReindexIsProcessingHeap ( Oid  heapOid)
extern

Definition at line 4152 of file index.c.

4153{
4154 return heapOid == currentlyReindexedHeap;
4155}
static Oid currentlyReindexedHeap
Definition index.c:4142

References currentlyReindexedHeap.

◆ ReindexIsProcessingIndex()

bool ReindexIsProcessingIndex ( Oid  indexOid)
extern

Definition at line 4173 of file index.c.

4174{
4175 return indexOid == currentlyReindexedIndex ||
4177}
static Oid currentlyReindexedIndex
Definition index.c:4143
bool list_member_oid(const List *list, Oid datum)
Definition list.c:722

References currentlyReindexedIndex, list_member_oid(), and pendingReindexedIndexes.

Referenced by CatalogIndexInsert(), reindex_relation(), systable_beginscan(), and systable_beginscan_ordered().

◆ ResetReindexState()

void ResetReindexState ( int  nestLevel)
extern

Definition at line 4246 of file index.c.

4247{
4248 /*
4249 * Because reindexing is not re-entrant, we don't need to cope with nested
4250 * reindexing states. We just need to avoid messing up the outer-level
4251 * state in case a subtransaction fails within a REINDEX. So checking the
4252 * current nest level against that of the reindex operation is sufficient.
4253 */
4254 if (reindexingNestLevel >= nestLevel)
4255 {
4258
4259 /*
4260 * We needn't try to release the contents of pendingReindexedIndexes;
4261 * that list should be in a transaction-lifespan context, so it will
4262 * go away automatically.
4263 */
4265
4267 }
4268}
static int reindexingNestLevel
Definition index.c:4145

References currentlyReindexedHeap, currentlyReindexedIndex, InvalidOid, NIL, pendingReindexedIndexes, and reindexingNestLevel.

Referenced by AbortSubTransaction(), and AbortTransaction().

◆ RestoreReindexState()

void RestoreReindexState ( const void *  reindexstate)
extern

Definition at line 4304 of file index.c.

4305{
4307 int c = 0;
4308 MemoryContext oldcontext;
4309
4310 currentlyReindexedHeap = sistate->currentlyReindexedHeap;
4311 currentlyReindexedIndex = sistate->currentlyReindexedIndex;
4312
4315 for (c = 0; c < sistate->numPendingReindexedIndexes; ++c)
4318 sistate->pendingReindexedIndexes[c]);
4319 MemoryContextSwitchTo(oldcontext);
4320
4321 /* Note the worker has its own transaction nesting level */
4323}
MemoryContext TopMemoryContext
Definition mcxt.c:167
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:138
char * c
int GetCurrentTransactionNestLevel(void)
Definition xact.c:931

References Assert, currentlyReindexedHeap, currentlyReindexedIndex, fb(), GetCurrentTransactionNestLevel(), lappend_oid(), MemoryContextSwitchTo(), NIL, pendingReindexedIndexes, reindexingNestLevel, and TopMemoryContext.

Referenced by ParallelWorkerMain().

◆ SerializeReindexState()

void SerializeReindexState ( Size  maxsize,
char *  start_address 
)
extern

Definition at line 4286 of file index.c.

4287{
4289 int c = 0;
4290 ListCell *lc;
4291
4292 sistate->currentlyReindexedHeap = currentlyReindexedHeap;
4293 sistate->currentlyReindexedIndex = currentlyReindexedIndex;
4294 sistate->numPendingReindexedIndexes = list_length(pendingReindexedIndexes);
4295 foreach(lc, pendingReindexedIndexes)
4296 sistate->pendingReindexedIndexes[c++] = lfirst_oid(lc);
4297}

References currentlyReindexedHeap, currentlyReindexedIndex, fb(), lfirst_oid, list_length(), and pendingReindexedIndexes.

Referenced by InitializeParallelDSM().

◆ validate_index()

void validate_index ( Oid  heapId,
Oid  indexId,
Snapshot  snapshot 
)
extern

Definition at line 3383 of file index.c.

3384{
3385 Relation heapRelation,
3386 indexRelation;
3387 IndexInfo *indexInfo;
3390 Oid save_userid;
3391 int save_sec_context;
3392 int save_nestlevel;
3393
3394 {
3395 const int progress_index[] = {
3401 };
3402 const int64 progress_vals[] = {
3404 0, 0, 0, 0
3405 };
3406
3408 }
3409
3410 /* Open and lock the parent heap relation */
3412
3413 /*
3414 * Switch to the table owner's userid, so that any index functions are run
3415 * as that user. Also lock down security-restricted operations and
3416 * arrange to make GUC variable changes local to this command.
3417 */
3418 GetUserIdAndSecContext(&save_userid, &save_sec_context);
3419 SetUserIdAndSecContext(heapRelation->rd_rel->relowner,
3420 save_sec_context | SECURITY_RESTRICTED_OPERATION);
3421 save_nestlevel = NewGUCNestLevel();
3423
3424 indexRelation = index_open(indexId, RowExclusiveLock);
3425
3426 /*
3427 * Fetch info needed for index_insert. (You might think this should be
3428 * passed in from DefineIndex, but its copy is long gone due to having
3429 * been built in a previous transaction.)
3430 */
3431 indexInfo = BuildIndexInfo(indexRelation);
3432
3433 /* mark build is concurrent just for consistency */
3434 indexInfo->ii_Concurrent = true;
3435
3436 /*
3437 * Scan the index and gather up all the TIDs into a tuplesort object.
3438 */
3439 ivinfo.index = indexRelation;
3440 ivinfo.heaprel = heapRelation;
3441 ivinfo.analyze_only = false;
3442 ivinfo.report_progress = true;
3443 ivinfo.estimated_count = true;
3444 ivinfo.message_level = DEBUG2;
3445 ivinfo.num_heap_tuples = heapRelation->rd_rel->reltuples;
3446 ivinfo.strategy = NULL;
3447
3448 /*
3449 * Encode TIDs as int8 values for the sort, rather than directly sorting
3450 * item pointers. This can be significantly faster, primarily because TID
3451 * is a pass-by-reference type on all platforms, whereas int8 is
3452 * pass-by-value on most platforms.
3453 */
3455 InvalidOid, false,
3458 state.htups = state.itups = state.tups_inserted = 0;
3459
3460 /* ambulkdelete updates progress metrics */
3463
3464 /* Execute the sort */
3465 {
3466 const int progress_index[] = {
3470 };
3471 const int64 progress_vals[] = {
3473 0, 0
3474 };
3475
3477 }
3478 tuplesort_performsort(state.tuplesort);
3479
3480 /*
3481 * Now scan the heap and "merge" it with the index
3482 */
3485 table_index_validate_scan(heapRelation,
3486 indexRelation,
3487 indexInfo,
3488 snapshot,
3489 &state);
3490
3491 /* Done with tuplesort object */
3492 tuplesort_end(state.tuplesort);
3493
3494 /* Make sure to release resources cached in indexInfo (if needed). */
3495 index_insert_cleanup(indexRelation, indexInfo);
3496
3497 elog(DEBUG2,
3498 "validate_index found %.0f heap tuples, %.0f index tuples; inserted %.0f missing tuples",
3499 state.htups, state.itups, state.tups_inserted);
3500
3501 /* Roll back any GUC changes executed by index functions */
3502 AtEOXact_GUC(false, save_nestlevel);
3503
3504 /* Restore userid and security context */
3505 SetUserIdAndSecContext(save_userid, save_sec_context);
3506
3507 /* Close rels, but keep locks */
3508 index_close(indexRelation, NoLock);
3509 table_close(heapRelation, NoLock);
3510}
#define DEBUG2
Definition elog.h:30
int maintenance_work_mem
Definition globals.c:135
static bool validate_index_callback(ItemPointer itemptr, void *opaque)
Definition index.c:3516
IndexBulkDeleteResult * index_bulk_delete(IndexVacuumInfo *info, IndexBulkDeleteResult *istat, IndexBulkDeleteCallback callback, void *callback_state)
Definition indexam.c:773
void index_insert_cleanup(Relation indexRelation, IndexInfo *indexInfo)
Definition indexam.c:242
#define PROGRESS_CREATEIDX_PHASE_VALIDATE_TABLESCAN
Definition progress.h:127
#define PROGRESS_CREATEIDX_PHASE_VALIDATE_SORT
Definition progress.h:126
#define PROGRESS_CREATEIDX_PHASE_VALIDATE_IDXSCAN
Definition progress.h:125
static void table_index_validate_scan(Relation table_rel, Relation index_rel, IndexInfo *index_info, Snapshot snapshot, ValidateIndexState *state)
Definition tableam.h:1909
void tuplesort_performsort(Tuplesortstate *state)
Definition tuplesort.c:1260
void tuplesort_end(Tuplesortstate *state)
Definition tuplesort.c:848
#define TUPLESORT_NONE
Definition tuplesort.h:67
Tuplesortstate * tuplesort_begin_datum(Oid datumType, Oid sortOperator, Oid sortCollation, bool nullsFirstFlag, int workMem, SortCoordinate coordinate, int sortopt)

References AtEOXact_GUC(), BuildIndexInfo(), DEBUG2, elog, fb(), GetUserIdAndSecContext(), IndexInfo::ii_Concurrent, index_bulk_delete(), index_close(), index_insert_cleanup(), index_open(), InvalidOid, maintenance_work_mem, NewGUCNestLevel(), NoLock, pgstat_progress_update_multi_param(), pgstat_progress_update_param(), PROGRESS_CREATEIDX_PHASE, PROGRESS_CREATEIDX_PHASE_VALIDATE_IDXSCAN, PROGRESS_CREATEIDX_PHASE_VALIDATE_SORT, PROGRESS_CREATEIDX_PHASE_VALIDATE_TABLESCAN, PROGRESS_CREATEIDX_TUPLES_DONE, PROGRESS_CREATEIDX_TUPLES_TOTAL, PROGRESS_SCAN_BLOCKS_DONE, PROGRESS_SCAN_BLOCKS_TOTAL, RelationData::rd_rel, RestrictSearchPath(), RowExclusiveLock, SECURITY_RESTRICTED_OPERATION, SetUserIdAndSecContext(), ShareUpdateExclusiveLock, table_close(), table_index_validate_scan(), table_open(), tuplesort_begin_datum(), tuplesort_end(), TUPLESORT_NONE, tuplesort_performsort(), and validate_index_callback().

Referenced by DefineIndex(), and ReindexRelationConcurrently().