PostgreSQL Source Code  git master
indexing.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/xact.h"
#include "catalog/index.h"
#include "catalog/indexing.h"
#include "executor/executor.h"
#include "utils/rel.h"
Include dependency graph for indexing.c:

Go to the source code of this file.

Macros

#define CatalogTupleCheckConstraints(heapRel, tup)   ((void) 0)
 

Functions

CatalogIndexState CatalogOpenIndexes (Relation heapRel)
 
void CatalogCloseIndexes (CatalogIndexState indstate)
 
static void CatalogIndexInsert (CatalogIndexState indstate, HeapTuple heapTuple)
 
void CatalogTupleInsert (Relation heapRel, HeapTuple tup)
 
void CatalogTupleInsertWithInfo (Relation heapRel, HeapTuple tup, CatalogIndexState indstate)
 
void CatalogTuplesMultiInsertWithInfo (Relation heapRel, TupleTableSlot **slot, int ntuples, CatalogIndexState indstate)
 
void CatalogTupleUpdate (Relation heapRel, ItemPointer otid, HeapTuple tup)
 
void CatalogTupleUpdateWithInfo (Relation heapRel, ItemPointer otid, HeapTuple tup, CatalogIndexState indstate)
 
void CatalogTupleDelete (Relation heapRel, ItemPointer tid)
 

Macro Definition Documentation

◆ CatalogTupleCheckConstraints

#define CatalogTupleCheckConstraints (   heapRel,
  tup 
)    ((void) 0)

Function Documentation

◆ CatalogCloseIndexes()

void CatalogCloseIndexes ( CatalogIndexState  indstate)

Definition at line 61 of file indexing.c.

References ExecCloseIndices(), and pfree().

Referenced by AddNewAttributeTuples(), AppendAttributeTuples(), CatalogTupleInsert(), CatalogTupleUpdate(), copyTemplateDependencies(), InsertPgAttributeTuples(), inv_truncate(), inv_write(), recordMultipleDependencies(), and swap_relation_files().

62 {
63  ExecCloseIndices(indstate);
64  pfree(indstate);
65 }
void pfree(void *pointer)
Definition: mcxt.c:1169
void ExecCloseIndices(ResultRelInfo *resultRelInfo)
Definition: execIndexing.c:231

◆ CatalogIndexInsert()

static void CatalogIndexInsert ( CatalogIndexState  indstate,
HeapTuple  heapTuple 
)
static

Definition at line 75 of file indexing.c.

References Assert, att_isnull, attnum, CatalogTupleCheckConstraints, ExecDropSingleTupleTableSlot(), ExecStoreHeapTuple(), FormIndexDatum(), HeapTupleHasNulls, HeapTupleIsHeapOnly, i, IndexInfo::ii_ExclusionOps, IndexInfo::ii_Expressions, IndexInfo::ii_NumIndexKeyAttrs, IndexInfo::ii_Predicate, IndexInfo::ii_ReadyForInserts, index_insert(), INDEX_MAX_KEYS, MakeSingleTupleTableSlot(), TupleDescData::natts, NIL, RelationData::rd_index, ReindexIsProcessingIndex(), RelationGetDescr, RelationGetRelid, ResultRelInfo::ri_IndexRelationDescs, ResultRelInfo::ri_IndexRelationInfo, ResultRelInfo::ri_NumIndices, ResultRelInfo::ri_RelationDesc, HeapTupleHeaderData::t_bits, HeapTupleData::t_data, HeapTupleData::t_self, TTSOpsHeapTuple, TupleDescAttr, UNIQUE_CHECK_NO, UNIQUE_CHECK_YES, and values.

Referenced by CatalogTupleInsert(), CatalogTupleInsertWithInfo(), CatalogTuplesMultiInsertWithInfo(), CatalogTupleUpdate(), and CatalogTupleUpdateWithInfo().

76 {
77  int i;
78  int numIndexes;
79  RelationPtr relationDescs;
80  Relation heapRelation;
81  TupleTableSlot *slot;
82  IndexInfo **indexInfoArray;
84  bool isnull[INDEX_MAX_KEYS];
85 
86  /*
87  * HOT update does not require index inserts. But with asserts enabled we
88  * want to check that it'd be legal to currently insert into the
89  * table/index.
90  */
91 #ifndef USE_ASSERT_CHECKING
92  if (HeapTupleIsHeapOnly(heapTuple))
93  return;
94 #endif
95 
96  /*
97  * Get information from the state structure. Fall out if nothing to do.
98  */
99  numIndexes = indstate->ri_NumIndices;
100  if (numIndexes == 0)
101  return;
102  relationDescs = indstate->ri_IndexRelationDescs;
103  indexInfoArray = indstate->ri_IndexRelationInfo;
104  heapRelation = indstate->ri_RelationDesc;
105 
106  /* Need a slot to hold the tuple being examined */
107  slot = MakeSingleTupleTableSlot(RelationGetDescr(heapRelation),
108  &TTSOpsHeapTuple);
109  ExecStoreHeapTuple(heapTuple, slot, false);
110 
111  /*
112  * for each index, form and insert the index tuple
113  */
114  for (i = 0; i < numIndexes; i++)
115  {
116  IndexInfo *indexInfo;
117  Relation index;
118 
119  indexInfo = indexInfoArray[i];
120  index = relationDescs[i];
121 
122  /* If the index is marked as read-only, ignore it */
123  if (!indexInfo->ii_ReadyForInserts)
124  continue;
125 
126  /*
127  * Expressional and partial indexes on system catalogs are not
128  * supported, nor exclusion constraints, nor deferred uniqueness
129  */
130  Assert(indexInfo->ii_Expressions == NIL);
131  Assert(indexInfo->ii_Predicate == NIL);
132  Assert(indexInfo->ii_ExclusionOps == NULL);
133  Assert(index->rd_index->indimmediate);
134  Assert(indexInfo->ii_NumIndexKeyAttrs != 0);
135 
136  /* see earlier check above */
137 #ifdef USE_ASSERT_CHECKING
138  if (HeapTupleIsHeapOnly(heapTuple))
139  {
141  continue;
142  }
143 #endif /* USE_ASSERT_CHECKING */
144 
145  /*
146  * FormIndexDatum fills in its values and isnull parameters with the
147  * appropriate values for the column(s) of the index.
148  */
149  FormIndexDatum(indexInfo,
150  slot,
151  NULL, /* no expression eval to do */
152  values,
153  isnull);
154 
155  /*
156  * The index AM does the rest.
157  */
158  index_insert(index, /* index relation */
159  values, /* array of index Datums */
160  isnull, /* is-null flags */
161  &(heapTuple->t_self), /* tid of heap tuple */
162  heapRelation,
163  index->rd_index->indisunique ?
165  false,
166  indexInfo);
167  }
168 
170 }
void FormIndexDatum(IndexInfo *indexInfo, TupleTableSlot *slot, EState *estate, Datum *values, bool *isnull)
Definition: index.c:2662
int ri_NumIndices
Definition: execnodes.h:414
#define NIL
Definition: pg_list.h:65
Relation ri_RelationDesc
Definition: execnodes.h:411
List * ii_Predicate
Definition: execnodes.h:163
#define RelationGetDescr(relation)
Definition: rel.h:503
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1238
Definition: type.h:89
Form_pg_index rd_index
Definition: rel.h:187
int ii_NumIndexKeyAttrs
Definition: execnodes.h:159
ItemPointerData t_self
Definition: htup.h:65
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1254
bool ii_ReadyForInserts
Definition: execnodes.h:173
uintptr_t Datum
Definition: postgres.h:411
List * ii_Expressions
Definition: execnodes.h:161
#define HeapTupleIsHeapOnly(tuple)
Definition: htup_details.h:684
#define Assert(condition)
Definition: c.h:804
#define INDEX_MAX_KEYS
static Datum values[MAXATTR]
Definition: bootstrap.c:166
Oid * ii_ExclusionOps
Definition: execnodes.h:165
IndexInfo ** ri_IndexRelationInfo
Definition: execnodes.h:420
int i
const TupleTableSlotOps TTSOpsHeapTuple
Definition: execTuples.c:84
bool index_insert(Relation indexRelation, Datum *values, bool *isnull, ItemPointer heap_t_ctid, Relation heapRelation, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
Definition: indexam.c:176
bool ReindexIsProcessingIndex(Oid indexOid)
Definition: index.c:3969
#define RelationGetRelid(relation)
Definition: rel.h:477
RelationPtr ri_IndexRelationDescs
Definition: execnodes.h:417
TupleTableSlot * ExecStoreHeapTuple(HeapTuple tuple, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1352

◆ CatalogOpenIndexes()

CatalogIndexState CatalogOpenIndexes ( Relation  heapRel)

Definition at line 43 of file indexing.c.

References ExecOpenIndices(), makeNode, ResultRelInfo::ri_RangeTableIndex, ResultRelInfo::ri_RelationDesc, and ResultRelInfo::ri_TrigDesc.

Referenced by AddNewAttributeTuples(), AppendAttributeTuples(), CatalogTupleInsert(), CatalogTupleUpdate(), copyTemplateDependencies(), InsertPgAttributeTuples(), inv_truncate(), inv_write(), recordMultipleDependencies(), and swap_relation_files().

44 {
45  ResultRelInfo *resultRelInfo;
46 
47  resultRelInfo = makeNode(ResultRelInfo);
48  resultRelInfo->ri_RangeTableIndex = 0; /* dummy */
49  resultRelInfo->ri_RelationDesc = heapRel;
50  resultRelInfo->ri_TrigDesc = NULL; /* we don't fire triggers */
51 
52  ExecOpenIndices(resultRelInfo, false);
53 
54  return resultRelInfo;
55 }
Relation ri_RelationDesc
Definition: execnodes.h:411
Index ri_RangeTableIndex
Definition: execnodes.h:408
void ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative)
Definition: execIndexing.c:156
TriggerDesc * ri_TrigDesc
Definition: execnodes.h:438
#define makeNode(_type_)
Definition: nodes.h:587

◆ CatalogTupleDelete()

void CatalogTupleDelete ( Relation  heapRel,
ItemPointer  tid 
)

Definition at line 350 of file indexing.c.

References simple_heap_delete().

Referenced by AlterSetting(), ATExecAlterColumnType(), ATExecDropExpression(), changeDependenciesOn(), changeDependencyFor(), CreateComments(), CreateSharedComments(), DeleteAttributeTuples(), DeleteComments(), deleteDependencyRecordsFor(), deleteDependencyRecordsForClass(), deleteDependencyRecordsForSpecific(), DeleteInheritsTuple(), DeleteInitPrivs(), deleteOneObject(), DeleteRelationTuple(), DeleteSecurityLabel(), DeleteSequenceTuple(), DeleteSharedComments(), DeleteSharedSecurityLabel(), DeleteSystemAttributeTuples(), DelRoleMems(), drop_parent_dependency(), DropConfigurationMapping(), dropDatabaseDependencies(), dropdb(), DropObjectById(), DropRole(), DropSetting(), DropSubscription(), DropTableSpace(), EnumValuesDelete(), heap_drop_with_catalog(), index_drop(), IndexSetParentIndex(), inv_truncate(), LargeObjectDrop(), MakeConfigurationMapping(), RangeDelete(), recordExtensionInitPrivWorker(), RelationRemoveInheritance(), RemoveAttrDefaultById(), RemoveAttributeById(), RemoveConstraintById(), RemoveExtensionById(), RemoveFunctionById(), RemoveOperatorById(), RemovePartitionKeyByRelId(), RemovePolicyById(), RemovePublicationRelById(), RemoveRewriteRuleById(), RemoveStatistics(), RemoveStatisticsById(), RemoveSubscriptionRel(), RemoveTriggerById(), RemoveTSConfigurationById(), RemoveTypeById(), replorigin_drop_guts(), SetSecurityLabel(), SetSharedSecurityLabel(), shdepChangeDep(), and shdepDropDependency().

351 {
352  simple_heap_delete(heapRel, tid);
353 }
void simple_heap_delete(Relation relation, ItemPointer tid)
Definition: heapam.c:3107

◆ CatalogTupleInsert()

void CatalogTupleInsert ( Relation  heapRel,
HeapTuple  tup 
)

Definition at line 221 of file indexing.c.

References CatalogCloseIndexes(), CatalogIndexInsert(), CatalogOpenIndexes(), CatalogTupleCheckConstraints, and simple_heap_insert().

Referenced by AddEnumLabel(), AddRoleMems(), AddSubscriptionRelState(), AggregateCreate(), AlterSetting(), CastCreate(), CollationCreate(), ConversionCreate(), CopyStatistics(), CreateAccessMethod(), CreateComments(), CreateConstraintEntry(), createdb(), CreateForeignDataWrapper(), CreateForeignServer(), CreateForeignTable(), CreateOpFamily(), CreatePolicy(), CreateProceduralLanguage(), CreatePublication(), CreateRole(), CreateSharedComments(), CreateStatistics(), CreateSubscription(), CreateTableSpace(), CreateTransform(), CreateTriggerFiringOn(), CreateUserMapping(), DefineOpClass(), DefineSequence(), DefineTSConfiguration(), DefineTSDictionary(), DefineTSParser(), DefineTSTemplate(), EnumValuesCreate(), insert_event_trigger_tuple(), InsertExtensionTuple(), InsertPgClassTuple(), InsertRule(), LargeObjectCreate(), MakeConfigurationMapping(), NamespaceCreate(), OperatorCreate(), OperatorShellMake(), ProcedureCreate(), publication_add_relation(), RangeCreate(), recordExtensionInitPrivWorker(), replorigin_create(), SetDefaultACL(), SetSecurityLabel(), SetSharedSecurityLabel(), shdepAddDependency(), shdepChangeDep(), StoreAttrDefault(), storeOperators(), StorePartitionKey(), storeProcedures(), StoreSingleInheritance(), TypeCreate(), TypeShellMake(), update_attstats(), and UpdateIndexRelation().

222 {
223  CatalogIndexState indstate;
224 
225  CatalogTupleCheckConstraints(heapRel, tup);
226 
227  indstate = CatalogOpenIndexes(heapRel);
228 
229  simple_heap_insert(heapRel, tup);
230 
231  CatalogIndexInsert(indstate, tup);
232  CatalogCloseIndexes(indstate);
233 }
static void CatalogIndexInsert(CatalogIndexState indstate, HeapTuple heapTuple)
Definition: indexing.c:75
void simple_heap_insert(Relation relation, HeapTuple tup)
Definition: heapam.c:2642
CatalogIndexState CatalogOpenIndexes(Relation heapRel)
Definition: indexing.c:43
void CatalogCloseIndexes(CatalogIndexState indstate)
Definition: indexing.c:61
#define CatalogTupleCheckConstraints(heapRel, tup)
Definition: indexing.c:205

◆ CatalogTupleInsertWithInfo()

void CatalogTupleInsertWithInfo ( Relation  heapRel,
HeapTuple  tup,
CatalogIndexState  indstate 
)

Definition at line 244 of file indexing.c.

References CatalogIndexInsert(), CatalogTupleCheckConstraints, and simple_heap_insert().

Referenced by inv_truncate(), and inv_write().

246 {
247  CatalogTupleCheckConstraints(heapRel, tup);
248 
249  simple_heap_insert(heapRel, tup);
250 
251  CatalogIndexInsert(indstate, tup);
252 }
static void CatalogIndexInsert(CatalogIndexState indstate, HeapTuple heapTuple)
Definition: indexing.c:75
void simple_heap_insert(Relation relation, HeapTuple tup)
Definition: heapam.c:2642
#define CatalogTupleCheckConstraints(heapRel, tup)
Definition: indexing.c:205

◆ CatalogTuplesMultiInsertWithInfo()

void CatalogTuplesMultiInsertWithInfo ( Relation  heapRel,
TupleTableSlot **  slot,
int  ntuples,
CatalogIndexState  indstate 
)

Definition at line 261 of file indexing.c.

References CatalogIndexInsert(), ExecFetchSlotHeapTuple(), GetCurrentCommandId(), heap_freetuple(), heap_multi_insert(), i, HeapTupleData::t_tableOid, and TupleTableSlot::tts_tableOid.

Referenced by copyTemplateDependencies(), InsertPgAttributeTuples(), and recordMultipleDependencies().

263 {
264  /* Nothing to do */
265  if (ntuples <= 0)
266  return;
267 
268  heap_multi_insert(heapRel, slot, ntuples,
269  GetCurrentCommandId(true), 0, NULL);
270 
271  /*
272  * There is no equivalent to heap_multi_insert for the catalog indexes, so
273  * we must loop over and insert individually.
274  */
275  for (int i = 0; i < ntuples; i++)
276  {
277  bool should_free;
278  HeapTuple tuple;
279 
280  tuple = ExecFetchSlotHeapTuple(slot[i], true, &should_free);
281  tuple->t_tableOid = slot[i]->tts_tableOid;
282  CatalogIndexInsert(indstate, tuple);
283 
284  if (should_free)
285  heap_freetuple(tuple);
286  }
287 }
Oid tts_tableOid
Definition: tuptable.h:131
static void CatalogIndexInsert(CatalogIndexState indstate, HeapTuple heapTuple)
Definition: indexing.c:75
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
Oid t_tableOid
Definition: htup.h:66
HeapTuple ExecFetchSlotHeapTuple(TupleTableSlot *slot, bool materialize, bool *shouldFree)
Definition: execTuples.c:1644
int i
void heap_multi_insert(Relation relation, TupleTableSlot **slots, int ntuples, CommandId cid, int options, BulkInsertState bistate)
Definition: heapam.c:2302
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:761

◆ CatalogTupleUpdate()

void CatalogTupleUpdate ( Relation  heapRel,
ItemPointer  otid,
HeapTuple  tup 
)

Definition at line 301 of file indexing.c.

References CatalogCloseIndexes(), CatalogIndexInsert(), CatalogOpenIndexes(), CatalogTupleCheckConstraints, and simple_heap_update().

Referenced by AddRoleMems(), AggregateCreate(), AlterCollation(), AlterConstraintNamespaces(), AlterDatabase(), AlterDatabaseOwner(), AlterDomainDefault(), AlterDomainNotNull(), AlterDomainValidateConstraint(), AlterEventTrigger(), AlterEventTriggerOwner_internal(), AlterExtensionNamespace(), AlterForeignDataWrapper(), AlterForeignDataWrapperOwner_internal(), AlterForeignServer(), AlterForeignServerOwner_internal(), AlterFunction(), AlterObjectNamespace_internal(), AlterObjectOwner_internal(), AlterObjectRename_internal(), AlterOperator(), AlterPolicy(), AlterPublicationOptions(), AlterPublicationOwner_internal(), AlterRelationNamespaceInternal(), AlterRole(), AlterSchemaOwner_internal(), AlterSequence(), AlterSetting(), AlterStatistics(), AlterSubscription(), AlterSubscriptionOwner_internal(), AlterTableSpaceOptions(), AlterTSDictionary(), AlterTypeNamespaceInternal(), AlterTypeOwnerInternal(), AlterTypeRecurse(), AlterUserMapping(), ApplyExtensionUpdates(), ATExecAddColumn(), ATExecAddIdentity(), ATExecAddOf(), ATExecAlterColumnGenericOptions(), ATExecAlterColumnType(), ATExecAlterConstrRecurse(), ATExecChangeOwner(), ATExecDropColumn(), ATExecDropConstraint(), ATExecDropExpression(), ATExecDropIdentity(), ATExecDropNotNull(), ATExecDropOf(), ATExecForceNoForceRowSecurity(), ATExecGenericOptions(), ATExecSetCompression(), ATExecSetIdentity(), ATExecSetNotNull(), ATExecSetOptions(), ATExecSetRelOptions(), ATExecSetRowSecurity(), ATExecSetStatistics(), ATExecSetStorage(), ATExecValidateConstraint(), change_owner_fix_column_acls(), changeDependenciesOf(), changeDependenciesOn(), changeDependencyFor(), ConstraintSetParentConstraint(), copy_table_data(), create_toast_table(), CreateComments(), CreateProceduralLanguage(), CreateSharedComments(), CreateTransform(), CreateTriggerFiringOn(), DefineIndex(), DefineQueryRewrite(), DelRoleMems(), DetachPartitionFinalize(), EnableDisableRule(), EnableDisableTrigger(), ExecGrant_Attribute(), ExecGrant_Database(), ExecGrant_Fdw(), ExecGrant_ForeignServer(), ExecGrant_Function(), ExecGrant_Language(), ExecGrant_Largeobject(), ExecGrant_Namespace(), ExecGrant_Relation(), ExecGrant_Tablespace(), ExecGrant_Type(), extension_config_remove(), finish_heap_swap(), index_build(), index_concurrently_swap(), index_constraint_create(), index_set_state_flags(), InsertRule(), MakeConfigurationMapping(), mark_index_clustered(), MarkInheritDetached(), MergeAttributesIntoExisting(), MergeConstraintsIntoExisting(), MergeWithExistingConstraint(), movedb(), OperatorCreate(), OperatorUpd(), pg_extension_config_dump(), ProcedureCreate(), recordExtensionInitPrivWorker(), reindex_index(), relation_mark_replica_identity(), RelationClearMissing(), RelationSetNewRelfilenode(), RemoveAttrDefaultById(), RemoveAttributeById(), RemoveConstraintById(), RemoveInheritance(), RemoveRoleFromObjectPolicy(), rename_policy(), renameatt_internal(), RenameConstraintById(), RenameDatabase(), RenameEnumLabel(), RenameRelationInternal(), RenameRewriteRule(), RenameRole(), RenameSchema(), RenameTableSpace(), renametrig_internal(), RenameTypeInternal(), RenumberEnumType(), SetAttrMissing(), SetDefaultACL(), SetIndexStorageProperties(), SetMatViewPopulatedState(), SetRelationHasSubclass(), SetRelationNumChecks(), SetRelationRuleStatus(), SetRelationTableSpace(), SetSecurityLabel(), SetSharedSecurityLabel(), shdepChangeDep(), statext_store(), StoreAttrDefault(), StorePartitionBound(), TypeCreate(), update_attstats(), update_default_partition_oid(), update_relispartition(), UpdateSubscriptionRelState(), UpdateTwoPhaseState(), and validatePartitionedIndex().

302 {
303  CatalogIndexState indstate;
304 
305  CatalogTupleCheckConstraints(heapRel, tup);
306 
307  indstate = CatalogOpenIndexes(heapRel);
308 
309  simple_heap_update(heapRel, otid, tup);
310 
311  CatalogIndexInsert(indstate, tup);
312  CatalogCloseIndexes(indstate);
313 }
static void CatalogIndexInsert(CatalogIndexState indstate, HeapTuple heapTuple)
Definition: indexing.c:75
CatalogIndexState CatalogOpenIndexes(Relation heapRel)
Definition: indexing.c:43
void simple_heap_update(Relation relation, ItemPointer otid, HeapTuple tup)
Definition: heapam.c:4159
void CatalogCloseIndexes(CatalogIndexState indstate)
Definition: indexing.c:61
#define CatalogTupleCheckConstraints(heapRel, tup)
Definition: indexing.c:205

◆ CatalogTupleUpdateWithInfo()

void CatalogTupleUpdateWithInfo ( Relation  heapRel,
ItemPointer  otid,
HeapTuple  tup,
CatalogIndexState  indstate 
)

Definition at line 324 of file indexing.c.

References CatalogIndexInsert(), CatalogTupleCheckConstraints, and simple_heap_update().

Referenced by inv_truncate(), inv_write(), and swap_relation_files().

326 {
327  CatalogTupleCheckConstraints(heapRel, tup);
328 
329  simple_heap_update(heapRel, otid, tup);
330 
331  CatalogIndexInsert(indstate, tup);
332 }
static void CatalogIndexInsert(CatalogIndexState indstate, HeapTuple heapTuple)
Definition: indexing.c:75
void simple_heap_update(Relation relation, ItemPointer otid, HeapTuple tup)
Definition: heapam.c:4159
#define CatalogTupleCheckConstraints(heapRel, tup)
Definition: indexing.c:205