1
/*-------------------------------------------------------------------------
4
* POSTGRES define and remove index code.
6
* Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
7
* Portions Copyright (c) 1994, Regents of the University of California
11
* src/backend/commands/indexcmds.c
13
*-------------------------------------------------------------------------
18
#include "access/amapi.h"
19
#include "access/htup_details.h"
20
#include "access/reloptions.h"
21
#include "access/sysattr.h"
22
#include "access/xact.h"
23
#include "catalog/catalog.h"
24
#include "catalog/index.h"
25
#include "catalog/indexing.h"
26
#include "catalog/partition.h"
27
#include "catalog/pg_am.h"
28
#include "catalog/pg_constraint.h"
29
#include "catalog/pg_inherits.h"
30
#include "catalog/pg_opclass.h"
31
#include "catalog/pg_opfamily.h"
32
#include "catalog/pg_tablespace.h"
33
#include "catalog/pg_type.h"
34
#include "commands/comment.h"
35
#include "commands/dbcommands.h"
36
#include "commands/defrem.h"
37
#include "commands/tablecmds.h"
38
#include "commands/tablespace.h"
39
#include "mb/pg_wchar.h"
40
#include "miscadmin.h"
41
#include "nodes/makefuncs.h"
42
#include "nodes/nodeFuncs.h"
43
#include "optimizer/clauses.h"
44
#include "optimizer/planner.h"
45
#include "optimizer/var.h"
46
#include "parser/parse_coerce.h"
47
#include "parser/parse_func.h"
48
#include "parser/parse_oper.h"
49
#include "rewrite/rewriteManip.h"
50
#include "storage/lmgr.h"
51
#include "storage/proc.h"
52
#include "storage/procarray.h"
53
#include "utils/acl.h"
54
#include "utils/builtins.h"
55
#include "utils/fmgroids.h"
56
#include "utils/inval.h"
57
#include "utils/lsyscache.h"
58
#include "utils/memutils.h"
59
#include "utils/partcache.h"
60
#include "utils/regproc.h"
61
#include "utils/snapmgr.h"
62
#include "utils/syscache.h"
63
#include "utils/tqual.h"
66
/* non-export function prototypes */
67
static void CheckPredicate(Expr *predicate);
68
static void ComputeIndexAttrs(IndexInfo *indexInfo,
74
List *exclusionOpNames,
76
const char *accessMethodName, Oid accessMethodId,
79
static char *ChooseIndexName(const char *tabname, Oid namespaceId,
80
List *colnames, List *exclusionOpNames,
81
bool primary, bool isconstraint);
82
static char *ChooseIndexNameAddition(List *colnames);
83
static List *ChooseIndexColumnNames(List *indexElems);
84
static void RangeVarCallbackForReindexIndex(const RangeVar *relation,
85
Oid relId, Oid oldRelId, void *arg);
86
static void ReindexPartitionedIndex(Relation parentIdx);
89
* CheckIndexCompatible
90
* Determine whether an existing index definition is compatible with a
91
* prospective index definition, such that the existing index storage
92
* could become the storage of the new index, avoiding a rebuild.
94
* 'heapRelation': the relation the index would apply to.
95
* 'accessMethodName': name of the AM to use.
96
* 'attributeList': a list of IndexElem specifying columns and expressions
98
* 'exclusionOpNames': list of names of exclusion-constraint operators,
99
* or NIL if not an exclusion constraint.
101
* This is tailored to the needs of ALTER TABLE ALTER TYPE, which recreates
102
* any indexes that depended on a changing column from their pg_get_indexdef
103
* or pg_get_constraintdef definitions. We omit some of the sanity checks of
104
* DefineIndex. We assume that the old and new indexes have the same number
105
* of columns and that if one has an expression column or predicate, both do.
106
* Errors arising from the attribute list still apply.
108
* Most column type changes that can skip a table rewrite do not invalidate
109
* indexes. We acknowledge this when all operator classes, collations and
110
* exclusion operators match. Though we could further permit intra-opfamily
111
* changes for btree and hash indexes, that adds subtle complexity with no
112
* concrete benefit for core types. Note, that INCLUDE columns aren't
113
* checked by this function, for them it's enough that table rewrite is
116
* When a comparison or exclusion operator has a polymorphic input type, the
117
* actual input types must also match. This defends against the possibility
118
* that operators could vary behavior in response to get_fn_expr_argtype().
119
* At present, this hazard is theoretical: check_exclusion_constraint() and
120
* all core index access methods decline to set fn_expr for such calls.
122
* We do not yet implement a test to verify compatibility of expression
123
* columns or predicates, so assume any such index is incompatible.
126
CheckIndexCompatible(Oid oldId,
127
const char *accessMethodName,
129
List *exclusionOpNames)
133
Oid *collationObjectId;
138
Form_pg_index indexForm;
139
Form_pg_am accessMethodForm;
140
IndexAmRoutine *amRoutine;
143
IndexInfo *indexInfo;
144
int numberOfAttributes;
148
oidvector *old_indclass;
149
oidvector *old_indcollation;
154
/* Caller should already have the relation locked in some way. */
155
relationId = IndexGetRelation(oldId, false);
158
* We can pretend isconstraint = false unconditionally. It only serves to
159
* decide the text of an error message that should never happen for us.
161
isconstraint = false;
163
numberOfAttributes = list_length(attributeList);
164
Assert(numberOfAttributes > 0);
165
Assert(numberOfAttributes <= INDEX_MAX_KEYS);
167
/* look up the access method */
168
tuple = SearchSysCache1(AMNAME, PointerGetDatum(accessMethodName));
169
if (!HeapTupleIsValid(tuple))
171
(errcode(ERRCODE_UNDEFINED_OBJECT),
172
errmsg("access method \"%s\" does not exist",
174
accessMethodId = HeapTupleGetOid(tuple);
175
accessMethodForm = (Form_pg_am) GETSTRUCT(tuple);
176
amRoutine = GetIndexAmRoutine(accessMethodForm->amhandler);
177
ReleaseSysCache(tuple);
179
amcanorder = amRoutine->amcanorder;
182
* Compute the operator classes, collations, and exclusion operators for
183
* the new index, so we can test whether it's compatible with the existing
184
* one. Note that ComputeIndexAttrs might fail here, but that's OK:
185
* DefineIndex would have called this function with the same arguments
186
* later on, and it would have failed then anyway. Our attributeList
187
* contains only key attributes, thus we're filling ii_NumIndexAttrs and
188
* ii_NumIndexKeyAttrs with same value.
190
indexInfo = makeNode(IndexInfo);
191
indexInfo->ii_NumIndexAttrs = numberOfAttributes;
192
indexInfo->ii_NumIndexKeyAttrs = numberOfAttributes;
193
indexInfo->ii_Expressions = NIL;
194
indexInfo->ii_ExpressionsState = NIL;
195
indexInfo->ii_PredicateState = NULL;
196
indexInfo->ii_ExclusionOps = NULL;
197
indexInfo->ii_ExclusionProcs = NULL;
198
indexInfo->ii_ExclusionStrats = NULL;
199
indexInfo->ii_Am = accessMethodId;
200
indexInfo->ii_AmCache = NULL;
201
indexInfo->ii_Context = CurrentMemoryContext;
202
typeObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
203
collationObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
204
classObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
205
coloptions = (int16 *) palloc(numberOfAttributes * sizeof(int16));
206
ComputeIndexAttrs(indexInfo,
207
typeObjectId, collationObjectId, classObjectId,
208
coloptions, attributeList,
209
exclusionOpNames, relationId,
210
accessMethodName, accessMethodId,
211
amcanorder, isconstraint);
214
/* Get the soon-obsolete pg_index tuple. */
215
tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(oldId));
216
if (!HeapTupleIsValid(tuple))
217
elog(ERROR, "cache lookup failed for index %u", oldId);
218
indexForm = (Form_pg_index) GETSTRUCT(tuple);
221
* We don't assess expressions or predicates; assume incompatibility.
222
* Also, if the index is invalid for any reason, treat it as incompatible.
224
if (!(heap_attisnull(tuple, Anum_pg_index_indpred, NULL) &&
225
heap_attisnull(tuple, Anum_pg_index_indexprs, NULL) &&
226
IndexIsValid(indexForm)))
228
ReleaseSysCache(tuple);
232
/* Any change in operator class or collation breaks compatibility. */
233
old_natts = indexForm->indnkeyatts;
234
Assert(old_natts == numberOfAttributes);
236
d = SysCacheGetAttr(INDEXRELID, tuple, Anum_pg_index_indcollation, &isnull);
238
old_indcollation = (oidvector *) DatumGetPointer(d);
240
d = SysCacheGetAttr(INDEXRELID, tuple, Anum_pg_index_indclass, &isnull);
242
old_indclass = (oidvector *) DatumGetPointer(d);
244
ret = (memcmp(old_indclass->values, classObjectId,
245
old_natts * sizeof(Oid)) == 0 &&
246
memcmp(old_indcollation->values, collationObjectId,
247
old_natts * sizeof(Oid)) == 0);
249
ReleaseSysCache(tuple);
254
/* For polymorphic opcintype, column type changes break compatibility. */
255
irel = index_open(oldId, AccessShareLock); /* caller probably has a lock */
256
for (i = 0; i < old_natts; i++)
258
if (IsPolymorphicType(get_opclass_input_type(classObjectId[i])) &&
259
TupleDescAttr(irel->rd_att, i)->atttypid != typeObjectId[i])
266
/* Any change in exclusion operator selections breaks compatibility. */
267
if (ret && indexInfo->ii_ExclusionOps != NULL)
273
RelationGetExclusionInfo(irel, &old_operators, &old_procs, &old_strats);
274
ret = memcmp(old_operators, indexInfo->ii_ExclusionOps,
275
old_natts * sizeof(Oid)) == 0;
277
/* Require an exact input type match for polymorphic operators. */
280
for (i = 0; i < old_natts && ret; i++)
285
op_input_types(indexInfo->ii_ExclusionOps[i], &left, &right);
286
if ((IsPolymorphicType(left) || IsPolymorphicType(right)) &&
287
TupleDescAttr(irel->rd_att, i)->atttypid != typeObjectId[i])
296
index_close(irel, NoLock);
302
* Creates a new index.
304
* 'relationId': the OID of the heap relation on which the index is to be
306
* 'stmt': IndexStmt describing the properties of the new index.
307
* 'indexRelationId': normally InvalidOid, but during bootstrap can be
308
* nonzero to specify a preselected OID for the index.
309
* 'parentIndexId': the OID of the parent index; InvalidOid if not the child
310
* of a partitioned index.
311
* 'parentConstraintId': the OID of the parent constraint; InvalidOid if not
312
* the child of a constraint (only used when recursing)
313
* 'is_alter_table': this is due to an ALTER rather than a CREATE operation.
314
* 'check_rights': check for CREATE rights in namespace and tablespace. (This
315
* should be true except when ALTER is deleting/recreating an index.)
316
* 'check_not_in_use': check for table not already in use in current session.
317
* This should be true unless caller is holding the table open, in which
318
* case the caller had better have checked it earlier.
319
* 'skip_build': make the catalog entries but don't create the index files
320
* 'quiet': suppress the NOTICE chatter ordinarily provided for constraints.
322
* Returns the object address of the created index.
325
DefineIndex(Oid relationId,
329
Oid parentConstraintId,
332
bool check_not_in_use,
336
char *indexRelationName;
337
char *accessMethodName;
339
Oid *collationObjectId;
344
Oid createdConstraintId = InvalidOid;
346
List *allIndexParams;
348
Relation indexRelation;
350
Form_pg_am accessMethodForm;
351
IndexAmRoutine *amRoutine;
353
amoptions_function amoptions;
357
IndexInfo *indexInfo;
360
int numberOfAttributes;
361
int numberOfKeyAttributes;
362
TransactionId limitXmin;
363
VirtualTransactionId *old_snapshots;
364
ObjectAddress address;
372
if (list_intersection(stmt->indexParams, stmt->indexIncludingParams) != NIL)
374
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
375
errmsg("included columns must not intersect with key columns")));
378
* count key attributes in index
380
numberOfKeyAttributes = list_length(stmt->indexParams);
383
* Calculate the new list of index columns including both key columns and
384
* INCLUDE columns. Later we can determine which of these are key
385
* columns, and which are just part of the INCLUDE list by checking the
386
* list position. A list item in a position less than ii_NumIndexKeyAttrs
387
* is part of the key columns, and anything equal to and over is part of
388
* the INCLUDE columns.
390
allIndexParams = list_concat(list_copy(stmt->indexParams),
391
list_copy(stmt->indexIncludingParams));
392
numberOfAttributes = list_length(allIndexParams);
394
if (numberOfAttributes <= 0)
396
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
397
errmsg("must specify at least one column")));
398
if (numberOfAttributes > INDEX_MAX_KEYS)
400
(errcode(ERRCODE_TOO_MANY_COLUMNS),
401
errmsg("cannot use more than %d columns in an index",
405
* Only SELECT ... FOR UPDATE/SHARE are allowed while doing a standard
406
* index build; but for concurrent builds we allow INSERT/UPDATE/DELETE
409
* NB: Caller is responsible for making sure that relationId refers to the
410
* relation on which the index should be built; except in bootstrap mode,
411
* this will typically require the caller to have already locked the
412
* relation. To avoid lock upgrade hazards, that lock should be at least
413
* as strong as the one we take here.
415
* NB: If the lock strength here ever changes, code that is run by
416
* parallel workers under the control of certain particular ambuild
417
* functions will need to be updated, too.
419
lockmode = stmt->concurrent ? ShareUpdateExclusiveLock : ShareLock;
420
rel = heap_open(relationId, lockmode);
422
relationId = RelationGetRelid(rel);
423
namespaceId = RelationGetNamespace(rel);
425
/* Ensure that it makes sense to index this kind of relation */
426
switch (rel->rd_rel->relkind)
428
case RELKIND_RELATION:
429
case RELKIND_MATVIEW:
430
case RELKIND_PARTITIONED_TABLE:
433
case RELKIND_FOREIGN_TABLE:
436
* Custom error message for FOREIGN TABLE since the term is close
437
* to a regular table and can confuse the user.
440
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
441
errmsg("cannot create index on foreign table \"%s\"",
442
RelationGetRelationName(rel))));
446
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
447
errmsg("\"%s\" is not a table or materialized view",
448
RelationGetRelationName(rel))));
453
* Establish behavior for partitioned tables, and verify sanity of
456
* We do not build an actual index in this case; we only create a few
457
* catalog entries. The actual indexes are built by recursing for each
460
partitioned = rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE;
463
if (stmt->concurrent)
465
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
466
errmsg("cannot create index on partitioned table \"%s\" concurrently",
467
RelationGetRelationName(rel))));
468
if (stmt->excludeOpNames)
470
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
471
errmsg("cannot create exclusion constraints on partitioned table \"%s\"",
472
RelationGetRelationName(rel))));
476
* Don't try to CREATE INDEX on temp tables of other backends.
478
if (RELATION_IS_OTHER_TEMP(rel))
480
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
481
errmsg("cannot create indexes on temporary tables of other sessions")));
484
* Unless our caller vouches for having checked this already, insist that
485
* the table not be in use by our own session, either. Otherwise we might
486
* fail to make entries in the new index (for instance, if an INSERT or
487
* UPDATE is in progress and has already made its list of target indexes).
489
if (check_not_in_use)
490
CheckTableNotInUse(rel, "CREATE INDEX");
493
* Verify we (still) have CREATE rights in the rel's namespace.
494
* (Presumably we did when the rel was created, but maybe not anymore.)
495
* Skip check if caller doesn't want it. Also skip check if
496
* bootstrapping, since permissions machinery may not be working yet.
498
if (check_rights && !IsBootstrapProcessingMode())
502
aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(),
504
if (aclresult != ACLCHECK_OK)
505
aclcheck_error(aclresult, OBJECT_SCHEMA,
506
get_namespace_name(namespaceId));
510
* Select tablespace to use. If not specified, use default tablespace
511
* (which may in turn default to database's default).
513
if (stmt->tableSpace)
515
tablespaceId = get_tablespace_oid(stmt->tableSpace, false);
519
tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence);
520
/* note InvalidOid is OK in this case */
523
/* Check tablespace permissions */
525
OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
529
aclresult = pg_tablespace_aclcheck(tablespaceId, GetUserId(),
531
if (aclresult != ACLCHECK_OK)
532
aclcheck_error(aclresult, OBJECT_TABLESPACE,
533
get_tablespace_name(tablespaceId));
537
* Force shared indexes into the pg_global tablespace. This is a bit of a
538
* hack but seems simpler than marking them in the BKI commands. On the
539
* other hand, if it's not shared, don't allow it to be placed there.
541
if (rel->rd_rel->relisshared)
542
tablespaceId = GLOBALTABLESPACE_OID;
543
else if (tablespaceId == GLOBALTABLESPACE_OID)
545
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
546
errmsg("only shared relations can be placed in pg_global tablespace")));
549
* Choose the index column names.
551
indexColNames = ChooseIndexColumnNames(allIndexParams);
554
* Select name for index if caller didn't specify
556
indexRelationName = stmt->idxname;
557
if (indexRelationName == NULL)
558
indexRelationName = ChooseIndexName(RelationGetRelationName(rel),
561
stmt->excludeOpNames,
566
* look up the access method, verify it can handle the requested features
568
accessMethodName = stmt->accessMethod;
569
tuple = SearchSysCache1(AMNAME, PointerGetDatum(accessMethodName));
570
if (!HeapTupleIsValid(tuple))
573
* Hack to provide more-or-less-transparent updating of old RTREE
574
* indexes to GiST: if RTREE is requested and not found, use GIST.
576
if (strcmp(accessMethodName, "rtree") == 0)
579
(errmsg("substituting access method \"gist\" for obsolete method \"rtree\"")));
580
accessMethodName = "gist";
581
tuple = SearchSysCache1(AMNAME, PointerGetDatum(accessMethodName));
584
if (!HeapTupleIsValid(tuple))
586
(errcode(ERRCODE_UNDEFINED_OBJECT),
587
errmsg("access method \"%s\" does not exist",
590
accessMethodId = HeapTupleGetOid(tuple);
591
accessMethodForm = (Form_pg_am) GETSTRUCT(tuple);
592
amRoutine = GetIndexAmRoutine(accessMethodForm->amhandler);
594
if (stmt->unique && !amRoutine->amcanunique)
596
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
597
errmsg("access method \"%s\" does not support unique indexes",
599
if (list_length(stmt->indexIncludingParams) > 0 && !amRoutine->amcaninclude)
601
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
602
errmsg("access method \"%s\" does not support included columns",
604
if (numberOfAttributes > 1 && !amRoutine->amcanmulticol)
606
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
607
errmsg("access method \"%s\" does not support multicolumn indexes",
609
if (stmt->excludeOpNames && amRoutine->amgettuple == NULL)
611
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
612
errmsg("access method \"%s\" does not support exclusion constraints",
615
amcanorder = amRoutine->amcanorder;
616
amoptions = amRoutine->amoptions;
619
ReleaseSysCache(tuple);
622
* Validate predicate, if given
624
if (stmt->whereClause)
625
CheckPredicate((Expr *) stmt->whereClause);
628
* Parse AM-specific options, convert to text array form, validate.
630
reloptions = transformRelOptions((Datum) 0, stmt->options,
631
NULL, NULL, false, false);
633
(void) index_reloptions(amoptions, reloptions, true);
636
* Prepare arguments for index_create, primarily an IndexInfo structure.
637
* Note that ii_Predicate must be in implicit-AND format.
639
indexInfo = makeNode(IndexInfo);
640
indexInfo->ii_NumIndexAttrs = numberOfAttributes;
641
indexInfo->ii_NumIndexKeyAttrs = numberOfKeyAttributes;
642
indexInfo->ii_Expressions = NIL; /* for now */
643
indexInfo->ii_ExpressionsState = NIL;
644
indexInfo->ii_Predicate = make_ands_implicit((Expr *) stmt->whereClause);
645
indexInfo->ii_PredicateState = NULL;
646
indexInfo->ii_ExclusionOps = NULL;
647
indexInfo->ii_ExclusionProcs = NULL;
648
indexInfo->ii_ExclusionStrats = NULL;
649
indexInfo->ii_Unique = stmt->unique;
650
/* In a concurrent build, mark it not-ready-for-inserts */
651
indexInfo->ii_ReadyForInserts = !stmt->concurrent;
652
indexInfo->ii_Concurrent = stmt->concurrent;
653
indexInfo->ii_BrokenHotChain = false;
654
indexInfo->ii_ParallelWorkers = 0;
655
indexInfo->ii_Am = accessMethodId;
656
indexInfo->ii_AmCache = NULL;
657
indexInfo->ii_Context = CurrentMemoryContext;
659
typeObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
660
collationObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
661
classObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
662
coloptions = (int16 *) palloc(numberOfAttributes * sizeof(int16));
663
ComputeIndexAttrs(indexInfo,
664
typeObjectId, collationObjectId, classObjectId,
665
coloptions, allIndexParams,
666
stmt->excludeOpNames, relationId,
667
accessMethodName, accessMethodId,
668
amcanorder, stmt->isconstraint);
671
* Extra checks when creating a PRIMARY KEY index.
674
index_check_primary_key(rel, indexInfo, is_alter_table);
677
* If this table is partitioned and we're creating a unique index or a
678
* primary key, make sure that the indexed columns are part of the
679
* partition key. Otherwise it would be possible to violate uniqueness by
680
* putting values that ought to be unique in different partitions.
682
* We could lift this limitation if we had global indexes, but those have
683
* their own problems, so this is a useful feature combination.
685
if (partitioned && (stmt->unique || stmt->primary))
687
PartitionKey key = rel->rd_partkey;
691
* A partitioned table can have unique indexes, as long as all the
692
* columns in the partition key appear in the unique key. A
693
* partition-local index can enforce global uniqueness iff the PK
694
* value completely determines the partition that a row is in.
696
* Thus, verify that all the columns in the partition key appear in
697
* the unique key definition.
699
for (i = 0; i < key->partnatts; i++)
703
const char *constraint_type;
706
constraint_type = "PRIMARY KEY";
707
else if (stmt->unique)
708
constraint_type = "UNIQUE";
709
else if (stmt->excludeOpNames != NIL)
710
constraint_type = "EXCLUDE";
713
elog(ERROR, "unknown constraint type");
714
constraint_type = NULL; /* keep compiler quiet */
718
* It may be possible to support UNIQUE constraints when partition
719
* keys are expressions, but is it worth it? Give up for now.
721
if (key->partattrs[i] == 0)
723
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
724
errmsg("unsupported %s constraint with partition key definition",
726
errdetail("%s constraints cannot be used when partition keys include expressions.",
729
for (j = 0; j < indexInfo->ii_NumIndexAttrs; j++)
731
if (key->partattrs[i] == indexInfo->ii_IndexAttrNumbers[j])
739
Form_pg_attribute att;
741
att = TupleDescAttr(RelationGetDescr(rel), key->partattrs[i] - 1);
743
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
744
errmsg("insufficient columns in %s constraint definition",
746
errdetail("%s constraint on table \"%s\" lacks column \"%s\" which is part of the partition key.",
747
constraint_type, RelationGetRelationName(rel),
748
NameStr(att->attname))));
755
* We disallow indexes on system columns other than OID. They would not
756
* necessarily get updated correctly, and they don't seem useful anyway.
758
for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++)
760
AttrNumber attno = indexInfo->ii_IndexAttrNumbers[i];
762
if (attno < 0 && attno != ObjectIdAttributeNumber)
764
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
765
errmsg("index creation on system columns is not supported")));
769
* Also check for system columns used in expressions or predicates.
771
if (indexInfo->ii_Expressions || indexInfo->ii_Predicate)
773
Bitmapset *indexattrs = NULL;
775
pull_varattnos((Node *) indexInfo->ii_Expressions, 1, &indexattrs);
776
pull_varattnos((Node *) indexInfo->ii_Predicate, 1, &indexattrs);
778
for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
780
if (i != ObjectIdAttributeNumber &&
781
bms_is_member(i - FirstLowInvalidHeapAttributeNumber,
784
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
785
errmsg("index creation on system columns is not supported")));
790
* Report index creation if appropriate (delay this till after most of the
793
if (stmt->isconstraint && !quiet)
795
const char *constraint_type;
798
constraint_type = "PRIMARY KEY";
799
else if (stmt->unique)
800
constraint_type = "UNIQUE";
801
else if (stmt->excludeOpNames != NIL)
802
constraint_type = "EXCLUDE";
805
elog(ERROR, "unknown constraint type");
806
constraint_type = NULL; /* keep compiler quiet */
810
(errmsg("%s %s will create implicit index \"%s\" for table \"%s\"",
811
is_alter_table ? "ALTER TABLE / ADD" : "CREATE TABLE /",
813
indexRelationName, RelationGetRelationName(rel))));
817
* A valid stmt->oldNode implies that we already have a built form of the
818
* index. The caller should also decline any index build.
820
Assert(!OidIsValid(stmt->oldNode) || (skip_build && !stmt->concurrent));
823
* Make the catalog entries for the index, including constraints. This
824
* step also actually builds the index, except if caller requested not to
825
* or in concurrent mode, in which case it'll be done later, or doing a
826
* partitioned index (because those don't have storage).
828
flags = constr_flags = 0;
829
if (stmt->isconstraint)
830
flags |= INDEX_CREATE_ADD_CONSTRAINT;
831
if (skip_build || stmt->concurrent || partitioned)
832
flags |= INDEX_CREATE_SKIP_BUILD;
833
if (stmt->if_not_exists)
834
flags |= INDEX_CREATE_IF_NOT_EXISTS;
835
if (stmt->concurrent)
836
flags |= INDEX_CREATE_CONCURRENT;
838
flags |= INDEX_CREATE_PARTITIONED;
840
flags |= INDEX_CREATE_IS_PRIMARY;
841
if (partitioned && stmt->relation && !stmt->relation->inh)
842
flags |= INDEX_CREATE_INVALID;
844
if (stmt->deferrable)
845
constr_flags |= INDEX_CONSTR_CREATE_DEFERRABLE;
846
if (stmt->initdeferred)
847
constr_flags |= INDEX_CONSTR_CREATE_INIT_DEFERRED;
850
index_create(rel, indexRelationName, indexRelationId, parentIndexId,
852
stmt->oldNode, indexInfo, indexColNames,
853
accessMethodId, tablespaceId,
854
collationObjectId, classObjectId,
855
coloptions, reloptions,
857
allowSystemTableMods, !check_rights,
858
&createdConstraintId);
860
ObjectAddressSet(address, RelationRelationId, indexRelationId);
862
if (!OidIsValid(indexRelationId))
864
heap_close(rel, NoLock);
868
/* Add any requested comment */
869
if (stmt->idxcomment != NULL)
870
CreateComments(indexRelationId, RelationRelationId, 0,
876
* Unless caller specified to skip this step (via ONLY), process each
877
* partition to make sure they all contain a corresponding index.
879
* If we're called internally (no stmt->relation), recurse always.
881
if (!stmt->relation || stmt->relation->inh)
883
PartitionDesc partdesc = RelationGetPartitionDesc(rel);
884
int nparts = partdesc->nparts;
885
Oid *part_oids = palloc(sizeof(Oid) * nparts);
886
bool invalidate_parent = false;
887
TupleDesc parentDesc;
890
memcpy(part_oids, partdesc->oids, sizeof(Oid) * nparts);
892
parentDesc = CreateTupleDescCopy(RelationGetDescr(rel));
893
opfamOids = palloc(sizeof(Oid) * numberOfKeyAttributes);
894
for (i = 0; i < numberOfKeyAttributes; i++)
895
opfamOids[i] = get_opclass_family(classObjectId[i]);
897
heap_close(rel, NoLock);
900
* For each partition, scan all existing indexes; if one matches
901
* our index definition and is not already attached to some other
902
* parent index, attach it to the one we just created.
904
* If none matches, build a new index by calling ourselves
905
* recursively with the same options (except for the index name).
907
for (i = 0; i < nparts; i++)
909
Oid childRelid = part_oids[i];
917
childrel = heap_open(childRelid, lockmode);
918
childidxs = RelationGetIndexList(childrel);
920
convert_tuples_by_name_map(RelationGetDescr(childrel),
922
gettext_noop("could not convert row type"));
923
maplen = parentDesc->natts;
926
foreach(cell, childidxs)
928
Oid cldidxid = lfirst_oid(cell);
930
IndexInfo *cldIdxInfo;
932
/* this index is already partition of another one */
933
if (has_superclass(cldidxid))
936
cldidx = index_open(cldidxid, lockmode);
937
cldIdxInfo = BuildIndexInfo(cldidx);
938
if (CompareIndexInfo(cldIdxInfo, indexInfo,
939
cldidx->rd_indcollation,
945
Oid cldConstrOid = InvalidOid;
950
* If this index is being created in the parent
951
* because of a constraint, then the child needs to
952
* have a constraint also, so look for one. If there
953
* is no such constraint, this index is no good, so
956
if (createdConstraintId != InvalidOid)
959
get_relation_idx_constraint_oid(childRelid,
961
if (cldConstrOid == InvalidOid)
963
index_close(cldidx, lockmode);
968
/* Attach index to parent and we're done. */
969
IndexSetParentIndex(cldidx, indexRelationId);
970
if (createdConstraintId != InvalidOid)
971
ConstraintSetParentConstraint(cldConstrOid,
972
createdConstraintId);
974
if (!IndexIsValid(cldidx->rd_index))
975
invalidate_parent = true;
978
/* keep lock till commit */
979
index_close(cldidx, NoLock);
983
index_close(cldidx, lockmode);
986
list_free(childidxs);
987
heap_close(childrel, NoLock);
990
* If no matching index was found, create our own.
994
IndexStmt *childStmt = copyObject(stmt);
995
bool found_whole_row;
997
childStmt->whereClause =
998
map_variable_attnos(stmt->whereClause, 1, 0,
1000
InvalidOid, &found_whole_row);
1001
if (found_whole_row)
1002
elog(ERROR, "cannot convert whole-row table reference");
1004
childStmt->idxname = NULL;
1005
childStmt->relationId = childRelid;
1006
DefineIndex(childRelid, childStmt,
1007
InvalidOid, /* no predefined OID */
1008
indexRelationId, /* this is our child */
1009
createdConstraintId,
1010
is_alter_table, check_rights, check_not_in_use,
1018
* The pg_index row we inserted for this index was marked
1019
* indisvalid=true. But if we attached an existing index that is
1020
* invalid, this is incorrect, so update our row to invalid too.
1022
if (invalidate_parent)
1024
Relation pg_index = heap_open(IndexRelationId, RowExclusiveLock);
1028
tup = SearchSysCache1(INDEXRELID,
1029
ObjectIdGetDatum(indexRelationId));
1031
elog(ERROR, "cache lookup failed for index %u",
1033
newtup = heap_copytuple(tup);
1034
((Form_pg_index) GETSTRUCT(newtup))->indisvalid = false;
1035
CatalogTupleUpdate(pg_index, &tup->t_self, newtup);
1036
ReleaseSysCache(tup);
1037
heap_close(pg_index, RowExclusiveLock);
1038
heap_freetuple(newtup);
1042
heap_close(rel, NoLock);
1045
* Indexes on partitioned tables are not themselves built, so we're
1051
if (!stmt->concurrent)
1053
/* Close the heap and we're done, in the non-concurrent case */
1054
heap_close(rel, NoLock);
1058
/* save lockrelid and locktag for below, then close rel */
1059
heaprelid = rel->rd_lockInfo.lockRelId;
1060
SET_LOCKTAG_RELATION(heaplocktag, heaprelid.dbId, heaprelid.relId);
1061
heap_close(rel, NoLock);
1064
* For a concurrent build, it's important to make the catalog entries
1065
* visible to other transactions before we start to build the index. That
1066
* will prevent them from making incompatible HOT updates. The new index
1067
* will be marked not indisready and not indisvalid, so that no one else
1068
* tries to either insert into it or use it for queries.
1070
* We must commit our current transaction so that the index becomes
1071
* visible; then start another. Note that all the data structures we just
1072
* built are lost in the commit. The only data we keep past here are the
1075
* Before committing, get a session-level lock on the table, to ensure
1076
* that neither it nor the index can be dropped before we finish. This
1077
* cannot block, even if someone else is waiting for access, because we
1078
* already have the same lock within our transaction.
1080
* Note: we don't currently bother with a session lock on the index,
1081
* because there are no operations that could change its state while we
1082
* hold lock on the parent table. This might need to change later.
1084
LockRelationIdForSession(&heaprelid, ShareUpdateExclusiveLock);
1086
PopActiveSnapshot();
1087
CommitTransactionCommand();
1088
StartTransactionCommand();
1091
* Phase 2 of concurrent index build (see comments for validate_index()
1092
* for an overview of how this works)
1094
* Now we must wait until no running transaction could have the table open
1095
* with the old list of indexes. Use ShareLock to consider running
1096
* transactions that hold locks that permit writing to the table. Note we
1097
* do not need to worry about xacts that open the table for writing after
1098
* this point; they will see the new index when they open it.
1100
* Note: the reason we use actual lock acquisition here, rather than just
1101
* checking the ProcArray and sleeping, is that deadlock is possible if
1102
* one of the transactions in question is blocked trying to acquire an
1103
* exclusive lock on our table. The lock code will detect deadlock and
1104
* error out properly.
1106
WaitForLockers(heaplocktag, ShareLock);
1109
* At this moment we are sure that there are no transactions with the
1110
* table open for write that don't have this new index in their list of
1111
* indexes. We have waited out all the existing transactions and any new
1112
* transaction will have the new index in its list, but the index is still
1113
* marked as "not-ready-for-inserts". The index is consulted while
1114
* deciding HOT-safety though. This arrangement ensures that no new HOT
1115
* chains can be created where the new tuple and the old tuple in the
1116
* chain have different index keys.
1118
* We now take a new snapshot, and build the index using all tuples that
1119
* are visible in this snapshot. We can be sure that any HOT updates to
1120
* these tuples will be compatible with the index, since any updates made
1121
* by transactions that didn't know about the index are now committed or
1122
* rolled back. Thus, each visible tuple is either the end of its
1123
* HOT-chain or the extension of the chain is HOT-safe for this index.
1126
/* Open and lock the parent heap relation */
1127
rel = heap_openrv(stmt->relation, ShareUpdateExclusiveLock);
1129
/* And the target index relation */
1130
indexRelation = index_open(indexRelationId, RowExclusiveLock);
1132
/* Set ActiveSnapshot since functions in the indexes may need it */
1133
PushActiveSnapshot(GetTransactionSnapshot());
1135
/* We have to re-build the IndexInfo struct, since it was lost in commit */
1136
indexInfo = BuildIndexInfo(indexRelation);
1137
Assert(!indexInfo->ii_ReadyForInserts);
1138
indexInfo->ii_Concurrent = true;
1139
indexInfo->ii_BrokenHotChain = false;
1141
/* Now build the index */
1142
index_build(rel, indexRelation, indexInfo, stmt->primary, false, true);
1144
/* Close both the relations, but keep the locks */
1145
heap_close(rel, NoLock);
1146
index_close(indexRelation, NoLock);
1149
* Update the pg_index row to mark the index as ready for inserts. Once we
1150
* commit this transaction, any new transactions that open the table must
1151
* insert new entries into the index for insertions and non-HOT updates.
1153
index_set_state_flags(indexRelationId, INDEX_CREATE_SET_READY);
1155
/* we can do away with our snapshot */
1156
PopActiveSnapshot();
1159
* Commit this transaction to make the indisready update visible.
1161
CommitTransactionCommand();
1162
StartTransactionCommand();
1165
* Phase 3 of concurrent index build
1167
* We once again wait until no transaction can have the table open with
1168
* the index marked as read-only for updates.
1170
WaitForLockers(heaplocktag, ShareLock);
1173
* Now take the "reference snapshot" that will be used by validate_index()
1174
* to filter candidate tuples. Beware! There might still be snapshots in
1175
* use that treat some transaction as in-progress that our reference
1176
* snapshot treats as committed. If such a recently-committed transaction
1177
* deleted tuples in the table, we will not include them in the index; yet
1178
* those transactions which see the deleting one as still-in-progress will
1179
* expect such tuples to be there once we mark the index as valid.
1181
* We solve this by waiting for all endangered transactions to exit before
1182
* we mark the index as valid.
1184
* We also set ActiveSnapshot to this snap, since functions in indexes may
1187
snapshot = RegisterSnapshot(GetTransactionSnapshot());
1188
PushActiveSnapshot(snapshot);
1191
* Scan the index and the heap, insert any missing index entries.
1193
validate_index(relationId, indexRelationId, snapshot);
1196
* Drop the reference snapshot. We must do this before waiting out other
1197
* snapshot holders, else we will deadlock against other processes also
1198
* doing CREATE INDEX CONCURRENTLY, which would see our snapshot as one
1199
* they must wait for. But first, save the snapshot's xmin to use as
1200
* limitXmin for GetCurrentVirtualXIDs().
1202
limitXmin = snapshot->xmin;
1204
PopActiveSnapshot();
1205
UnregisterSnapshot(snapshot);
1208
* The snapshot subsystem could still contain registered snapshots that
1209
* are holding back our process's advertised xmin; in particular, if
1210
* default_transaction_isolation = serializable, there is a transaction
1211
* snapshot that is still active. The CatalogSnapshot is likewise a
1212
* hazard. To ensure no deadlocks, we must commit and start yet another
1213
* transaction, and do our wait before any snapshot has been taken in it.
1215
CommitTransactionCommand();
1216
StartTransactionCommand();
1218
/* We should now definitely not be advertising any xmin. */
1219
Assert(MyPgXact->xmin == InvalidTransactionId);
1222
* The index is now valid in the sense that it contains all currently
1223
* interesting tuples. But since it might not contain tuples deleted just
1224
* before the reference snap was taken, we have to wait out any
1225
* transactions that might have older snapshots. Obtain a list of VXIDs
1226
* of such transactions, and wait for them individually.
1228
* We can exclude any running transactions that have xmin > the xmin of
1229
* our reference snapshot; their oldest snapshot must be newer than ours.
1230
* We can also exclude any transactions that have xmin = zero, since they
1231
* evidently have no live snapshot at all (and any one they might be in
1232
* process of taking is certainly newer than ours). Transactions in other
1233
* DBs can be ignored too, since they'll never even be able to see this
1236
* We can also exclude autovacuum processes and processes running manual
1237
* lazy VACUUMs, because they won't be fazed by missing index entries
1238
* either. (Manual ANALYZEs, however, can't be excluded because they
1239
* might be within transactions that are going to do arbitrary operations
1242
* Also, GetCurrentVirtualXIDs never reports our own vxid, so we need not
1245
* If a process goes idle-in-transaction with xmin zero, we do not need to
1246
* wait for it anymore, per the above argument. We do not have the
1247
* infrastructure right now to stop waiting if that happens, but we can at
1248
* least avoid the folly of waiting when it is idle at the time we would
1249
* begin to wait. We do this by repeatedly rechecking the output of
1250
* GetCurrentVirtualXIDs. If, during any iteration, a particular vxid
1251
* doesn't show up in the output, we know we can forget about it.
1253
old_snapshots = GetCurrentVirtualXIDs(limitXmin, true, false,
1254
PROC_IS_AUTOVACUUM | PROC_IN_VACUUM,
1257
for (i = 0; i < n_old_snapshots; i++)
1259
if (!VirtualTransactionIdIsValid(old_snapshots[i]))
1260
continue; /* found uninteresting in previous cycle */
1264
/* see if anything's changed ... */
1265
VirtualTransactionId *newer_snapshots;
1266
int n_newer_snapshots;
1270
newer_snapshots = GetCurrentVirtualXIDs(limitXmin,
1272
PROC_IS_AUTOVACUUM | PROC_IN_VACUUM,
1273
&n_newer_snapshots);
1274
for (j = i; j < n_old_snapshots; j++)
1276
if (!VirtualTransactionIdIsValid(old_snapshots[j]))
1277
continue; /* found uninteresting in previous cycle */
1278
for (k = 0; k < n_newer_snapshots; k++)
1280
if (VirtualTransactionIdEquals(old_snapshots[j],
1281
newer_snapshots[k]))
1284
if (k >= n_newer_snapshots) /* not there anymore */
1285
SetInvalidVirtualTransactionId(old_snapshots[j]);
1287
pfree(newer_snapshots);
1290
if (VirtualTransactionIdIsValid(old_snapshots[i]))
1291
VirtualXactLock(old_snapshots[i], true);
1295
* Index can now be marked valid -- update its pg_index entry
1297
index_set_state_flags(indexRelationId, INDEX_CREATE_SET_VALID);
1300
* The pg_index update will cause backends (including this one) to update
1301
* relcache entries for the index itself, but we should also send a
1302
* relcache inval on the parent table to force replanning of cached plans.
1303
* Otherwise existing sessions might fail to use the new index where it
1304
* would be useful. (Note that our earlier commits did not create reasons
1305
* to replan; so relcache flush on the index itself was sufficient.)
1307
CacheInvalidateRelcacheByRelid(heaprelid.relId);
1310
* Last thing to do is release the session-level lock on the parent table.
1312
UnlockRelationIdForSession(&heaprelid, ShareUpdateExclusiveLock);
1320
* Test whether given expression is mutable
1323
CheckMutability(Expr *expr)
1326
* First run the expression through the planner. This has a couple of
1327
* important consequences. First, function default arguments will get
1328
* inserted, which may affect volatility (consider "default now()").
1329
* Second, inline-able functions will get inlined, which may allow us to
1330
* conclude that the function is really less volatile than it's marked. As
1331
* an example, polymorphic functions must be marked with the most volatile
1332
* behavior that they have for any input type, but once we inline the
1333
* function we may be able to conclude that it's not so volatile for the
1334
* particular input type we're dealing with.
1336
* We assume here that expression_planner() won't scribble on its input.
1338
expr = expression_planner(expr);
1340
/* Now we can search for non-immutable functions */
1341
return contain_mutable_functions((Node *) expr);
1347
* Checks that the given partial-index predicate is valid.
1349
* This used to also constrain the form of the predicate to forms that
1350
* indxpath.c could do something with. However, that seems overly
1351
* restrictive. One useful application of partial indexes is to apply
1352
* a UNIQUE constraint across a subset of a table, and in that scenario
1353
* any evaluable predicate will work. So accept any predicate here
1354
* (except ones requiring a plan), and let indxpath.c fend for itself.
1357
CheckPredicate(Expr *predicate)
1360
* transformExpr() should have already rejected subqueries, aggregates,
1361
* and window functions, based on the EXPR_KIND_ for a predicate.
1365
* A predicate using mutable functions is probably wrong, for the same
1366
* reasons that we don't allow an index expression to use one.
1368
if (CheckMutability(predicate))
1370
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1371
errmsg("functions in index predicate must be marked IMMUTABLE")));
1375
* Compute per-index-column information, including indexed column numbers
1376
* or index expressions, opclasses, and indoptions. Note, all output vectors
1377
* should be allocated for all columns, including "including" ones.
1380
ComputeIndexAttrs(IndexInfo *indexInfo,
1385
List *attList, /* list of IndexElem's */
1386
List *exclusionOpNames,
1388
const char *accessMethodName,
1393
ListCell *nextExclOp;
1396
int nkeycols = indexInfo->ii_NumIndexKeyAttrs;
1398
/* Allocate space for exclusion operator info, if needed */
1399
if (exclusionOpNames)
1401
Assert(list_length(exclusionOpNames) == nkeycols);
1402
indexInfo->ii_ExclusionOps = (Oid *) palloc(sizeof(Oid) * nkeycols);
1403
indexInfo->ii_ExclusionProcs = (Oid *) palloc(sizeof(Oid) * nkeycols);
1404
indexInfo->ii_ExclusionStrats = (uint16 *) palloc(sizeof(uint16) * nkeycols);
1405
nextExclOp = list_head(exclusionOpNames);
1411
* process attributeList
1414
foreach(lc, attList)
1416
IndexElem *attribute = (IndexElem *) lfirst(lc);
1421
* Process the column-or-expression to be indexed.
1423
if (attribute->name != NULL)
1425
/* Simple index attribute */
1427
Form_pg_attribute attform;
1429
Assert(attribute->expr == NULL);
1430
atttuple = SearchSysCacheAttName(relId, attribute->name);
1431
if (!HeapTupleIsValid(atttuple))
1433
/* difference in error message spellings is historical */
1436
(errcode(ERRCODE_UNDEFINED_COLUMN),
1437
errmsg("column \"%s\" named in key does not exist",
1441
(errcode(ERRCODE_UNDEFINED_COLUMN),
1442
errmsg("column \"%s\" does not exist",
1445
attform = (Form_pg_attribute) GETSTRUCT(atttuple);
1446
indexInfo->ii_IndexAttrNumbers[attn] = attform->attnum;
1447
atttype = attform->atttypid;
1448
attcollation = attform->attcollation;
1449
ReleaseSysCache(atttuple);
1453
/* Index expression */
1454
Node *expr = attribute->expr;
1456
Assert(expr != NULL);
1458
if (attn >= nkeycols)
1460
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1461
errmsg("expressions are not supported in included columns")));
1462
atttype = exprType(expr);
1463
attcollation = exprCollation(expr);
1466
* Strip any top-level COLLATE clause. This ensures that we treat
1467
* "x COLLATE y" and "(x COLLATE y)" alike.
1469
while (IsA(expr, CollateExpr))
1470
expr = (Node *) ((CollateExpr *) expr)->arg;
1472
if (IsA(expr, Var) &&
1473
((Var *) expr)->varattno != InvalidAttrNumber)
1476
* User wrote "(column)" or "(column COLLATE something)".
1477
* Treat it like simple attribute anyway.
1479
indexInfo->ii_IndexAttrNumbers[attn] = ((Var *) expr)->varattno;
1483
indexInfo->ii_IndexAttrNumbers[attn] = 0; /* marks expression */
1484
indexInfo->ii_Expressions = lappend(indexInfo->ii_Expressions,
1488
* transformExpr() should have already rejected subqueries,
1489
* aggregates, and window functions, based on the EXPR_KIND_
1490
* for an index expression.
1494
* An expression using mutable functions is probably wrong,
1495
* since if you aren't going to get the same result for the
1496
* same data every time, it's not clear what the index entries
1499
if (CheckMutability((Expr *) expr))
1501
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1502
errmsg("functions in index expression must be marked IMMUTABLE")));
1506
typeOidP[attn] = atttype;
1509
* Included columns have no collation, no opclass and no ordering
1512
if (attn >= nkeycols)
1514
if (attribute->collation)
1516
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1517
errmsg("including column does not support a collation")));
1518
if (attribute->opclass)
1520
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1521
errmsg("including column does not support an operator class")));
1522
if (attribute->ordering != SORTBY_DEFAULT)
1524
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1525
errmsg("including column does not support ASC/DESC options")));
1526
if (attribute->nulls_ordering != SORTBY_NULLS_DEFAULT)
1528
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1529
errmsg("including column does not support NULLS FIRST/LAST options")));
1531
classOidP[attn] = InvalidOid;
1532
colOptionP[attn] = 0;
1533
collationOidP[attn] = InvalidOid;
1540
* Apply collation override if any
1542
if (attribute->collation)
1543
attcollation = get_collation_oid(attribute->collation, false);
1546
* Check we have a collation iff it's a collatable type. The only
1547
* expected failures here are (1) COLLATE applied to a noncollatable
1548
* type, or (2) index expression had an unresolved collation. But we
1549
* might as well code this to be a complete consistency check.
1551
if (type_is_collatable(atttype))
1553
if (!OidIsValid(attcollation))
1555
(errcode(ERRCODE_INDETERMINATE_COLLATION),
1556
errmsg("could not determine which collation to use for index expression"),
1557
errhint("Use the COLLATE clause to set the collation explicitly.")));
1561
if (OidIsValid(attcollation))
1563
(errcode(ERRCODE_DATATYPE_MISMATCH),
1564
errmsg("collations are not supported by type %s",
1565
format_type_be(atttype))));
1568
collationOidP[attn] = attcollation;
1571
* Identify the opclass to use.
1573
classOidP[attn] = ResolveOpClass(attribute->opclass,
1579
* Identify the exclusion operator, if any.
1583
List *opname = (List *) lfirst(nextExclOp);
1589
* Find the operator --- it must accept the column datatype
1590
* without runtime coercion (but binary compatibility is OK)
1592
opid = compatible_oper_opid(opname, atttype, atttype, false);
1595
* Only allow commutative operators to be used in exclusion
1596
* constraints. If X conflicts with Y, but Y does not conflict
1597
* with X, bad things will happen.
1599
if (get_commutator(opid) != opid)
1601
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
1602
errmsg("operator %s is not commutative",
1603
format_operator(opid)),
1604
errdetail("Only commutative operators can be used in exclusion constraints.")));
1607
* Operator must be a member of the right opfamily, too
1609
opfamily = get_opclass_family(classOidP[attn]);
1610
strat = get_op_opfamily_strategy(opid, opfamily);
1614
Form_pg_opfamily opfform;
1617
* attribute->opclass might not explicitly name the opfamily,
1618
* so fetch the name of the selected opfamily for use in the
1621
opftuple = SearchSysCache1(OPFAMILYOID,
1622
ObjectIdGetDatum(opfamily));
1623
if (!HeapTupleIsValid(opftuple))
1624
elog(ERROR, "cache lookup failed for opfamily %u",
1626
opfform = (Form_pg_opfamily) GETSTRUCT(opftuple);
1629
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
1630
errmsg("operator %s is not a member of operator family \"%s\"",
1631
format_operator(opid),
1632
NameStr(opfform->opfname)),
1633
errdetail("The exclusion operator must be related to the index operator class for the constraint.")));
1636
indexInfo->ii_ExclusionOps[attn] = opid;
1637
indexInfo->ii_ExclusionProcs[attn] = get_opcode(opid);
1638
indexInfo->ii_ExclusionStrats[attn] = strat;
1639
nextExclOp = lnext(nextExclOp);
1643
* Set up the per-column options (indoption field). For now, this is
1644
* zero for any un-ordered index, while ordered indexes have DESC and
1645
* NULLS FIRST/LAST options.
1647
colOptionP[attn] = 0;
1650
/* default ordering is ASC */
1651
if (attribute->ordering == SORTBY_DESC)
1652
colOptionP[attn] |= INDOPTION_DESC;
1653
/* default null ordering is LAST for ASC, FIRST for DESC */
1654
if (attribute->nulls_ordering == SORTBY_NULLS_DEFAULT)
1656
if (attribute->ordering == SORTBY_DESC)
1657
colOptionP[attn] |= INDOPTION_NULLS_FIRST;
1659
else if (attribute->nulls_ordering == SORTBY_NULLS_FIRST)
1660
colOptionP[attn] |= INDOPTION_NULLS_FIRST;
1664
/* index AM does not support ordering */
1665
if (attribute->ordering != SORTBY_DEFAULT)
1667
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1668
errmsg("access method \"%s\" does not support ASC/DESC options",
1669
accessMethodName)));
1670
if (attribute->nulls_ordering != SORTBY_NULLS_DEFAULT)
1672
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1673
errmsg("access method \"%s\" does not support NULLS FIRST/LAST options",
1674
accessMethodName)));
1682
* Resolve possibly-defaulted operator class specification
1684
* Note: This is used to resolve operator class specification in index and
1685
* partition key definitions.
1688
ResolveOpClass(List *opclass, Oid attrType,
1689
const char *accessMethodName, Oid accessMethodId)
1698
* Release 7.0 removed network_ops, timespan_ops, and datetime_ops, so we
1699
* ignore those opclass names so the default *_ops is used. This can be
1700
* removed in some later release. bjm 2000/02/07
1702
* Release 7.1 removes lztext_ops, so suppress that too for a while. tgl
1705
* Release 7.2 renames timestamp_ops to timestamptz_ops, so suppress that
1706
* too for awhile. I'm starting to think we need a better approach. tgl
1709
* Release 8.0 removes bigbox_ops (which was dead code for a long while
1710
* anyway). tgl 2003/11/11
1712
if (list_length(opclass) == 1)
1714
char *claname = strVal(linitial(opclass));
1716
if (strcmp(claname, "network_ops") == 0 ||
1717
strcmp(claname, "timespan_ops") == 0 ||
1718
strcmp(claname, "datetime_ops") == 0 ||
1719
strcmp(claname, "lztext_ops") == 0 ||
1720
strcmp(claname, "timestamp_ops") == 0 ||
1721
strcmp(claname, "bigbox_ops") == 0)
1727
/* no operator class specified, so find the default */
1728
opClassId = GetDefaultOpClass(attrType, accessMethodId);
1729
if (!OidIsValid(opClassId))
1731
(errcode(ERRCODE_UNDEFINED_OBJECT),
1732
errmsg("data type %s has no default operator class for access method \"%s\"",
1733
format_type_be(attrType), accessMethodName),
1734
errhint("You must specify an operator class for the index or define a default operator class for the data type.")));
1739
* Specific opclass name given, so look up the opclass.
1742
/* deconstruct the name list */
1743
DeconstructQualifiedName(opclass, &schemaname, &opcname);
1747
/* Look in specific schema only */
1750
namespaceId = LookupExplicitNamespace(schemaname, false);
1751
tuple = SearchSysCache3(CLAAMNAMENSP,
1752
ObjectIdGetDatum(accessMethodId),
1753
PointerGetDatum(opcname),
1754
ObjectIdGetDatum(namespaceId));
1758
/* Unqualified opclass name, so search the search path */
1759
opClassId = OpclassnameGetOpcid(accessMethodId, opcname);
1760
if (!OidIsValid(opClassId))
1762
(errcode(ERRCODE_UNDEFINED_OBJECT),
1763
errmsg("operator class \"%s\" does not exist for access method \"%s\"",
1764
opcname, accessMethodName)));
1765
tuple = SearchSysCache1(CLAOID, ObjectIdGetDatum(opClassId));
1768
if (!HeapTupleIsValid(tuple))
1770
(errcode(ERRCODE_UNDEFINED_OBJECT),
1771
errmsg("operator class \"%s\" does not exist for access method \"%s\"",
1772
NameListToString(opclass), accessMethodName)));
1775
* Verify that the index operator class accepts this datatype. Note we
1776
* will accept binary compatibility.
1778
opClassId = HeapTupleGetOid(tuple);
1779
opInputType = ((Form_pg_opclass) GETSTRUCT(tuple))->opcintype;
1781
if (!IsBinaryCoercible(attrType, opInputType))
1783
(errcode(ERRCODE_DATATYPE_MISMATCH),
1784
errmsg("operator class \"%s\" does not accept data type %s",
1785
NameListToString(opclass), format_type_be(attrType))));
1787
ReleaseSysCache(tuple);
1795
* Given the OIDs of a datatype and an access method, find the default
1796
* operator class, if any. Returns InvalidOid if there is none.
1799
GetDefaultOpClass(Oid type_id, Oid am_id)
1801
Oid result = InvalidOid;
1803
int ncompatible = 0;
1804
int ncompatiblepreferred = 0;
1806
ScanKeyData skey[1];
1809
TYPCATEGORY tcategory;
1811
/* If it's a domain, look at the base type instead */
1812
type_id = getBaseType(type_id);
1814
tcategory = TypeCategory(type_id);
1817
* We scan through all the opclasses available for the access method,
1818
* looking for one that is marked default and matches the target type
1819
* (either exactly or binary-compatibly, but prefer an exact match).
1821
* We could find more than one binary-compatible match. If just one is
1822
* for a preferred type, use that one; otherwise we fail, forcing the user
1823
* to specify which one he wants. (The preferred-type special case is a
1824
* kluge for varchar: it's binary-compatible to both text and bpchar, so
1825
* we need a tiebreaker.) If we find more than one exact match, then
1826
* someone put bogus entries in pg_opclass.
1828
rel = heap_open(OperatorClassRelationId, AccessShareLock);
1830
ScanKeyInit(&skey[0],
1831
Anum_pg_opclass_opcmethod,
1832
BTEqualStrategyNumber, F_OIDEQ,
1833
ObjectIdGetDatum(am_id));
1835
scan = systable_beginscan(rel, OpclassAmNameNspIndexId, true,
1838
while (HeapTupleIsValid(tup = systable_getnext(scan)))
1840
Form_pg_opclass opclass = (Form_pg_opclass) GETSTRUCT(tup);
1842
/* ignore altogether if not a default opclass */
1843
if (!opclass->opcdefault)
1845
if (opclass->opcintype == type_id)
1848
result = HeapTupleGetOid(tup);
1850
else if (nexact == 0 &&
1851
IsBinaryCoercible(type_id, opclass->opcintype))
1853
if (IsPreferredType(tcategory, opclass->opcintype))
1855
ncompatiblepreferred++;
1856
result = HeapTupleGetOid(tup);
1858
else if (ncompatiblepreferred == 0)
1861
result = HeapTupleGetOid(tup);
1866
systable_endscan(scan);
1868
heap_close(rel, AccessShareLock);
1870
/* raise error if pg_opclass contains inconsistent data */
1873
(errcode(ERRCODE_DUPLICATE_OBJECT),
1874
errmsg("there are multiple default operator classes for data type %s",
1875
format_type_be(type_id))));
1878
ncompatiblepreferred == 1 ||
1879
(ncompatiblepreferred == 0 && ncompatible == 1))
1888
* Create a name for an implicitly created index, sequence, constraint,
1889
* extended statistics, etc.
1891
* The parameters are typically: the original table name, the original field
1892
* name, and a "type" string (such as "seq" or "pkey"). The field name
1893
* and/or type can be NULL if not relevant.
1895
* The result is a palloc'd string.
1897
* The basic result we want is "name1_name2_label", omitting "_name2" or
1898
* "_label" when those parameters are NULL. However, we must generate
1899
* a name with less than NAMEDATALEN characters! So, we truncate one or
1900
* both names if necessary to make a short-enough string. The label part
1901
* is never truncated (so it had better be reasonably short).
1903
* The caller is responsible for checking uniqueness of the generated
1904
* name and retrying as needed; retrying will be done by altering the
1905
* "label" string (which is why we never truncate that part).
1908
makeObjectName(const char *name1, const char *name2, const char *label)
1911
int overhead = 0; /* chars needed for label and underscores */
1912
int availchars; /* chars available for name(s) */
1913
int name1chars; /* chars allocated to name1 */
1914
int name2chars; /* chars allocated to name2 */
1917
name1chars = strlen(name1);
1920
name2chars = strlen(name2);
1921
overhead++; /* allow for separating underscore */
1926
overhead += strlen(label) + 1;
1928
availchars = NAMEDATALEN - 1 - overhead;
1929
Assert(availchars > 0); /* else caller chose a bad label */
1932
* If we must truncate, preferentially truncate the longer name. This
1933
* logic could be expressed without a loop, but it's simple and obvious as
1936
while (name1chars + name2chars > availchars)
1938
if (name1chars > name2chars)
1944
name1chars = pg_mbcliplen(name1, name1chars, name1chars);
1946
name2chars = pg_mbcliplen(name2, name2chars, name2chars);
1948
/* Now construct the string using the chosen lengths */
1949
name = palloc(name1chars + name2chars + overhead + 1);
1950
memcpy(name, name1, name1chars);
1955
memcpy(name + ndx, name2, name2chars);
1961
strcpy(name + ndx, label);
1970
* Select a nonconflicting name for a new relation. This is ordinarily
1971
* used to choose index names (which is why it's here) but it can also
1972
* be used for sequences, or any autogenerated relation kind.
1974
* name1, name2, and label are used the same way as for makeObjectName(),
1975
* except that the label can't be NULL; digits will be appended to the label
1976
* if needed to create a name that is unique within the specified namespace.
1978
* Note: it is theoretically possible to get a collision anyway, if someone
1979
* else chooses the same name concurrently. This is fairly unlikely to be
1980
* a problem in practice, especially if one is holding an exclusive lock on
1981
* the relation identified by name1. However, if choosing multiple names
1982
* within a single command, you'd better create the new object and do
1983
* CommandCounterIncrement before choosing the next one!
1985
* Returns a palloc'd string.
1988
ChooseRelationName(const char *name1, const char *name2,
1989
const char *label, Oid namespaceid)
1992
char *relname = NULL;
1993
char modlabel[NAMEDATALEN];
1995
/* try the unmodified label first */
1996
StrNCpy(modlabel, label, sizeof(modlabel));
2000
relname = makeObjectName(name1, name2, modlabel);
2002
if (!OidIsValid(get_relname_relid(relname, namespaceid)))
2005
/* found a conflict, so try a new name component */
2007
snprintf(modlabel, sizeof(modlabel), "%s%d", label, ++pass);
2014
* Select the name to be used for an index.
2016
* The argument list is pretty ad-hoc :-(
2019
ChooseIndexName(const char *tabname, Oid namespaceId,
2020
List *colnames, List *exclusionOpNames,
2021
bool primary, bool isconstraint)
2027
/* the primary key's name does not depend on the specific column(s) */
2028
indexname = ChooseRelationName(tabname,
2033
else if (exclusionOpNames != NIL)
2035
indexname = ChooseRelationName(tabname,
2036
ChooseIndexNameAddition(colnames),
2040
else if (isconstraint)
2042
indexname = ChooseRelationName(tabname,
2043
ChooseIndexNameAddition(colnames),
2049
indexname = ChooseRelationName(tabname,
2050
ChooseIndexNameAddition(colnames),
2059
* Generate "name2" for a new index given the list of column names for it
2060
* (as produced by ChooseIndexColumnNames). This will be passed to
2061
* ChooseRelationName along with the parent table name and a suitable label.
2063
* We know that less than NAMEDATALEN characters will actually be used,
2064
* so we can truncate the result once we've generated that many.
2066
* XXX See also ChooseExtendedStatisticNameAddition.
2069
ChooseIndexNameAddition(List *colnames)
2071
char buf[NAMEDATALEN * 2];
2076
foreach(lc, colnames)
2078
const char *name = (const char *) lfirst(lc);
2081
buf[buflen++] = '_'; /* insert _ between names */
2084
* At this point we have buflen <= NAMEDATALEN. name should be less
2085
* than NAMEDATALEN already, but use strlcpy for paranoia.
2087
strlcpy(buf + buflen, name, NAMEDATALEN);
2088
buflen += strlen(buf + buflen);
2089
if (buflen >= NAMEDATALEN)
2092
return pstrdup(buf);
2096
* Select the actual names to be used for the columns of an index, given the
2097
* list of IndexElems for the columns. This is mostly about ensuring the
2098
* names are unique so we don't get a conflicting-attribute-names error.
2100
* Returns a List of plain strings (char *, not String nodes).
2103
ChooseIndexColumnNames(List *indexElems)
2108
foreach(lc, indexElems)
2110
IndexElem *ielem = (IndexElem *) lfirst(lc);
2111
const char *origname;
2112
const char *curname;
2114
char buf[NAMEDATALEN];
2116
/* Get the preliminary name from the IndexElem */
2117
if (ielem->indexcolname)
2118
origname = ielem->indexcolname; /* caller-specified name */
2119
else if (ielem->name)
2120
origname = ielem->name; /* simple column reference */
2122
origname = "expr"; /* default name for expression */
2124
/* If it conflicts with any previous column, tweak it */
2132
foreach(lc2, result)
2134
if (strcmp(curname, (char *) lfirst(lc2)) == 0)
2138
break; /* found nonconflicting name */
2140
sprintf(nbuf, "%d", i);
2142
/* Ensure generated names are shorter than NAMEDATALEN */
2143
nlen = pg_mbcliplen(origname, strlen(origname),
2144
NAMEDATALEN - 1 - strlen(nbuf));
2145
memcpy(buf, origname, nlen);
2146
strcpy(buf + nlen, nbuf);
2150
/* And attach to the result list */
2151
result = lappend(result, pstrdup(curname));
2158
* Recreate a specific index.
2161
ReindexIndex(RangeVar *indexRelation, int options)
2164
Oid heapOid = InvalidOid;
2169
* Find and lock index, and check permissions on table; use callback to
2170
* obtain lock on table first, to avoid deadlock hazard. The lock level
2171
* used here must match the index lock obtained in reindex_index().
2173
indOid = RangeVarGetRelidExtended(indexRelation, AccessExclusiveLock,
2175
RangeVarCallbackForReindexIndex,
2179
* Obtain the current persistence of the existing index. We already hold
2180
* lock on the index.
2182
irel = index_open(indOid, NoLock);
2184
if (irel->rd_rel->relkind == RELKIND_PARTITIONED_INDEX)
2186
ReindexPartitionedIndex(irel);
2190
persistence = irel->rd_rel->relpersistence;
2191
index_close(irel, NoLock);
2193
reindex_index(indOid, false, persistence, options);
2197
* Check permissions on table before acquiring relation lock; also lock
2198
* the heap before the RangeVarGetRelidExtended takes the index lock, to avoid
2202
RangeVarCallbackForReindexIndex(const RangeVar *relation,
2203
Oid relId, Oid oldRelId, void *arg)
2206
Oid *heapOid = (Oid *) arg;
2209
* If we previously locked some other index's heap, and the name we're
2210
* looking up no longer refers to that relation, release the now-useless
2213
if (relId != oldRelId && OidIsValid(oldRelId))
2215
/* lock level here should match reindex_index() heap lock */
2216
UnlockRelationOid(*heapOid, ShareLock);
2217
*heapOid = InvalidOid;
2220
/* If the relation does not exist, there's nothing more to do. */
2221
if (!OidIsValid(relId))
2225
* If the relation does exist, check whether it's an index. But note that
2226
* the relation might have been dropped between the time we did the name
2227
* lookup and now. In that case, there's nothing to do.
2229
relkind = get_rel_relkind(relId);
2232
if (relkind != RELKIND_INDEX &&
2233
relkind != RELKIND_PARTITIONED_INDEX)
2235
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
2236
errmsg("\"%s\" is not an index", relation->relname)));
2238
/* Check permissions */
2239
if (!pg_class_ownercheck(relId, GetUserId()))
2240
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_INDEX, relation->relname);
2242
/* Lock heap before index to avoid deadlock. */
2243
if (relId != oldRelId)
2246
* Lock level here should match reindex_index() heap lock. If the OID
2247
* isn't valid, it means the index as concurrently dropped, which is
2248
* not a problem for us; just return normally.
2250
*heapOid = IndexGetRelation(relId, true);
2251
if (OidIsValid(*heapOid))
2252
LockRelationOid(*heapOid, ShareLock);
2258
* Recreate all indexes of a table (and of its toast table, if any)
2261
ReindexTable(RangeVar *relation, int options)
2265
/* The lock level used here should match reindex_relation(). */
2266
heapOid = RangeVarGetRelidExtended(relation, ShareLock, 0,
2267
RangeVarCallbackOwnsTable, NULL);
2269
if (!reindex_relation(heapOid,
2270
REINDEX_REL_PROCESS_TOAST |
2271
REINDEX_REL_CHECK_CONSTRAINTS,
2274
(errmsg("table \"%s\" has no indexes",
2275
relation->relname)));
2281
* ReindexMultipleTables
2282
* Recreate indexes of tables selected by objectName/objectKind.
2284
* To reduce the probability of deadlocks, each table is reindexed in a
2285
* separate transaction, so we can release the lock on it right away.
2286
* That means this must not be called within a user transaction block!
2289
ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind,
2293
Relation relationRelation;
2295
ScanKeyData scan_keys[1];
2297
MemoryContext private_context;
2303
AssertArg(objectName);
2304
Assert(objectKind == REINDEX_OBJECT_SCHEMA ||
2305
objectKind == REINDEX_OBJECT_SYSTEM ||
2306
objectKind == REINDEX_OBJECT_DATABASE);
2309
* Get OID of object to reindex, being the database currently being used
2310
* by session for a database or for system catalogs, or the schema defined
2311
* by caller. At the same time do permission checks that need different
2312
* processing depending on the object type.
2314
if (objectKind == REINDEX_OBJECT_SCHEMA)
2316
objectOid = get_namespace_oid(objectName, false);
2318
if (!pg_namespace_ownercheck(objectOid, GetUserId()))
2319
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_SCHEMA,
2324
objectOid = MyDatabaseId;
2326
if (strcmp(objectName, get_database_name(objectOid)) != 0)
2328
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2329
errmsg("can only reindex the currently open database")));
2330
if (!pg_database_ownercheck(objectOid, GetUserId()))
2331
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_DATABASE,
2336
* Create a memory context that will survive forced transaction commits we
2337
* do below. Since it is a child of PortalContext, it will go away
2338
* eventually even if we suffer an error; there's no need for special
2339
* abort cleanup logic.
2341
private_context = AllocSetContextCreate(PortalContext,
2342
"ReindexMultipleTables",
2343
ALLOCSET_SMALL_SIZES);
2346
* Define the search keys to find the objects to reindex. For a schema, we
2347
* select target relations using relnamespace, something not necessary for
2348
* a database-wide operation.
2350
if (objectKind == REINDEX_OBJECT_SCHEMA)
2353
ScanKeyInit(&scan_keys[0],
2354
Anum_pg_class_relnamespace,
2355
BTEqualStrategyNumber, F_OIDEQ,
2356
ObjectIdGetDatum(objectOid));
2362
* Scan pg_class to build a list of the relations we need to reindex.
2364
* We only consider plain relations and materialized views here (toast
2365
* rels will be processed indirectly by reindex_relation).
2367
relationRelation = heap_open(RelationRelationId, AccessShareLock);
2368
scan = heap_beginscan_catalog(relationRelation, num_keys, scan_keys);
2369
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
2371
Form_pg_class classtuple = (Form_pg_class) GETSTRUCT(tuple);
2372
Oid relid = HeapTupleGetOid(tuple);
2375
* Only regular tables and matviews can have indexes, so ignore any
2376
* other kind of relation.
2378
* It is tempting to also consider partitioned tables here, but that
2379
* has the problem that if the children are in the same schema, they
2380
* would be processed twice. Maybe we could have a separate list of
2381
* partitioned tables, and expand that afterwards into relids,
2382
* ignoring any duplicates.
2384
if (classtuple->relkind != RELKIND_RELATION &&
2385
classtuple->relkind != RELKIND_MATVIEW)
2388
/* Skip temp tables of other backends; we can't reindex them at all */
2389
if (classtuple->relpersistence == RELPERSISTENCE_TEMP &&
2390
!isTempNamespace(classtuple->relnamespace))
2393
/* Check user/system classification, and optionally skip */
2394
if (objectKind == REINDEX_OBJECT_SYSTEM &&
2395
!IsSystemClass(relid, classtuple))
2398
/* Save the list of relation OIDs in private context */
2399
old = MemoryContextSwitchTo(private_context);
2402
* We always want to reindex pg_class first if it's selected to be
2403
* reindexed. This ensures that if there is any corruption in
2404
* pg_class' indexes, they will be fixed before we process any other
2405
* tables. This is critical because reindexing itself will try to
2408
if (relid == RelationRelationId)
2409
relids = lcons_oid(relid, relids);
2411
relids = lappend_oid(relids, relid);
2413
MemoryContextSwitchTo(old);
2416
heap_close(relationRelation, AccessShareLock);
2418
/* Now reindex each rel in a separate transaction */
2419
PopActiveSnapshot();
2420
CommitTransactionCommand();
2423
Oid relid = lfirst_oid(l);
2425
StartTransactionCommand();
2426
/* functions in indexes may want a snapshot set */
2427
PushActiveSnapshot(GetTransactionSnapshot());
2428
if (reindex_relation(relid,
2429
REINDEX_REL_PROCESS_TOAST |
2430
REINDEX_REL_CHECK_CONSTRAINTS,
2433
if (options & REINDEXOPT_VERBOSE)
2435
(errmsg("table \"%s.%s\" was reindexed",
2436
get_namespace_name(get_rel_namespace(relid)),
2437
get_rel_name(relid))));
2438
PopActiveSnapshot();
2439
CommitTransactionCommand();
2441
StartTransactionCommand();
2443
MemoryContextDelete(private_context);
2447
* ReindexPartitionedIndex
2448
* Reindex each child of the given partitioned index.
2450
* Not yet implemented.
2453
ReindexPartitionedIndex(Relation parentIdx)
2456
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2457
errmsg("REINDEX is not yet implemented for partitioned indexes")));
2461
* Insert or delete an appropriate pg_inherits tuple to make the given index
2462
* be a partition of the indicated parent index.
2464
* This also corrects the pg_depend information for the affected index.
2467
IndexSetParentIndex(Relation partitionIdx, Oid parentOid)
2469
Relation pg_inherits;
2472
Oid partRelid = RelationGetRelid(partitionIdx);
2474
bool fix_dependencies;
2476
/* Make sure this is an index */
2477
Assert(partitionIdx->rd_rel->relkind == RELKIND_INDEX ||
2478
partitionIdx->rd_rel->relkind == RELKIND_PARTITIONED_INDEX);
2481
* Scan pg_inherits for rows linking our index to some parent.
2483
pg_inherits = relation_open(InheritsRelationId, RowExclusiveLock);
2484
ScanKeyInit(&key[0],
2485
Anum_pg_inherits_inhrelid,
2486
BTEqualStrategyNumber, F_OIDEQ,
2487
ObjectIdGetDatum(partRelid));
2488
ScanKeyInit(&key[1],
2489
Anum_pg_inherits_inhseqno,
2490
BTEqualStrategyNumber, F_INT4EQ,
2492
scan = systable_beginscan(pg_inherits, InheritsRelidSeqnoIndexId, true,
2494
tuple = systable_getnext(scan);
2496
if (!HeapTupleIsValid(tuple))
2498
if (parentOid == InvalidOid)
2501
* No pg_inherits row, and no parent wanted: nothing to do in this
2504
fix_dependencies = false;
2508
Datum values[Natts_pg_inherits];
2509
bool isnull[Natts_pg_inherits];
2512
* No pg_inherits row exists, and we want a parent for this index,
2515
values[Anum_pg_inherits_inhrelid - 1] = ObjectIdGetDatum(partRelid);
2516
values[Anum_pg_inherits_inhparent - 1] =
2517
ObjectIdGetDatum(parentOid);
2518
values[Anum_pg_inherits_inhseqno - 1] = Int32GetDatum(1);
2519
memset(isnull, false, sizeof(isnull));
2521
tuple = heap_form_tuple(RelationGetDescr(pg_inherits),
2523
CatalogTupleInsert(pg_inherits, tuple);
2525
fix_dependencies = true;
2530
Form_pg_inherits inhForm = (Form_pg_inherits) GETSTRUCT(tuple);
2532
if (parentOid == InvalidOid)
2535
* There exists a pg_inherits row, which we want to clear; do so.
2537
CatalogTupleDelete(pg_inherits, &tuple->t_self);
2538
fix_dependencies = true;
2543
* A pg_inherits row exists. If it's the same we want, then we're
2544
* good; if it differs, that amounts to a corrupt catalog and
2545
* should not happen.
2547
if (inhForm->inhparent != parentOid)
2549
/* unexpected: we should not get called in this case */
2550
elog(ERROR, "bogus pg_inherit row: inhrelid %u inhparent %u",
2551
inhForm->inhrelid, inhForm->inhparent);
2554
/* already in the right state */
2555
fix_dependencies = false;
2559
/* done with pg_inherits */
2560
systable_endscan(scan);
2561
relation_close(pg_inherits, RowExclusiveLock);
2563
if (fix_dependencies)
2565
ObjectAddress partIdx;
2568
* Insert/delete pg_depend rows. If setting a parent, add an
2569
* INTERNAL_AUTO dependency to the parent index; if making standalone,
2570
* remove all existing rows and put back the regular dependency on the
2573
ObjectAddressSet(partIdx, RelationRelationId, partRelid);
2575
if (OidIsValid(parentOid))
2577
ObjectAddress parentIdx;
2579
ObjectAddressSet(parentIdx, RelationRelationId, parentOid);
2580
recordDependencyOn(&partIdx, &parentIdx, DEPENDENCY_INTERNAL_AUTO);
2584
ObjectAddress partitionTbl;
2586
ObjectAddressSet(partitionTbl, RelationRelationId,
2587
partitionIdx->rd_index->indrelid);
2589
deleteDependencyRecordsForClass(RelationRelationId, partRelid,
2591
DEPENDENCY_INTERNAL_AUTO);
2593
recordDependencyOn(&partIdx, &partitionTbl, DEPENDENCY_AUTO);
2596
/* make our updates visible */
2597
CommandCounterIncrement();