1 /*-------------------------------------------------------------------------
4 * POSTGRES define and remove index code.
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
13 *-------------------------------------------------------------------------
18 #include "access/genam.h"
19 #include "access/heapam.h"
20 #include "access/reloptions.h"
21 #include "access/transam.h"
22 #include "access/xact.h"
23 #include "catalog/catalog.h"
24 #include "catalog/heap.h"
25 #include "catalog/index.h"
26 #include "catalog/indexing.h"
27 #include "catalog/pg_opclass.h"
28 #include "catalog/pg_tablespace.h"
29 #include "commands/dbcommands.h"
30 #include "commands/defrem.h"
31 #include "commands/tablecmds.h"
32 #include "commands/tablespace.h"
33 #include "mb/pg_wchar.h"
34 #include "miscadmin.h"
35 #include "nodes/nodeFuncs.h"
36 #include "optimizer/clauses.h"
37 #include "parser/parse_coerce.h"
38 #include "parser/parse_func.h"
39 #include "parser/parsetree.h"
40 #include "storage/lmgr.h"
41 #include "storage/proc.h"
42 #include "storage/procarray.h"
43 #include "utils/acl.h"
44 #include "utils/builtins.h"
45 #include "utils/fmgroids.h"
46 #include "utils/inval.h"
47 #include "utils/lsyscache.h"
48 #include "utils/memutils.h"
49 #include "utils/relcache.h"
50 #include "utils/snapmgr.h"
51 #include "utils/syscache.h"
52 #include "utils/tqual.h"
55 /* non-export function prototypes */
56 static void CheckPredicate(Expr
*predicate
);
57 static void ComputeIndexAttrs(IndexInfo
*indexInfo
,
62 char *accessMethodName
, Oid accessMethodId
,
65 static Oid
GetIndexOpClass(List
*opclass
, Oid attrType
,
66 char *accessMethodName
, Oid accessMethodId
);
67 static bool relationHasPrimaryKey(Relation rel
);
72 * Creates a new index.
74 * 'heapRelation': the relation the index will apply to.
75 * 'indexRelationName': the name for the new index, or NULL to indicate
76 * that a nonconflicting default name should be picked.
77 * 'indexRelationId': normally InvalidOid, but during bootstrap can be
78 * nonzero to specify a preselected OID for the index.
79 * 'accessMethodName': name of the AM to use.
80 * 'tableSpaceName': name of the tablespace to create the index in.
81 * NULL specifies using the appropriate default.
82 * 'attributeList': a list of IndexElem specifying columns and expressions
84 * 'predicate': the partial-index condition, or NULL if none.
85 * 'options': reloptions from WITH (in list-of-DefElem form).
86 * 'unique': make the index enforce uniqueness.
87 * 'primary': mark the index as a primary key in the catalogs.
88 * 'isconstraint': index is for a PRIMARY KEY or UNIQUE constraint,
89 * so build a pg_constraint entry for it.
90 * 'is_alter_table': this is due to an ALTER rather than a CREATE operation.
91 * 'check_rights': check for CREATE rights in the namespace. (This should
92 * be true except when ALTER is deleting/recreating an index.)
93 * 'skip_build': make the catalog entries but leave the index file empty;
94 * it will be filled later.
95 * 'quiet': suppress the NOTICE chatter ordinarily provided for constraints.
96 * 'concurrent': avoid blocking writers to the table while building.
99 DefineIndex(RangeVar
*heapRelation
,
100 char *indexRelationName
,
102 char *accessMethodName
,
103 char *tableSpaceName
,
122 Relation indexRelation
;
124 Form_pg_am accessMethodForm
;
126 RegProcedure amoptions
;
129 IndexInfo
*indexInfo
;
130 int numberOfAttributes
;
131 VirtualTransactionId
*old_lockholders
;
132 VirtualTransactionId
*old_snapshots
;
137 HeapTuple indexTuple
;
138 Form_pg_index indexForm
;
141 * count attributes in index
143 numberOfAttributes
= list_length(attributeList
);
144 if (numberOfAttributes
<= 0)
146 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION
),
147 errmsg("must specify at least one column")));
148 if (numberOfAttributes
> INDEX_MAX_KEYS
)
150 (errcode(ERRCODE_TOO_MANY_COLUMNS
),
151 errmsg("cannot use more than %d columns in an index",
155 * Open heap relation, acquire a suitable lock on it, remember its OID
157 * Only SELECT ... FOR UPDATE/SHARE are allowed while doing a standard
158 * index build; but for concurrent builds we allow INSERT/UPDATE/DELETE
161 rel
= heap_openrv(heapRelation
,
162 (concurrent
? ShareUpdateExclusiveLock
: ShareLock
));
164 relationId
= RelationGetRelid(rel
);
165 namespaceId
= RelationGetNamespace(rel
);
167 /* Note: during bootstrap may see uncataloged relation */
168 if (rel
->rd_rel
->relkind
!= RELKIND_RELATION
&&
169 rel
->rd_rel
->relkind
!= RELKIND_UNCATALOGED
)
171 (errcode(ERRCODE_WRONG_OBJECT_TYPE
),
172 errmsg("\"%s\" is not a table",
173 heapRelation
->relname
)));
176 * Don't try to CREATE INDEX on temp tables of other backends.
178 if (isOtherTempNamespace(namespaceId
))
180 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
181 errmsg("cannot create indexes on temporary tables of other sessions")));
184 * Verify we (still) have CREATE rights in the rel's namespace.
185 * (Presumably we did when the rel was created, but maybe not anymore.)
186 * Skip check if caller doesn't want it. Also skip check if
187 * bootstrapping, since permissions machinery may not be working yet.
189 if (check_rights
&& !IsBootstrapProcessingMode())
193 aclresult
= pg_namespace_aclcheck(namespaceId
, GetUserId(),
195 if (aclresult
!= ACLCHECK_OK
)
196 aclcheck_error(aclresult
, ACL_KIND_NAMESPACE
,
197 get_namespace_name(namespaceId
));
201 * Select tablespace to use. If not specified, use default tablespace
202 * (which may in turn default to database's default).
206 tablespaceId
= get_tablespace_oid(tableSpaceName
);
207 if (!OidIsValid(tablespaceId
))
209 (errcode(ERRCODE_UNDEFINED_OBJECT
),
210 errmsg("tablespace \"%s\" does not exist",
215 tablespaceId
= GetDefaultTablespace(rel
->rd_istemp
);
216 /* note InvalidOid is OK in this case */
219 /* Check permissions except when using database's default */
220 if (OidIsValid(tablespaceId
) && tablespaceId
!= MyDatabaseTableSpace
)
224 aclresult
= pg_tablespace_aclcheck(tablespaceId
, GetUserId(),
226 if (aclresult
!= ACLCHECK_OK
)
227 aclcheck_error(aclresult
, ACL_KIND_TABLESPACE
,
228 get_tablespace_name(tablespaceId
));
232 * Force shared indexes into the pg_global tablespace. This is a bit of a
233 * hack but seems simpler than marking them in the BKI commands.
235 if (rel
->rd_rel
->relisshared
)
236 tablespaceId
= GLOBALTABLESPACE_OID
;
239 * Select name for index if caller didn't specify
241 if (indexRelationName
== NULL
)
244 indexRelationName
= ChooseRelationName(RelationGetRelationName(rel
),
250 IndexElem
*iparam
= (IndexElem
*) linitial(attributeList
);
252 indexRelationName
= ChooseRelationName(RelationGetRelationName(rel
),
260 * look up the access method, verify it can handle the requested features
262 tuple
= SearchSysCache(AMNAME
,
263 PointerGetDatum(accessMethodName
),
265 if (!HeapTupleIsValid(tuple
))
268 * Hack to provide more-or-less-transparent updating of old RTREE
269 * indexes to GIST: if RTREE is requested and not found, use GIST.
271 if (strcmp(accessMethodName
, "rtree") == 0)
274 (errmsg("substituting access method \"gist\" for obsolete method \"rtree\"")));
275 accessMethodName
= "gist";
276 tuple
= SearchSysCache(AMNAME
,
277 PointerGetDatum(accessMethodName
),
281 if (!HeapTupleIsValid(tuple
))
283 (errcode(ERRCODE_UNDEFINED_OBJECT
),
284 errmsg("access method \"%s\" does not exist",
287 accessMethodId
= HeapTupleGetOid(tuple
);
288 accessMethodForm
= (Form_pg_am
) GETSTRUCT(tuple
);
290 if (unique
&& !accessMethodForm
->amcanunique
)
292 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
293 errmsg("access method \"%s\" does not support unique indexes",
295 if (numberOfAttributes
> 1 && !accessMethodForm
->amcanmulticol
)
297 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
298 errmsg("access method \"%s\" does not support multicolumn indexes",
301 amcanorder
= accessMethodForm
->amcanorder
;
302 amoptions
= accessMethodForm
->amoptions
;
304 ReleaseSysCache(tuple
);
307 * Validate predicate, if given
310 CheckPredicate(predicate
);
313 * Extra checks when creating a PRIMARY KEY index.
321 * If ALTER TABLE, check that there isn't already a PRIMARY KEY. In
322 * CREATE TABLE, we have faith that the parser rejected multiple pkey
323 * clauses; and CREATE INDEX doesn't have a way to say PRIMARY KEY, so
324 * it's no problem either.
326 if (is_alter_table
&&
327 relationHasPrimaryKey(rel
))
330 (errcode(ERRCODE_INVALID_TABLE_DEFINITION
),
331 errmsg("multiple primary keys for table \"%s\" are not allowed",
332 RelationGetRelationName(rel
))));
336 * Check that all of the attributes in a primary key are marked as not
337 * null, otherwise attempt to ALTER TABLE .. SET NOT NULL
340 foreach(keys
, attributeList
)
342 IndexElem
*key
= (IndexElem
*) lfirst(keys
);
347 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
348 errmsg("primary keys cannot be expressions")));
350 /* System attributes are never null, so no problem */
351 if (SystemAttributeByName(key
->name
, rel
->rd_rel
->relhasoids
))
354 atttuple
= SearchSysCacheAttName(relationId
, key
->name
);
355 if (HeapTupleIsValid(atttuple
))
357 if (!((Form_pg_attribute
) GETSTRUCT(atttuple
))->attnotnull
)
359 /* Add a subcommand to make this one NOT NULL */
360 AlterTableCmd
*cmd
= makeNode(AlterTableCmd
);
362 cmd
->subtype
= AT_SetNotNull
;
363 cmd
->name
= key
->name
;
365 cmds
= lappend(cmds
, cmd
);
367 ReleaseSysCache(atttuple
);
372 * This shouldn't happen during CREATE TABLE, but can happen
373 * during ALTER TABLE. Keep message in sync with
374 * transformIndexConstraints() in parser/parse_utilcmd.c.
377 (errcode(ERRCODE_UNDEFINED_COLUMN
),
378 errmsg("column \"%s\" named in key does not exist",
384 * XXX: Shouldn't the ALTER TABLE .. SET NOT NULL cascade to child
385 * tables? Currently, since the PRIMARY KEY itself doesn't cascade,
386 * we don't cascade the notnull constraint(s) either; but this is
389 * XXX: possible future improvement: when being called from ALTER
390 * TABLE, it would be more efficient to merge this with the outer
391 * ALTER TABLE, so as to avoid two scans. But that seems to
392 * complicate DefineIndex's API unduly.
395 AlterTableInternal(relationId
, cmds
, false);
399 * Parse AM-specific options, convert to text array form, validate.
401 reloptions
= transformRelOptions((Datum
) 0, options
, false, false);
403 (void) index_reloptions(amoptions
, reloptions
, true);
406 * Prepare arguments for index_create, primarily an IndexInfo structure.
407 * Note that ii_Predicate must be in implicit-AND format.
409 indexInfo
= makeNode(IndexInfo
);
410 indexInfo
->ii_NumIndexAttrs
= numberOfAttributes
;
411 indexInfo
->ii_Expressions
= NIL
; /* for now */
412 indexInfo
->ii_ExpressionsState
= NIL
;
413 indexInfo
->ii_Predicate
= make_ands_implicit(predicate
);
414 indexInfo
->ii_PredicateState
= NIL
;
415 indexInfo
->ii_Unique
= unique
;
416 /* In a concurrent build, mark it not-ready-for-inserts */
417 indexInfo
->ii_ReadyForInserts
= !concurrent
;
418 indexInfo
->ii_Concurrent
= concurrent
;
419 indexInfo
->ii_BrokenHotChain
= false;
421 classObjectId
= (Oid
*) palloc(numberOfAttributes
* sizeof(Oid
));
422 coloptions
= (int16
*) palloc(numberOfAttributes
* sizeof(int16
));
423 ComputeIndexAttrs(indexInfo
, classObjectId
, coloptions
, attributeList
,
424 relationId
, accessMethodName
, accessMethodId
,
425 amcanorder
, isconstraint
);
428 * Report index creation if appropriate (delay this till after most of the
431 if (isconstraint
&& !quiet
)
433 (errmsg("%s %s will create implicit index \"%s\" for table \"%s\"",
434 is_alter_table
? "ALTER TABLE / ADD" : "CREATE TABLE /",
435 primary
? "PRIMARY KEY" : "UNIQUE",
436 indexRelationName
, RelationGetRelationName(rel
))));
438 /* save lockrelid and locktag for below, then close rel */
439 heaprelid
= rel
->rd_lockInfo
.lockRelId
;
440 SET_LOCKTAG_RELATION(heaplocktag
, heaprelid
.dbId
, heaprelid
.relId
);
441 heap_close(rel
, NoLock
);
446 index_create(relationId
, indexRelationName
, indexRelationId
,
447 indexInfo
, accessMethodId
, tablespaceId
, classObjectId
,
448 coloptions
, reloptions
, primary
, isconstraint
,
449 allowSystemTableMods
, skip_build
, concurrent
);
451 return; /* We're done, in the standard case */
455 * For a concurrent build, we next insert the catalog entry and add
456 * constraints. We don't build the index just yet; we must first make the
457 * catalog entry so that the new index is visible to updating
458 * transactions. That will prevent them from making incompatible HOT
459 * updates. The new index will be marked not indisready and not
460 * indisvalid, so that no one else tries to either insert into it or use
461 * it for queries. We pass skip_build = true to prevent the build.
464 index_create(relationId
, indexRelationName
, indexRelationId
,
465 indexInfo
, accessMethodId
, tablespaceId
, classObjectId
,
466 coloptions
, reloptions
, primary
, isconstraint
,
467 allowSystemTableMods
, true, concurrent
);
470 * We must commit our current transaction so that the index becomes
471 * visible; then start another. Note that all the data structures we just
472 * built are lost in the commit. The only data we keep past here are the
475 * Before committing, get a session-level lock on the table, to ensure
476 * that neither it nor the index can be dropped before we finish. This
477 * cannot block, even if someone else is waiting for access, because we
478 * already have the same lock within our transaction.
480 * Note: we don't currently bother with a session lock on the index,
481 * because there are no operations that could change its state while we
482 * hold lock on the parent table. This might need to change later.
484 LockRelationIdForSession(&heaprelid
, ShareUpdateExclusiveLock
);
487 CommitTransactionCommand();
488 StartTransactionCommand();
491 * Phase 2 of concurrent index build (see comments for validate_index()
492 * for an overview of how this works)
494 * Now we must wait until no running transaction could have the table open
495 * with the old list of indexes. To do this, inquire which xacts
496 * currently would conflict with ShareLock on the table -- ie, which ones
497 * have a lock that permits writing the table. Then wait for each of
498 * these xacts to commit or abort. Note we do not need to worry about
499 * xacts that open the table for writing after this point; they will see
500 * the new index when they open it.
502 * Note: the reason we use actual lock acquisition here, rather than just
503 * checking the ProcArray and sleeping, is that deadlock is possible if
504 * one of the transactions in question is blocked trying to acquire an
505 * exclusive lock on our table. The lock code will detect deadlock and
506 * error out properly.
508 * Note: GetLockConflicts() never reports our own xid, hence we need not
509 * check for that. Also, prepared xacts are not reported, which is fine
510 * since they certainly aren't going to do anything more.
512 old_lockholders
= GetLockConflicts(&heaplocktag
, ShareLock
);
514 while (VirtualTransactionIdIsValid(*old_lockholders
))
516 VirtualXactLockTableWait(*old_lockholders
);
521 * At this moment we are sure that there are no transactions with the
522 * table open for write that don't have this new index in their list of
523 * indexes. We have waited out all the existing transactions and any new
524 * transaction will have the new index in its list, but the index is still
525 * marked as "not-ready-for-inserts". The index is consulted while
526 * deciding HOT-safety though. This arrangement ensures that no new HOT
527 * chains can be created where the new tuple and the old tuple in the
528 * chain have different index keys.
530 * We now take a new snapshot, and build the index using all tuples that
531 * are visible in this snapshot. We can be sure that any HOT updates to
532 * these tuples will be compatible with the index, since any updates made
533 * by transactions that didn't know about the index are now committed or
534 * rolled back. Thus, each visible tuple is either the end of its
535 * HOT-chain or the extension of the chain is HOT-safe for this index.
538 /* Open and lock the parent heap relation */
539 rel
= heap_openrv(heapRelation
, ShareUpdateExclusiveLock
);
541 /* And the target index relation */
542 indexRelation
= index_open(indexRelationId
, RowExclusiveLock
);
544 /* Set ActiveSnapshot since functions in the indexes may need it */
545 PushActiveSnapshot(GetTransactionSnapshot());
547 /* We have to re-build the IndexInfo struct, since it was lost in commit */
548 indexInfo
= BuildIndexInfo(indexRelation
);
549 Assert(!indexInfo
->ii_ReadyForInserts
);
550 indexInfo
->ii_Concurrent
= true;
551 indexInfo
->ii_BrokenHotChain
= false;
553 /* Now build the index */
554 index_build(rel
, indexRelation
, indexInfo
, primary
);
556 /* Close both the relations, but keep the locks */
557 heap_close(rel
, NoLock
);
558 index_close(indexRelation
, NoLock
);
561 * Update the pg_index row to mark the index as ready for inserts. Once we
562 * commit this transaction, any new transactions that open the table must
563 * insert new entries into the index for insertions and non-HOT updates.
565 pg_index
= heap_open(IndexRelationId
, RowExclusiveLock
);
567 indexTuple
= SearchSysCacheCopy(INDEXRELID
,
568 ObjectIdGetDatum(indexRelationId
),
570 if (!HeapTupleIsValid(indexTuple
))
571 elog(ERROR
, "cache lookup failed for index %u", indexRelationId
);
572 indexForm
= (Form_pg_index
) GETSTRUCT(indexTuple
);
574 Assert(!indexForm
->indisready
);
575 Assert(!indexForm
->indisvalid
);
577 indexForm
->indisready
= true;
579 simple_heap_update(pg_index
, &indexTuple
->t_self
, indexTuple
);
580 CatalogUpdateIndexes(pg_index
, indexTuple
);
582 heap_close(pg_index
, RowExclusiveLock
);
584 /* we can do away with our snapshot */
588 * Commit this transaction to make the indisready update visible.
590 CommitTransactionCommand();
591 StartTransactionCommand();
594 * Phase 3 of concurrent index build
596 * We once again wait until no transaction can have the table open with
597 * the index marked as read-only for updates.
599 old_lockholders
= GetLockConflicts(&heaplocktag
, ShareLock
);
601 while (VirtualTransactionIdIsValid(*old_lockholders
))
603 VirtualXactLockTableWait(*old_lockholders
);
608 * Now take the "reference snapshot" that will be used by validate_index()
609 * to filter candidate tuples. Beware! There might still be snapshots in
610 * use that treat some transaction as in-progress that our reference
611 * snapshot treats as committed. If such a recently-committed transaction
612 * deleted tuples in the table, we will not include them in the index; yet
613 * those transactions which see the deleting one as still-in-progress will
614 * expect them to be there once we mark the index as valid.
616 * We solve this by waiting for all endangered transactions to exit before
617 * we mark the index as valid.
619 * We also set ActiveSnapshot to this snap, since functions in indexes may
622 snapshot
= RegisterSnapshot(GetTransactionSnapshot());
623 PushActiveSnapshot(snapshot
);
626 * Scan the index and the heap, insert any missing index entries.
628 validate_index(relationId
, indexRelationId
, snapshot
);
631 * The index is now valid in the sense that it contains all currently
632 * interesting tuples. But since it might not contain tuples deleted just
633 * before the reference snap was taken, we have to wait out any
634 * transactions that might have older snapshots. Obtain a list of VXIDs
635 * of such transactions, and wait for them individually.
637 * We can exclude any running transactions that have xmin >= the xmax of
638 * our reference snapshot, since they are clearly not interested in any
639 * missing older tuples. Transactions in other DBs aren't a problem
640 * either, since they'll never even be able to see this index.
642 * We can also exclude autovacuum processes and processes running manual
643 * lazy VACUUMs, because they won't be fazed by missing index entries
644 * either. (Manual ANALYZEs, however, can't be excluded because they
645 * might be within transactions that are going to do arbitrary operations
648 * Also, GetCurrentVirtualXIDs never reports our own vxid, so we need not
651 old_snapshots
= GetCurrentVirtualXIDs(snapshot
->xmax
, false,
652 PROC_IS_AUTOVACUUM
| PROC_IN_VACUUM
);
654 while (VirtualTransactionIdIsValid(*old_snapshots
))
656 VirtualXactLockTableWait(*old_snapshots
);
661 * Index can now be marked valid -- update its pg_index entry
663 pg_index
= heap_open(IndexRelationId
, RowExclusiveLock
);
665 indexTuple
= SearchSysCacheCopy(INDEXRELID
,
666 ObjectIdGetDatum(indexRelationId
),
668 if (!HeapTupleIsValid(indexTuple
))
669 elog(ERROR
, "cache lookup failed for index %u", indexRelationId
);
670 indexForm
= (Form_pg_index
) GETSTRUCT(indexTuple
);
672 Assert(indexForm
->indisready
);
673 Assert(!indexForm
->indisvalid
);
675 indexForm
->indisvalid
= true;
677 simple_heap_update(pg_index
, &indexTuple
->t_self
, indexTuple
);
678 CatalogUpdateIndexes(pg_index
, indexTuple
);
680 heap_close(pg_index
, RowExclusiveLock
);
683 * The pg_index update will cause backends (including this one) to update
684 * relcache entries for the index itself, but we should also send a
685 * relcache inval on the parent table to force replanning of cached plans.
686 * Otherwise existing sessions might fail to use the new index where it
687 * would be useful. (Note that our earlier commits did not create reasons
688 * to replan; relcache flush on the index itself was sufficient.)
690 CacheInvalidateRelcacheByRelid(heaprelid
.relId
);
692 /* we can now do away with our active snapshot */
695 /* And we can remove the validating snapshot too */
696 UnregisterSnapshot(snapshot
);
699 * Last thing to do is release the session-level lock on the parent table.
701 UnlockRelationIdForSession(&heaprelid
, ShareUpdateExclusiveLock
);
707 * Checks that the given partial-index predicate is valid.
709 * This used to also constrain the form of the predicate to forms that
710 * indxpath.c could do something with. However, that seems overly
711 * restrictive. One useful application of partial indexes is to apply
712 * a UNIQUE constraint across a subset of a table, and in that scenario
713 * any evaluatable predicate will work. So accept any predicate here
714 * (except ones requiring a plan), and let indxpath.c fend for itself.
717 CheckPredicate(Expr
*predicate
)
720 * We don't currently support generation of an actual query plan for a
721 * predicate, only simple scalar expressions; hence these restrictions.
723 if (contain_subplans((Node
*) predicate
))
725 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
726 errmsg("cannot use subquery in index predicate")));
727 if (contain_agg_clause((Node
*) predicate
))
729 (errcode(ERRCODE_GROUPING_ERROR
),
730 errmsg("cannot use aggregate in index predicate")));
733 * A predicate using mutable functions is probably wrong, for the same
734 * reasons that we don't allow an index expression to use one.
736 if (contain_mutable_functions((Node
*) predicate
))
738 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION
),
739 errmsg("functions in index predicate must be marked IMMUTABLE")));
743 * Compute per-index-column information, including indexed column numbers
744 * or index expressions, opclasses, and indoptions.
747 ComputeIndexAttrs(IndexInfo
*indexInfo
,
750 List
*attList
, /* list of IndexElem's */
752 char *accessMethodName
,
761 * process attributeList
763 foreach(rest
, attList
)
765 IndexElem
*attribute
= (IndexElem
*) lfirst(rest
);
769 * Process the column-or-expression to be indexed.
771 if (attribute
->name
!= NULL
)
773 /* Simple index attribute */
775 Form_pg_attribute attform
;
777 Assert(attribute
->expr
== NULL
);
778 atttuple
= SearchSysCacheAttName(relId
, attribute
->name
);
779 if (!HeapTupleIsValid(atttuple
))
781 /* difference in error message spellings is historical */
784 (errcode(ERRCODE_UNDEFINED_COLUMN
),
785 errmsg("column \"%s\" named in key does not exist",
789 (errcode(ERRCODE_UNDEFINED_COLUMN
),
790 errmsg("column \"%s\" does not exist",
793 attform
= (Form_pg_attribute
) GETSTRUCT(atttuple
);
794 indexInfo
->ii_KeyAttrNumbers
[attn
] = attform
->attnum
;
795 atttype
= attform
->atttypid
;
796 ReleaseSysCache(atttuple
);
798 else if (attribute
->expr
&& IsA(attribute
->expr
, Var
))
800 /* Tricky tricky, he wrote (column) ... treat as simple attr */
801 Var
*var
= (Var
*) attribute
->expr
;
803 indexInfo
->ii_KeyAttrNumbers
[attn
] = var
->varattno
;
804 atttype
= get_atttype(relId
, var
->varattno
);
808 /* Index expression */
809 Assert(attribute
->expr
!= NULL
);
810 indexInfo
->ii_KeyAttrNumbers
[attn
] = 0; /* marks expression */
811 indexInfo
->ii_Expressions
= lappend(indexInfo
->ii_Expressions
,
813 atttype
= exprType(attribute
->expr
);
816 * We don't currently support generation of an actual query plan
817 * for an index expression, only simple scalar expressions; hence
818 * these restrictions.
820 if (contain_subplans(attribute
->expr
))
822 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
823 errmsg("cannot use subquery in index expression")));
824 if (contain_agg_clause(attribute
->expr
))
826 (errcode(ERRCODE_GROUPING_ERROR
),
827 errmsg("cannot use aggregate function in index expression")));
830 * A expression using mutable functions is probably wrong, since
831 * if you aren't going to get the same result for the same data
832 * every time, it's not clear what the index entries mean at all.
834 if (contain_mutable_functions(attribute
->expr
))
836 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION
),
837 errmsg("functions in index expression must be marked IMMUTABLE")));
841 * Identify the opclass to use.
843 classOidP
[attn
] = GetIndexOpClass(attribute
->opclass
,
849 * Set up the per-column options (indoption field). For now, this is
850 * zero for any un-ordered index, while ordered indexes have DESC and
851 * NULLS FIRST/LAST options.
853 colOptionP
[attn
] = 0;
856 /* default ordering is ASC */
857 if (attribute
->ordering
== SORTBY_DESC
)
858 colOptionP
[attn
] |= INDOPTION_DESC
;
859 /* default null ordering is LAST for ASC, FIRST for DESC */
860 if (attribute
->nulls_ordering
== SORTBY_NULLS_DEFAULT
)
862 if (attribute
->ordering
== SORTBY_DESC
)
863 colOptionP
[attn
] |= INDOPTION_NULLS_FIRST
;
865 else if (attribute
->nulls_ordering
== SORTBY_NULLS_FIRST
)
866 colOptionP
[attn
] |= INDOPTION_NULLS_FIRST
;
870 /* index AM does not support ordering */
871 if (attribute
->ordering
!= SORTBY_DEFAULT
)
873 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
874 errmsg("access method \"%s\" does not support ASC/DESC options",
876 if (attribute
->nulls_ordering
!= SORTBY_NULLS_DEFAULT
)
878 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
879 errmsg("access method \"%s\" does not support NULLS FIRST/LAST options",
888 * Resolve possibly-defaulted operator class specification
891 GetIndexOpClass(List
*opclass
, Oid attrType
,
892 char *accessMethodName
, Oid accessMethodId
)
901 * Release 7.0 removed network_ops, timespan_ops, and datetime_ops, so we
902 * ignore those opclass names so the default *_ops is used. This can be
903 * removed in some later release. bjm 2000/02/07
905 * Release 7.1 removes lztext_ops, so suppress that too for a while. tgl
908 * Release 7.2 renames timestamp_ops to timestamptz_ops, so suppress that
909 * too for awhile. I'm starting to think we need a better approach. tgl
912 * Release 8.0 removes bigbox_ops (which was dead code for a long while
913 * anyway). tgl 2003/11/11
915 if (list_length(opclass
) == 1)
917 char *claname
= strVal(linitial(opclass
));
919 if (strcmp(claname
, "network_ops") == 0 ||
920 strcmp(claname
, "timespan_ops") == 0 ||
921 strcmp(claname
, "datetime_ops") == 0 ||
922 strcmp(claname
, "lztext_ops") == 0 ||
923 strcmp(claname
, "timestamp_ops") == 0 ||
924 strcmp(claname
, "bigbox_ops") == 0)
930 /* no operator class specified, so find the default */
931 opClassId
= GetDefaultOpClass(attrType
, accessMethodId
);
932 if (!OidIsValid(opClassId
))
934 (errcode(ERRCODE_UNDEFINED_OBJECT
),
935 errmsg("data type %s has no default operator class for access method \"%s\"",
936 format_type_be(attrType
), accessMethodName
),
937 errhint("You must specify an operator class for the index or define a default operator class for the data type.")));
942 * Specific opclass name given, so look up the opclass.
945 /* deconstruct the name list */
946 DeconstructQualifiedName(opclass
, &schemaname
, &opcname
);
950 /* Look in specific schema only */
953 namespaceId
= LookupExplicitNamespace(schemaname
);
954 tuple
= SearchSysCache(CLAAMNAMENSP
,
955 ObjectIdGetDatum(accessMethodId
),
956 PointerGetDatum(opcname
),
957 ObjectIdGetDatum(namespaceId
),
962 /* Unqualified opclass name, so search the search path */
963 opClassId
= OpclassnameGetOpcid(accessMethodId
, opcname
);
964 if (!OidIsValid(opClassId
))
966 (errcode(ERRCODE_UNDEFINED_OBJECT
),
967 errmsg("operator class \"%s\" does not exist for access method \"%s\"",
968 opcname
, accessMethodName
)));
969 tuple
= SearchSysCache(CLAOID
,
970 ObjectIdGetDatum(opClassId
),
974 if (!HeapTupleIsValid(tuple
))
976 (errcode(ERRCODE_UNDEFINED_OBJECT
),
977 errmsg("operator class \"%s\" does not exist for access method \"%s\"",
978 NameListToString(opclass
), accessMethodName
)));
981 * Verify that the index operator class accepts this datatype. Note we
982 * will accept binary compatibility.
984 opClassId
= HeapTupleGetOid(tuple
);
985 opInputType
= ((Form_pg_opclass
) GETSTRUCT(tuple
))->opcintype
;
987 if (!IsBinaryCoercible(attrType
, opInputType
))
989 (errcode(ERRCODE_DATATYPE_MISMATCH
),
990 errmsg("operator class \"%s\" does not accept data type %s",
991 NameListToString(opclass
), format_type_be(attrType
))));
993 ReleaseSysCache(tuple
);
1001 * Given the OIDs of a datatype and an access method, find the default
1002 * operator class, if any. Returns InvalidOid if there is none.
1005 GetDefaultOpClass(Oid type_id
, Oid am_id
)
1007 Oid result
= InvalidOid
;
1009 int ncompatible
= 0;
1010 int ncompatiblepreferred
= 0;
1012 ScanKeyData skey
[1];
1015 TYPCATEGORY tcategory
;
1017 /* If it's a domain, look at the base type instead */
1018 type_id
= getBaseType(type_id
);
1020 tcategory
= TypeCategory(type_id
);
1023 * We scan through all the opclasses available for the access method,
1024 * looking for one that is marked default and matches the target type
1025 * (either exactly or binary-compatibly, but prefer an exact match).
1027 * We could find more than one binary-compatible match. If just one is
1028 * for a preferred type, use that one; otherwise we fail, forcing the user
1029 * to specify which one he wants. (The preferred-type special case is a
1030 * kluge for varchar: it's binary-compatible to both text and bpchar, so
1031 * we need a tiebreaker.) If we find more than one exact match, then
1032 * someone put bogus entries in pg_opclass.
1034 rel
= heap_open(OperatorClassRelationId
, AccessShareLock
);
1036 ScanKeyInit(&skey
[0],
1037 Anum_pg_opclass_opcmethod
,
1038 BTEqualStrategyNumber
, F_OIDEQ
,
1039 ObjectIdGetDatum(am_id
));
1041 scan
= systable_beginscan(rel
, OpclassAmNameNspIndexId
, true,
1042 SnapshotNow
, 1, skey
);
1044 while (HeapTupleIsValid(tup
= systable_getnext(scan
)))
1046 Form_pg_opclass opclass
= (Form_pg_opclass
) GETSTRUCT(tup
);
1048 /* ignore altogether if not a default opclass */
1049 if (!opclass
->opcdefault
)
1051 if (opclass
->opcintype
== type_id
)
1054 result
= HeapTupleGetOid(tup
);
1056 else if (nexact
== 0 &&
1057 IsBinaryCoercible(type_id
, opclass
->opcintype
))
1059 if (IsPreferredType(tcategory
, opclass
->opcintype
))
1061 ncompatiblepreferred
++;
1062 result
= HeapTupleGetOid(tup
);
1064 else if (ncompatiblepreferred
== 0)
1067 result
= HeapTupleGetOid(tup
);
1072 systable_endscan(scan
);
1074 heap_close(rel
, AccessShareLock
);
1076 /* raise error if pg_opclass contains inconsistent data */
1079 (errcode(ERRCODE_DUPLICATE_OBJECT
),
1080 errmsg("there are multiple default operator classes for data type %s",
1081 format_type_be(type_id
))));
1084 ncompatiblepreferred
== 1 ||
1085 (ncompatiblepreferred
== 0 && ncompatible
== 1))
1094 * Create a name for an implicitly created index, sequence, constraint, etc.
1096 * The parameters are typically: the original table name, the original field
1097 * name, and a "type" string (such as "seq" or "pkey"). The field name
1098 * and/or type can be NULL if not relevant.
1100 * The result is a palloc'd string.
1102 * The basic result we want is "name1_name2_label", omitting "_name2" or
1103 * "_label" when those parameters are NULL. However, we must generate
1104 * a name with less than NAMEDATALEN characters! So, we truncate one or
1105 * both names if necessary to make a short-enough string. The label part
1106 * is never truncated (so it had better be reasonably short).
1108 * The caller is responsible for checking uniqueness of the generated
1109 * name and retrying as needed; retrying will be done by altering the
1110 * "label" string (which is why we never truncate that part).
1113 makeObjectName(const char *name1
, const char *name2
, const char *label
)
1116 int overhead
= 0; /* chars needed for label and underscores */
1117 int availchars
; /* chars available for name(s) */
1118 int name1chars
; /* chars allocated to name1 */
1119 int name2chars
; /* chars allocated to name2 */
1122 name1chars
= strlen(name1
);
1125 name2chars
= strlen(name2
);
1126 overhead
++; /* allow for separating underscore */
1131 overhead
+= strlen(label
) + 1;
1133 availchars
= NAMEDATALEN
- 1 - overhead
;
1134 Assert(availchars
> 0); /* else caller chose a bad label */
1137 * If we must truncate, preferentially truncate the longer name. This
1138 * logic could be expressed without a loop, but it's simple and obvious as
1141 while (name1chars
+ name2chars
> availchars
)
1143 if (name1chars
> name2chars
)
1149 name1chars
= pg_mbcliplen(name1
, name1chars
, name1chars
);
1151 name2chars
= pg_mbcliplen(name2
, name2chars
, name2chars
);
1153 /* Now construct the string using the chosen lengths */
1154 name
= palloc(name1chars
+ name2chars
+ overhead
+ 1);
1155 memcpy(name
, name1
, name1chars
);
1160 memcpy(name
+ ndx
, name2
, name2chars
);
1166 strcpy(name
+ ndx
, label
);
1175 * Select a nonconflicting name for a new relation. This is ordinarily
1176 * used to choose index names (which is why it's here) but it can also
1177 * be used for sequences, or any autogenerated relation kind.
1179 * name1, name2, and label are used the same way as for makeObjectName(),
1180 * except that the label can't be NULL; digits will be appended to the label
1181 * if needed to create a name that is unique within the specified namespace.
1183 * Note: it is theoretically possible to get a collision anyway, if someone
1184 * else chooses the same name concurrently. This is fairly unlikely to be
1185 * a problem in practice, especially if one is holding an exclusive lock on
1186 * the relation identified by name1. However, if choosing multiple names
1187 * within a single command, you'd better create the new object and do
1188 * CommandCounterIncrement before choosing the next one!
1190 * Returns a palloc'd string.
1193 ChooseRelationName(const char *name1
, const char *name2
,
1194 const char *label
, Oid
namespace)
1197 char *relname
= NULL
;
1198 char modlabel
[NAMEDATALEN
];
1200 /* try the unmodified label first */
1201 StrNCpy(modlabel
, label
, sizeof(modlabel
));
1205 relname
= makeObjectName(name1
, name2
, modlabel
);
1207 if (!OidIsValid(get_relname_relid(relname
, namespace)))
1210 /* found a conflict, so try a new name component */
1212 snprintf(modlabel
, sizeof(modlabel
), "%s%d", label
, ++pass
);
1219 * relationHasPrimaryKey -
1221 * See whether an existing relation has a primary key.
1224 relationHasPrimaryKey(Relation rel
)
1226 bool result
= false;
1228 ListCell
*indexoidscan
;
1231 * Get the list of index OIDs for the table from the relcache, and look up
1232 * each one in the pg_index syscache until we find one marked primary key
1233 * (hopefully there isn't more than one such).
1235 indexoidlist
= RelationGetIndexList(rel
);
1237 foreach(indexoidscan
, indexoidlist
)
1239 Oid indexoid
= lfirst_oid(indexoidscan
);
1240 HeapTuple indexTuple
;
1242 indexTuple
= SearchSysCache(INDEXRELID
,
1243 ObjectIdGetDatum(indexoid
),
1245 if (!HeapTupleIsValid(indexTuple
)) /* should not happen */
1246 elog(ERROR
, "cache lookup failed for index %u", indexoid
);
1247 result
= ((Form_pg_index
) GETSTRUCT(indexTuple
))->indisprimary
;
1248 ReleaseSysCache(indexTuple
);
1253 list_free(indexoidlist
);
1260 * Recreate a specific index.
1263 ReindexIndex(RangeVar
*indexRelation
)
1268 indOid
= RangeVarGetRelid(indexRelation
, false);
1269 tuple
= SearchSysCache(RELOID
,
1270 ObjectIdGetDatum(indOid
),
1272 if (!HeapTupleIsValid(tuple
)) /* shouldn't happen */
1273 elog(ERROR
, "cache lookup failed for relation %u", indOid
);
1275 if (((Form_pg_class
) GETSTRUCT(tuple
))->relkind
!= RELKIND_INDEX
)
1277 (errcode(ERRCODE_WRONG_OBJECT_TYPE
),
1278 errmsg("\"%s\" is not an index",
1279 indexRelation
->relname
)));
1281 /* Check permissions */
1282 if (!pg_class_ownercheck(indOid
, GetUserId()))
1283 aclcheck_error(ACLCHECK_NOT_OWNER
, ACL_KIND_CLASS
,
1284 indexRelation
->relname
);
1286 ReleaseSysCache(tuple
);
1288 reindex_index(indOid
);
1293 * Recreate all indexes of a table (and of its toast table, if any)
1296 ReindexTable(RangeVar
*relation
)
1301 heapOid
= RangeVarGetRelid(relation
, false);
1302 tuple
= SearchSysCache(RELOID
,
1303 ObjectIdGetDatum(heapOid
),
1305 if (!HeapTupleIsValid(tuple
)) /* shouldn't happen */
1306 elog(ERROR
, "cache lookup failed for relation %u", heapOid
);
1308 if (((Form_pg_class
) GETSTRUCT(tuple
))->relkind
!= RELKIND_RELATION
&&
1309 ((Form_pg_class
) GETSTRUCT(tuple
))->relkind
!= RELKIND_TOASTVALUE
)
1311 (errcode(ERRCODE_WRONG_OBJECT_TYPE
),
1312 errmsg("\"%s\" is not a table",
1313 relation
->relname
)));
1315 /* Check permissions */
1316 if (!pg_class_ownercheck(heapOid
, GetUserId()))
1317 aclcheck_error(ACLCHECK_NOT_OWNER
, ACL_KIND_CLASS
,
1320 /* Can't reindex shared tables except in standalone mode */
1321 if (((Form_pg_class
) GETSTRUCT(tuple
))->relisshared
&& IsUnderPostmaster
)
1323 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE
),
1324 errmsg("shared table \"%s\" can only be reindexed in stand-alone mode",
1325 relation
->relname
)));
1327 ReleaseSysCache(tuple
);
1329 if (!reindex_relation(heapOid
, true))
1331 (errmsg("table \"%s\" has no indexes",
1332 relation
->relname
)));
1337 * Recreate indexes of a database.
1339 * To reduce the probability of deadlocks, each table is reindexed in a
1340 * separate transaction, so we can release the lock on it right away.
1341 * That means this must not be called within a user transaction block!
1344 ReindexDatabase(const char *databaseName
, bool do_system
, bool do_user
)
1346 Relation relationRelation
;
1349 MemoryContext private_context
;
1354 AssertArg(databaseName
);
1356 if (strcmp(databaseName
, get_database_name(MyDatabaseId
)) != 0)
1358 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
1359 errmsg("can only reindex the currently open database")));
1361 if (!pg_database_ownercheck(MyDatabaseId
, GetUserId()))
1362 aclcheck_error(ACLCHECK_NOT_OWNER
, ACL_KIND_DATABASE
,
1366 * Create a memory context that will survive forced transaction commits we
1367 * do below. Since it is a child of PortalContext, it will go away
1368 * eventually even if we suffer an error; there's no need for special
1369 * abort cleanup logic.
1371 private_context
= AllocSetContextCreate(PortalContext
,
1373 ALLOCSET_DEFAULT_MINSIZE
,
1374 ALLOCSET_DEFAULT_INITSIZE
,
1375 ALLOCSET_DEFAULT_MAXSIZE
);
1378 * We always want to reindex pg_class first. This ensures that if there
1379 * is any corruption in pg_class' indexes, they will be fixed before we
1380 * process any other tables. This is critical because reindexing itself
1381 * will try to update pg_class.
1385 old
= MemoryContextSwitchTo(private_context
);
1386 relids
= lappend_oid(relids
, RelationRelationId
);
1387 MemoryContextSwitchTo(old
);
1391 * Scan pg_class to build a list of the relations we need to reindex.
1393 * We only consider plain relations here (toast rels will be processed
1394 * indirectly by reindex_relation).
1396 relationRelation
= heap_open(RelationRelationId
, AccessShareLock
);
1397 scan
= heap_beginscan(relationRelation
, SnapshotNow
, 0, NULL
);
1398 while ((tuple
= heap_getnext(scan
, ForwardScanDirection
)) != NULL
)
1400 Form_pg_class classtuple
= (Form_pg_class
) GETSTRUCT(tuple
);
1402 if (classtuple
->relkind
!= RELKIND_RELATION
)
1405 /* Skip temp tables of other backends; we can't reindex them at all */
1406 if (isOtherTempNamespace(classtuple
->relnamespace
))
1409 /* Check user/system classification, and optionally skip */
1410 if (IsSystemClass(classtuple
))
1421 if (IsUnderPostmaster
) /* silently ignore shared tables */
1423 if (classtuple
->relisshared
)
1427 if (HeapTupleGetOid(tuple
) == RelationRelationId
)
1428 continue; /* got it already */
1430 old
= MemoryContextSwitchTo(private_context
);
1431 relids
= lappend_oid(relids
, HeapTupleGetOid(tuple
));
1432 MemoryContextSwitchTo(old
);
1435 heap_close(relationRelation
, AccessShareLock
);
1437 /* Now reindex each rel in a separate transaction */
1438 PopActiveSnapshot();
1439 CommitTransactionCommand();
1442 Oid relid
= lfirst_oid(l
);
1444 StartTransactionCommand();
1445 /* functions in indexes may want a snapshot set */
1446 PushActiveSnapshot(GetTransactionSnapshot());
1447 if (reindex_relation(relid
, true))
1449 (errmsg("table \"%s\" was reindexed",
1450 get_rel_name(relid
))));
1451 PopActiveSnapshot();
1452 CommitTransactionCommand();
1454 StartTransactionCommand();
1456 MemoryContextDelete(private_context
);