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, TU_UpdateIndexes updateIndexes)
 
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)

Definition at line 217 of file indexing.c.

Function Documentation

◆ CatalogCloseIndexes()

◆ CatalogIndexInsert()

static void CatalogIndexInsert ( CatalogIndexState  indstate,
HeapTuple  heapTuple,
TU_UpdateIndexes  updateIndexes 
)
static

Definition at line 75 of file indexing.c.

77 {
78  int i;
79  int numIndexes;
80  RelationPtr relationDescs;
81  Relation heapRelation;
82  TupleTableSlot *slot;
83  IndexInfo **indexInfoArray;
85  bool isnull[INDEX_MAX_KEYS];
86  bool onlySummarized = (updateIndexes == TU_Summarizing);
87 
88  /*
89  * HOT update does not require index inserts. But with asserts enabled we
90  * want to check that it'd be legal to currently insert into the
91  * table/index.
92  */
93 #ifndef USE_ASSERT_CHECKING
94  if (HeapTupleIsHeapOnly(heapTuple) && !onlySummarized)
95  return;
96 #endif
97 
98  /* When only updating summarized indexes, the tuple has to be HOT. */
99  Assert((!onlySummarized) || HeapTupleIsHeapOnly(heapTuple));
100 
101  /*
102  * Get information from the state structure. Fall out if nothing to do.
103  */
104  numIndexes = indstate->ri_NumIndices;
105  if (numIndexes == 0)
106  return;
107  relationDescs = indstate->ri_IndexRelationDescs;
108  indexInfoArray = indstate->ri_IndexRelationInfo;
109  heapRelation = indstate->ri_RelationDesc;
110 
111  /* Need a slot to hold the tuple being examined */
112  slot = MakeSingleTupleTableSlot(RelationGetDescr(heapRelation),
113  &TTSOpsHeapTuple);
114  ExecStoreHeapTuple(heapTuple, slot, false);
115 
116  /*
117  * for each index, form and insert the index tuple
118  */
119  for (i = 0; i < numIndexes; i++)
120  {
121  IndexInfo *indexInfo;
122  Relation index;
123 
124  indexInfo = indexInfoArray[i];
125  index = relationDescs[i];
126 
127  /* If the index is marked as read-only, ignore it */
128  if (!indexInfo->ii_ReadyForInserts)
129  continue;
130 
131  /*
132  * Expressional and partial indexes on system catalogs are not
133  * supported, nor exclusion constraints, nor deferred uniqueness
134  */
135  Assert(indexInfo->ii_Expressions == NIL);
136  Assert(indexInfo->ii_Predicate == NIL);
137  Assert(indexInfo->ii_ExclusionOps == NULL);
138  Assert(index->rd_index->indimmediate);
139  Assert(indexInfo->ii_NumIndexKeyAttrs != 0);
140 
141  /* see earlier check above */
142 #ifdef USE_ASSERT_CHECKING
143  if (HeapTupleIsHeapOnly(heapTuple) && !onlySummarized)
144  {
146  continue;
147  }
148 #endif /* USE_ASSERT_CHECKING */
149 
150  /*
151  * Skip insertions into non-summarizing indexes if we only need to
152  * update summarizing indexes.
153  */
154  if (onlySummarized && !indexInfo->ii_Summarizing)
155  continue;
156 
157  /*
158  * FormIndexDatum fills in its values and isnull parameters with the
159  * appropriate values for the column(s) of the index.
160  */
161  FormIndexDatum(indexInfo,
162  slot,
163  NULL, /* no expression eval to do */
164  values,
165  isnull);
166 
167  /*
168  * The index AM does the rest.
169  */
170  index_insert(index, /* index relation */
171  values, /* array of index Datums */
172  isnull, /* is-null flags */
173  &(heapTuple->t_self), /* tid of heap tuple */
174  heapRelation,
175  index->rd_index->indisunique ?
177  false,
178  indexInfo);
179  }
180 
182 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1253
TupleTableSlot * ExecStoreHeapTuple(HeapTuple tuple, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1351
const TupleTableSlotOps TTSOpsHeapTuple
Definition: execTuples.c:84
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1237
@ UNIQUE_CHECK_NO
Definition: genam.h:117
@ UNIQUE_CHECK_YES
Definition: genam.h:118
#define HeapTupleIsHeapOnly(tuple)
Definition: htup_details.h:683
bool ReindexIsProcessingIndex(Oid indexOid)
Definition: index.c:4097
void FormIndexDatum(IndexInfo *indexInfo, TupleTableSlot *slot, EState *estate, Datum *values, bool *isnull)
Definition: index.c:2736
bool index_insert(Relation indexRelation, Datum *values, bool *isnull, ItemPointer heap_t_ctid, Relation heapRelation, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
Definition: indexam.c:213
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
#define INDEX_MAX_KEYS
#define NIL
Definition: pg_list.h:68
uintptr_t Datum
Definition: postgres.h:64
#define RelationGetRelid(relation)
Definition: rel.h:504
#define RelationGetDescr(relation)
Definition: rel.h:530
ItemPointerData t_self
Definition: htup.h:65
Oid * ii_ExclusionOps
Definition: execnodes.h:191
int ii_NumIndexKeyAttrs
Definition: execnodes.h:185
List * ii_Expressions
Definition: execnodes.h:187
bool ii_Summarizing
Definition: execnodes.h:204
bool ii_ReadyForInserts
Definition: execnodes.h:199
List * ii_Predicate
Definition: execnodes.h:189
int ri_NumIndices
Definition: execnodes.h:459
Relation ri_RelationDesc
Definition: execnodes.h:456
RelationPtr ri_IndexRelationDescs
Definition: execnodes.h:462
IndexInfo ** ri_IndexRelationInfo
Definition: execnodes.h:465
Definition: type.h:95
@ TU_Summarizing
Definition: tableam.h:118

References Assert(), ExecDropSingleTupleTableSlot(), ExecStoreHeapTuple(), FormIndexDatum(), HeapTupleIsHeapOnly, i, IndexInfo::ii_ExclusionOps, IndexInfo::ii_Expressions, IndexInfo::ii_NumIndexKeyAttrs, IndexInfo::ii_Predicate, IndexInfo::ii_ReadyForInserts, IndexInfo::ii_Summarizing, index_insert(), INDEX_MAX_KEYS, MakeSingleTupleTableSlot(), NIL, ReindexIsProcessingIndex(), RelationGetDescr, RelationGetRelid, ResultRelInfo::ri_IndexRelationDescs, ResultRelInfo::ri_IndexRelationInfo, ResultRelInfo::ri_NumIndices, ResultRelInfo::ri_RelationDesc, HeapTupleData::t_self, TTSOpsHeapTuple, TU_Summarizing, UNIQUE_CHECK_NO, UNIQUE_CHECK_YES, and values.

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

◆ CatalogOpenIndexes()

CatalogIndexState CatalogOpenIndexes ( Relation  heapRel)

Definition at line 43 of file indexing.c.

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 }
void ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative)
Definition: execIndexing.c:156
#define makeNode(_type_)
Definition: nodes.h:155
TriggerDesc * ri_TrigDesc
Definition: execnodes.h:486
Index ri_RangeTableIndex
Definition: execnodes.h:453

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

Referenced by AddNewAttributeTuples(), AppendAttributeTuples(), CatalogTupleInsert(), CatalogTupleUpdate(), CopyStatistics(), copyTemplateDependencies(), DefineTSConfiguration(), EnumValuesCreate(), InsertPgAttributeTuples(), inv_truncate(), inv_write(), MakeConfigurationMapping(), recordMultipleDependencies(), swap_relation_files(), and update_attstats().

◆ CatalogTupleDelete()

void CatalogTupleDelete ( Relation  heapRel,
ItemPointer  tid 
)

Definition at line 365 of file indexing.c.

366 {
367  simple_heap_delete(heapRel, tid);
368 }
void simple_heap_delete(Relation relation, ItemPointer tid)
Definition: heapam.c:2938

References simple_heap_delete().

Referenced by AlterSetting(), ATExecAlterColumnType(), 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(), ExecGrant_Parameter(), heap_drop_with_catalog(), index_drop(), IndexSetParentIndex(), inv_truncate(), LargeObjectDrop(), MakeConfigurationMapping(), RangeDelete(), recordExtensionInitPrivWorker(), RelationRemoveInheritance(), RemoveAttrDefaultById(), RemoveConstraintById(), RemoveExtensionById(), RemoveFunctionById(), RemoveOperatorById(), RemovePartitionKeyByRelId(), RemovePolicyById(), RemovePublicationById(), RemovePublicationRelById(), RemovePublicationSchemaById(), RemoveRewriteRuleById(), RemoveStatistics(), RemoveStatisticsById(), RemoveStatisticsDataById(), RemoveSubscriptionRel(), RemoveTriggerById(), RemoveTSConfigurationById(), RemoveTypeById(), replorigin_drop_by_name(), SetSecurityLabel(), SetSharedSecurityLabel(), shdepChangeDep(), and shdepDropDependency().

◆ CatalogTupleInsert()

void CatalogTupleInsert ( Relation  heapRel,
HeapTuple  tup 
)

Definition at line 233 of file indexing.c.

234 {
235  CatalogIndexState indstate;
236 
237  CatalogTupleCheckConstraints(heapRel, tup);
238 
239  indstate = CatalogOpenIndexes(heapRel);
240 
241  simple_heap_insert(heapRel, tup);
242 
243  CatalogIndexInsert(indstate, tup, TU_All);
244  CatalogCloseIndexes(indstate);
245 }
void simple_heap_insert(Relation relation, HeapTuple tup)
Definition: heapam.c:2459
static void CatalogIndexInsert(CatalogIndexState indstate, HeapTuple heapTuple, TU_UpdateIndexes updateIndexes)
Definition: indexing.c:75
#define CatalogTupleCheckConstraints(heapRel, tup)
Definition: indexing.c:217
void CatalogCloseIndexes(CatalogIndexState indstate)
Definition: indexing.c:61
CatalogIndexState CatalogOpenIndexes(Relation heapRel)
Definition: indexing.c:43
@ TU_All
Definition: tableam.h:115

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

Referenced by AddEnumLabel(), AddRoleMems(), AddSubscriptionRelState(), AggregateCreate(), AlterSetting(), CastCreate(), CollationCreate(), ConversionCreate(), 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(), insert_event_trigger_tuple(), InsertExtensionTuple(), InsertPgClassTuple(), InsertRule(), LargeObjectCreate(), NamespaceCreate(), OperatorCreate(), OperatorShellMake(), ParameterAclCreate(), ProcedureCreate(), publication_add_relation(), publication_add_schema(), RangeCreate(), recordExtensionInitPrivWorker(), replorigin_create(), SetDefaultACL(), SetSecurityLabel(), SetSharedSecurityLabel(), shdepAddDependency(), shdepChangeDep(), statext_store(), StoreAttrDefault(), storeOperators(), StorePartitionKey(), storeProcedures(), StoreSingleInheritance(), TypeCreate(), TypeShellMake(), and UpdateIndexRelation().

◆ CatalogTupleInsertWithInfo()

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

Definition at line 256 of file indexing.c.

258 {
259  CatalogTupleCheckConstraints(heapRel, tup);
260 
261  simple_heap_insert(heapRel, tup);
262 
263  CatalogIndexInsert(indstate, tup, TU_All);
264 }

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

Referenced by CopyStatistics(), inv_truncate(), inv_write(), and update_attstats().

◆ CatalogTuplesMultiInsertWithInfo()

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

Definition at line 273 of file indexing.c.

275 {
276  /* Nothing to do */
277  if (ntuples <= 0)
278  return;
279 
280  heap_multi_insert(heapRel, slot, ntuples,
281  GetCurrentCommandId(true), 0, NULL);
282 
283  /*
284  * There is no equivalent to heap_multi_insert for the catalog indexes, so
285  * we must loop over and insert individually.
286  */
287  for (int i = 0; i < ntuples; i++)
288  {
289  bool should_free;
290  HeapTuple tuple;
291 
292  tuple = ExecFetchSlotHeapTuple(slot[i], true, &should_free);
293  tuple->t_tableOid = slot[i]->tts_tableOid;
294  CatalogIndexInsert(indstate, tuple, TU_All);
295 
296  if (should_free)
297  heap_freetuple(tuple);
298  }
299 }
HeapTuple ExecFetchSlotHeapTuple(TupleTableSlot *slot, bool materialize, bool *shouldFree)
Definition: execTuples.c:1643
void heap_multi_insert(Relation relation, TupleTableSlot **slots, int ntuples, CommandId cid, int options, BulkInsertState bistate)
Definition: heapam.c:2097
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1435
Oid t_tableOid
Definition: htup.h:66
Oid tts_tableOid
Definition: tuptable.h:130
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:818

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

Referenced by copyTemplateDependencies(), DefineTSConfiguration(), EnumValuesCreate(), InsertPgAttributeTuples(), MakeConfigurationMapping(), and recordMultipleDependencies().

◆ CatalogTupleUpdate()

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

Definition at line 313 of file indexing.c.

314 {
315  CatalogIndexState indstate;
316  TU_UpdateIndexes updateIndexes = TU_All;
317 
318  CatalogTupleCheckConstraints(heapRel, tup);
319 
320  indstate = CatalogOpenIndexes(heapRel);
321 
322  simple_heap_update(heapRel, otid, tup, &updateIndexes);
323 
324  CatalogIndexInsert(indstate, tup, updateIndexes);
325  CatalogCloseIndexes(indstate);
326 }
void simple_heap_update(Relation relation, ItemPointer otid, HeapTuple tup, TU_UpdateIndexes *update_indexes)
Definition: heapam.c:4055
TU_UpdateIndexes
Definition: tableam.h:110

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

Referenced by AddRoleMems(), AdjustNotNullInheritance(), AdjustNotNullInheritance1(), AggregateCreate(), AlterCollation(), AlterConstraintNamespaces(), AlterDatabase(), AlterDatabaseOwner(), AlterDatabaseRefreshColl(), 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(), ATExecDropExpression(), ATExecDropIdentity(), ATExecDropOf(), ATExecForceNoForceRowSecurity(), ATExecGenericOptions(), ATExecSetCompression(), ATExecSetIdentity(), ATExecSetNotNull(), ATExecSetOptions(), ATExecSetRelOptions(), ATExecSetRowSecurity(), ATExecSetStatistics(), ATExecSetStorage(), ATExecValidateConstraint(), change_owner_fix_column_acls(), changeDependenciesOf(), changeDependenciesOn(), changeDependencyFor(), clear_subscription_skip_lsn(), ConstraintSetParentConstraint(), copy_table_data(), create_toast_table(), CreateComments(), CreateProceduralLanguage(), CreateSharedComments(), CreateTransform(), CreateTriggerFiringOn(), DefineIndex(), DelRoleMems(), DetachPartitionFinalize(), DisableSubscription(), dropconstraint_internal(), EnableDisableRule(), EnableDisableTrigger(), ExecGrant_Attribute(), ExecGrant_common(), ExecGrant_Largeobject(), ExecGrant_Parameter(), ExecGrant_Relation(), extension_config_remove(), finish_heap_swap(), index_build(), index_concurrently_swap(), index_constraint_create(), index_set_state_flags(), InsertRule(), mark_index_clustered(), MarkInheritDetached(), MergeAttributesIntoExisting(), MergeConstraintsIntoExisting(), MergeWithExistingConstraint(), movedb(), OperatorCreate(), OperatorUpd(), pg_extension_config_dump(), ProcedureCreate(), recordExtensionInitPrivWorker(), reindex_index(), relation_mark_replica_identity(), RelationClearMissing(), RelationSetNewRelfilenumber(), RemoveAttrDefaultById(), RemoveAttributeById(), RemoveConstraintById(), RemoveInheritance(), RemoveRoleFromObjectPolicy(), rename_policy(), renameatt_internal(), RenameConstraintById(), RenameDatabase(), RenameEnumLabel(), RenameRelationInternal(), RenameRewriteRule(), RenameRole(), RenameSchema(), RenameTableSpace(), renametrig_internal(), RenameTypeInternal(), RenumberEnumType(), ResetRelRewrite(), set_attnotnull(), SetAttrMissing(), SetDatatabaseHasLoginEventTriggers(), SetDefaultACL(), SetIndexStorageProperties(), SetMatViewPopulatedState(), SetRelationHasSubclass(), SetRelationNumChecks(), SetRelationRuleStatus(), SetRelationTableSpace(), SetSecurityLabel(), SetSharedSecurityLabel(), shdepChangeDep(), StoreAttrDefault(), StorePartitionBound(), TriggerSetParentTrigger(), TypeCreate(), update_default_partition_oid(), update_relispartition(), UpdateSubscriptionRelState(), UpdateTwoPhaseState(), and validatePartitionedIndex().

◆ CatalogTupleUpdateWithInfo()

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

Definition at line 337 of file indexing.c.

339 {
340  TU_UpdateIndexes updateIndexes = TU_All;
341 
342  CatalogTupleCheckConstraints(heapRel, tup);
343 
344  simple_heap_update(heapRel, otid, tup, &updateIndexes);
345 
346  CatalogIndexInsert(indstate, tup, updateIndexes);
347 }

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

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