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:1057
void ExecCloseIndices(ResultRelInfo *resultRelInfo)
Definition: execIndexing.c:240

◆ 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:2755
int ri_NumIndices
Definition: execnodes.h:415
#define NIL
Definition: pg_list.h:65
Relation ri_RelationDesc
Definition: execnodes.h:412
List * ii_Predicate
Definition: execnodes.h:162
#define RelationGetDescr(relation)
Definition: rel.h:483
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1208
Definition: type.h:89
Form_pg_index rd_index
Definition: rel.h:175
int ii_NumIndexKeyAttrs
Definition: execnodes.h:158
ItemPointerData t_self
Definition: htup.h:65
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1224
bool ii_ReadyForInserts
Definition: execnodes.h:172
uintptr_t Datum
Definition: postgres.h:367
List * ii_Expressions
Definition: execnodes.h:160
#define HeapTupleIsHeapOnly(tuple)
Definition: htup_details.h:685
#define Assert(condition)
Definition: c.h:792
#define INDEX_MAX_KEYS
static Datum values[MAXATTR]
Definition: bootstrap.c:165
Oid * ii_ExclusionOps
Definition: execnodes.h:164
IndexInfo ** ri_IndexRelationInfo
Definition: execnodes.h:421
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:4020
#define RelationGetRelid(relation)
Definition: rel.h:457
RelationPtr ri_IndexRelationDescs
Definition: execnodes.h:418
TupleTableSlot * ExecStoreHeapTuple(HeapTuple tuple, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1322

◆ 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:412
Index ri_RangeTableIndex
Definition: execnodes.h:409
void ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative)
Definition: execIndexing.c:165
TriggerDesc * ri_TrigDesc
Definition: execnodes.h:424
#define makeNode(_type_)
Definition: nodes.h:576

◆ CatalogTupleDelete()

void CatalogTupleDelete ( Relation  heapRel,
ItemPointer  tid 
)

Definition at line 351 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(), SetSecurityLabel(), SetSharedSecurityLabel(), shdepChangeDep(), and shdepDropDependency().

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

◆ CatalogTupleInsert()

void CatalogTupleInsert ( Relation  heapRel,
HeapTuple  tup 
)

Definition at line 222 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(), CreateTrigger(), CreateUserMapping(), DefineOpClass(), DefineSequence(), DefineTSConfiguration(), DefineTSDictionary(), DefineTSParser(), DefineTSTemplate(), EnumValuesCreate(), IndexSetParentIndex(), 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().

223 {
224  CatalogIndexState indstate;
225 
226  CatalogTupleCheckConstraints(heapRel, tup);
227 
228  indstate = CatalogOpenIndexes(heapRel);
229 
230  simple_heap_insert(heapRel, tup);
231 
232  CatalogIndexInsert(indstate, tup);
233  CatalogCloseIndexes(indstate);
234 }
static void CatalogIndexInsert(CatalogIndexState indstate, HeapTuple heapTuple)
Definition: indexing.c:75
void simple_heap_insert(Relation relation, HeapTuple tup)
Definition: heapam.c:2455
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 245 of file indexing.c.

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

Referenced by inv_truncate(), and inv_write().

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

◆ CatalogTuplesMultiInsertWithInfo()

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

Definition at line 262 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().

264 {
265  /* Nothing to do */
266  if (ntuples <= 0)
267  return;
268 
269  heap_multi_insert(heapRel, slot, ntuples,
270  GetCurrentCommandId(true), 0, NULL);
271 
272  /*
273  * There is no equivalent to heap_multi_insert for the catalog indexes, so
274  * we must loop over and insert individually.
275  */
276  for (int i = 0; i < ntuples; i++)
277  {
278  bool should_free;
279  HeapTuple tuple;
280 
281  tuple = ExecFetchSlotHeapTuple(slot[i], true, &should_free);
282  tuple->t_tableOid = slot[i]->tts_tableOid;
283  CatalogIndexInsert(indstate, tuple);
284 
285  if (should_free)
286  heap_freetuple(tuple);
287  }
288 }
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:1614
int i
void heap_multi_insert(Relation relation, TupleTableSlot **slots, int ntuples, CommandId cid, int options, BulkInsertState bistate)
Definition: heapam.c:2115
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:761

◆ CatalogTupleUpdate()

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

Definition at line 302 of file indexing.c.

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

Referenced by AddRoleMems(), AggregateCreate(), 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(), ATExecAlterConstraint(), ATExecChangeOwner(), ATExecDetachPartition(), ATExecDisableRowSecurity(), ATExecDropColumn(), ATExecDropConstraint(), ATExecDropExpression(), ATExecDropIdentity(), ATExecDropNotNull(), ATExecDropOf(), ATExecEnableRowSecurity(), ATExecForceNoForceRowSecurity(), ATExecGenericOptions(), ATExecSetIdentity(), ATExecSetNotNull(), ATExecSetOptions(), ATExecSetRelOptions(), ATExecSetStatistics(), ATExecSetStorage(), ATExecSetTableSpace(), ATExecSetTableSpaceNoStorage(), ATExecValidateConstraint(), change_owner_fix_column_acls(), changeDependenciesOf(), changeDependenciesOn(), changeDependencyFor(), ConstraintSetParentConstraint(), copy_table_data(), create_toast_table(), CreateComments(), CreateProceduralLanguage(), CreateSharedComments(), CreateTransform(), CreateTrigger(), DefineIndex(), DefineQueryRewrite(), DelRoleMems(), 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(), 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(), RenameTypeInternal(), RenumberEnumType(), SetAttrMissing(), SetDefaultACL(), SetMatViewPopulatedState(), SetRelationHasSubclass(), SetRelationNumChecks(), SetRelationRuleStatus(), SetSecurityLabel(), SetSharedSecurityLabel(), shdepChangeDep(), statext_store(), StoreAttrDefault(), StorePartitionBound(), TypeCreate(), update_attstats(), update_default_partition_oid(), update_relispartition(), UpdateStatisticsForTypeChange(), UpdateSubscriptionRelState(), validatePartitionedIndex(), and visitDependenciesOf().

303 {
304  CatalogIndexState indstate;
305 
306  CatalogTupleCheckConstraints(heapRel, tup);
307 
308  indstate = CatalogOpenIndexes(heapRel);
309 
310  simple_heap_update(heapRel, otid, tup);
311 
312  CatalogIndexInsert(indstate, tup);
313  CatalogCloseIndexes(indstate);
314 }
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:3956
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 325 of file indexing.c.

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

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

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