1 /*-------------------------------------------------------------------------
4 * POSTGRES define and remove index code.
6 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/commands/indexcmds.c,v 1.96 2003/01/02 19:29:22 tgl Exp $
13 *-------------------------------------------------------------------------
18 #include "access/heapam.h"
19 #include "catalog/catalog.h"
20 #include "catalog/catname.h"
21 #include "catalog/dependency.h"
22 #include "catalog/heap.h"
23 #include "catalog/index.h"
24 #include "catalog/namespace.h"
25 #include "catalog/pg_opclass.h"
26 #include "catalog/pg_proc.h"
27 #include "commands/defrem.h"
28 #include "commands/tablecmds.h"
29 #include "executor/executor.h"
30 #include "miscadmin.h"
31 #include "optimizer/clauses.h"
32 #include "optimizer/prep.h"
33 #include "parser/parsetree.h"
34 #include "parser/parse_coerce.h"
35 #include "parser/parse_func.h"
36 #include "utils/acl.h"
37 #include "utils/builtins.h"
38 #include "utils/lsyscache.h"
39 #include "utils/syscache.h"
42 #define IsFuncIndex(ATTR_LIST) (((IndexElem*)lfirst(ATTR_LIST))->funcname != NIL)
44 /* non-export function prototypes */
45 static void CheckPredicate(List *predList, List *rangeTable, Oid baseRelOid);
46 static void FuncIndexArgs(IndexInfo *indexInfo, Oid *classOidP,
49 char *accessMethodName, Oid accessMethodId);
50 static void NormIndexAttrs(IndexInfo *indexInfo, Oid *classOidP,
53 char *accessMethodName, Oid accessMethodId);
54 static Oid GetAttrOpClass(IndexElem *attribute, Oid attrType,
55 char *accessMethodName, Oid accessMethodId);
56 static Oid GetDefaultOpClass(Oid attrType, Oid accessMethodId);
60 * Creates a new index.
62 * 'attributeList' is a list of IndexElem specifying either a functional
63 * index or a list of attributes to index on.
64 * 'predicate' is the qual specified in the where clause.
65 * 'rangetable' is needed to interpret the predicate.
68 DefineIndex(RangeVar *heapRelation,
69 char *indexRelationName,
70 char *accessMethodName,
84 Form_pg_am accessMethodForm;
86 int numberOfAttributes;
90 * count attributes in index
92 numberOfAttributes = length(attributeList);
93 if (numberOfAttributes <= 0)
94 elog(ERROR, "DefineIndex: must specify at least one attribute");
95 if (numberOfAttributes > INDEX_MAX_KEYS)
96 elog(ERROR, "Cannot use more than %d attributes in an index",
100 * Open heap relation, acquire a suitable lock on it, remember its OID
102 rel = heap_openrv(heapRelation, ShareLock);
104 /* Note: during bootstrap may see uncataloged relation */
105 if (rel->rd_rel->relkind != RELKIND_RELATION &&
106 rel->rd_rel->relkind != RELKIND_UNCATALOGED)
107 elog(ERROR, "DefineIndex: relation \"%s\" is not a table",
108 heapRelation->relname);
110 relationId = RelationGetRelid(rel);
111 namespaceId = RelationGetNamespace(rel);
113 if (!IsBootstrapProcessingMode() &&
114 IsSystemRelation(rel) &&
115 !IndexesAreActive(rel))
116 elog(ERROR, "Existing indexes are inactive. REINDEX first");
118 heap_close(rel, NoLock);
121 * Verify we (still) have CREATE rights in the rel's namespace.
122 * (Presumably we did when the rel was created, but maybe not
123 * anymore.) Skip check if bootstrapping, since permissions machinery
124 * may not be working yet.
126 if (!IsBootstrapProcessingMode())
130 aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(),
132 if (aclresult != ACLCHECK_OK)
133 aclcheck_error(aclresult, get_namespace_name(namespaceId));
137 * look up the access method, verify it can handle the requested
140 tuple = SearchSysCache(AMNAME,
141 PointerGetDatum(accessMethodName),
143 if (!HeapTupleIsValid(tuple))
144 elog(ERROR, "DefineIndex: access method \"%s\" not found",
146 accessMethodId = HeapTupleGetOid(tuple);
147 accessMethodForm = (Form_pg_am) GETSTRUCT(tuple);
149 if (unique && !accessMethodForm->amcanunique)
150 elog(ERROR, "DefineIndex: access method \"%s\" does not support UNIQUE indexes",
152 if (numberOfAttributes > 1 && !accessMethodForm->amcanmulticol)
153 elog(ERROR, "DefineIndex: access method \"%s\" does not support multi-column indexes",
156 ReleaseSysCache(tuple);
159 * Convert the partial-index predicate from parsetree form to an
160 * implicit-AND qual expression, for easier evaluation at runtime.
161 * While we are at it, we reduce it to a canonical (CNF or DNF) form
162 * to simplify the task of proving implications.
166 cnfPred = canonicalize_qual((Expr *) copyObject(predicate), true);
167 CheckPredicate(cnfPred, rangetable, relationId);
171 * Check that all of the attributes in a primary key are marked
172 * as not null, otherwise attempt to ALTER TABLE .. SET NOT NULL
174 if (primary && !IsFuncIndex(attributeList))
178 foreach(keys, attributeList)
180 IndexElem *key = (IndexElem *) lfirst(keys);
183 /* System attributes are never null, so no problem */
184 if (SystemAttributeByName(key->name, rel->rd_rel->relhasoids))
187 atttuple = SearchSysCacheAttName(relationId, key->name);
188 if (HeapTupleIsValid(atttuple))
190 if (! ((Form_pg_attribute) GETSTRUCT(atttuple))->attnotnull)
193 * Try to make it NOT NULL.
195 * XXX: Shouldn't the ALTER TABLE .. SET NOT NULL cascade
196 * to child tables? Currently, since the PRIMARY KEY
197 * itself doesn't cascade, we don't cascade the notnull
198 * constraint either; but this is pretty debatable.
200 AlterTableAlterColumnSetNotNull(relationId, false,
203 ReleaseSysCache(atttuple);
207 /* This shouldn't happen if parser did its job ... */
208 elog(ERROR, "DefineIndex: column \"%s\" named in key does not exist",
215 * Prepare arguments for index_create, primarily an IndexInfo
218 indexInfo = makeNode(IndexInfo);
219 indexInfo->ii_Predicate = cnfPred;
220 indexInfo->ii_PredicateState = NIL;
221 indexInfo->ii_FuncOid = InvalidOid;
222 indexInfo->ii_Unique = unique;
224 if (IsFuncIndex(attributeList))
226 IndexElem *funcIndex = (IndexElem *) lfirst(attributeList);
229 /* Parser should have given us only one list item, but check */
230 if (numberOfAttributes != 1)
231 elog(ERROR, "Functional index can only have one attribute");
233 nargs = length(funcIndex->args);
234 if (nargs > INDEX_MAX_KEYS)
235 elog(ERROR, "Index function can take at most %d arguments",
238 indexInfo->ii_NumIndexAttrs = 1;
239 indexInfo->ii_NumKeyAttrs = nargs;
241 classObjectId = (Oid *) palloc(sizeof(Oid));
243 FuncIndexArgs(indexInfo, classObjectId, funcIndex,
244 relationId, accessMethodName, accessMethodId);
248 indexInfo->ii_NumIndexAttrs = numberOfAttributes;
249 indexInfo->ii_NumKeyAttrs = numberOfAttributes;
251 classObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
253 NormIndexAttrs(indexInfo, classObjectId, attributeList,
254 relationId, accessMethodName, accessMethodId);
257 index_create(relationId, indexRelationName,
258 indexInfo, accessMethodId, classObjectId,
259 primary, isconstraint, allowSystemTableMods);
262 * We update the relation's pg_class tuple even if it already has
263 * relhasindex = true. This is needed to cause a shared-cache-inval
264 * message to be sent for the pg_class tuple, which will cause other
265 * backends to flush their relcache entries and in particular their
266 * cached lists of the indexes for this relation.
268 setRelhasindex(relationId, true, primary, InvalidOid);
274 * Checks that the given list of partial-index predicates refer
275 * (via the given range table) only to the given base relation oid.
277 * This used to also constrain the form of the predicate to forms that
278 * indxpath.c could do something with. However, that seems overly
279 * restrictive. One useful application of partial indexes is to apply
280 * a UNIQUE constraint across a subset of a table, and in that scenario
281 * any evaluatable predicate will work. So accept any predicate here
282 * (except ones requiring a plan), and let indxpath.c fend for itself.
286 CheckPredicate(List *predList, List *rangeTable, Oid baseRelOid)
288 if (length(rangeTable) != 1 || getrelid(1, rangeTable) != baseRelOid)
290 "Partial-index predicates may refer only to the base relation");
293 * We don't currently support generation of an actual query plan for a
294 * predicate, only simple scalar expressions; hence these
297 if (contain_subplans((Node *) predList))
298 elog(ERROR, "Cannot use subselect in index predicate");
299 if (contain_agg_clause((Node *) predList))
300 elog(ERROR, "Cannot use aggregate in index predicate");
303 * A predicate using mutable functions is probably wrong, for the same
304 * reasons that we don't allow a functional index to use one.
306 if (contain_mutable_functions((Node *) predList))
307 elog(ERROR, "Functions in index predicate must be marked isImmutable");
312 FuncIndexArgs(IndexInfo *indexInfo,
314 IndexElem *funcIndex,
316 char *accessMethodName,
319 Oid argTypes[FUNC_MAX_ARGS];
323 FuncDetailCode fdresult;
330 * process the function arguments, which are a list of T_String
331 * (someday ought to allow more general expressions?)
333 * Note caller already checked that list is not too long.
335 MemSet(argTypes, 0, sizeof(argTypes));
337 foreach(arglist, funcIndex->args)
339 char *arg = strVal(lfirst(arglist));
341 Form_pg_attribute att;
343 tuple = SearchSysCacheAttName(relId, arg);
344 if (!HeapTupleIsValid(tuple))
345 elog(ERROR, "DefineIndex: column \"%s\" named in key does not exist", arg);
346 att = (Form_pg_attribute) GETSTRUCT(tuple);
347 indexInfo->ii_KeyAttrNumbers[nargs] = att->attnum;
348 argTypes[nargs] = att->atttypid;
349 ReleaseSysCache(tuple);
354 * Lookup the function procedure to get its OID and result type.
356 * We rely on parse_func.c to find the correct function in the possible
357 * presence of binary-compatible types. However, parse_func may do
358 * too much: it will accept a function that requires run-time coercion
359 * of input types, and the executor is not currently set up to support
360 * that. So, check to make sure that the selected function has
361 * exact-match or binary-compatible input types.
363 fdresult = func_get_detail(funcIndex->funcname, funcIndex->args,
365 &funcid, &rettype, &retset,
367 if (fdresult != FUNCDETAIL_NORMAL)
369 if (fdresult == FUNCDETAIL_AGGREGATE)
370 elog(ERROR, "DefineIndex: functional index may not use an aggregate function");
371 else if (fdresult == FUNCDETAIL_COERCION)
372 elog(ERROR, "DefineIndex: functional index must use a real function, not a type coercion"
373 "\n\tTry specifying the index opclass you want to use, instead");
375 func_error("DefineIndex", funcIndex->funcname, nargs, argTypes,
380 elog(ERROR, "DefineIndex: cannot index on a function returning a set");
382 for (i = 0; i < nargs; i++)
384 if (!IsBinaryCoercible(argTypes[i], true_typeids[i]))
385 func_error("DefineIndex", funcIndex->funcname, nargs, true_typeids,
386 "Index function must be binary-compatible with table datatype");
390 * Require that the function be marked immutable. Using a mutable
391 * function for a functional index is highly questionable, since if
392 * you aren't going to get the same result for the same data every
393 * time, it's not clear what the index entries mean at all.
395 if (func_volatile(funcid) != PROVOLATILE_IMMUTABLE)
396 elog(ERROR, "DefineIndex: index function must be marked isImmutable");
398 /* Process opclass, using func return type as default type */
400 classOidP[0] = GetAttrOpClass(funcIndex, rettype,
401 accessMethodName, accessMethodId);
403 /* OK, return results */
405 indexInfo->ii_FuncOid = funcid;
406 /* Need to do the fmgr function lookup now, too */
407 fmgr_info(funcid, &indexInfo->ii_FuncInfo);
411 NormIndexAttrs(IndexInfo *indexInfo,
413 List *attList, /* list of IndexElem's */
415 char *accessMethodName,
422 * process attributeList
424 foreach(rest, attList)
426 IndexElem *attribute = (IndexElem *) lfirst(rest);
428 Form_pg_attribute attform;
430 if (attribute->name == NULL)
431 elog(ERROR, "missing attribute for define index");
433 atttuple = SearchSysCacheAttName(relId, attribute->name);
434 if (!HeapTupleIsValid(atttuple))
435 elog(ERROR, "DefineIndex: attribute \"%s\" not found",
437 attform = (Form_pg_attribute) GETSTRUCT(atttuple);
439 indexInfo->ii_KeyAttrNumbers[attn] = attform->attnum;
441 classOidP[attn] = GetAttrOpClass(attribute, attform->atttypid,
442 accessMethodName, accessMethodId);
444 ReleaseSysCache(atttuple);
450 GetAttrOpClass(IndexElem *attribute, Oid attrType,
451 char *accessMethodName, Oid accessMethodId)
459 if (attribute->opclass == NIL)
461 /* no operator class specified, so find the default */
462 opClassId = GetDefaultOpClass(attrType, accessMethodId);
463 if (!OidIsValid(opClassId))
464 elog(ERROR, "data type %s has no default operator class for access method \"%s\""
465 "\n\tYou must specify an operator class for the index or define a"
466 "\n\tdefault operator class for the data type",
467 format_type_be(attrType), accessMethodName);
472 * Specific opclass name given, so look up the opclass.
475 /* deconstruct the name list */
476 DeconstructQualifiedName(attribute->opclass, &schemaname, &opcname);
480 /* Look in specific schema only */
483 namespaceId = LookupExplicitNamespace(schemaname);
484 tuple = SearchSysCache(CLAAMNAMENSP,
485 ObjectIdGetDatum(accessMethodId),
486 PointerGetDatum(opcname),
487 ObjectIdGetDatum(namespaceId),
492 /* Unqualified opclass name, so search the search path */
493 opClassId = OpclassnameGetOpcid(accessMethodId, opcname);
494 if (!OidIsValid(opClassId))
495 elog(ERROR, "DefineIndex: operator class \"%s\" not supported by access method \"%s\"",
496 opcname, accessMethodName);
497 tuple = SearchSysCache(CLAOID,
498 ObjectIdGetDatum(opClassId),
502 if (!HeapTupleIsValid(tuple))
503 elog(ERROR, "DefineIndex: operator class \"%s\" not supported by access method \"%s\"",
504 NameListToString(attribute->opclass), accessMethodName);
507 * Verify that the index operator class accepts this datatype. Note
508 * we will accept binary compatibility.
510 opClassId = HeapTupleGetOid(tuple);
511 opInputType = ((Form_pg_opclass) GETSTRUCT(tuple))->opcintype;
513 if (!IsBinaryCoercible(attrType, opInputType))
514 elog(ERROR, "operator class \"%s\" does not accept data type %s",
515 NameListToString(attribute->opclass), format_type_be(attrType));
517 ReleaseSysCache(tuple);
523 GetDefaultOpClass(Oid attrType, Oid accessMethodId)
525 OpclassCandidateList opclass;
528 Oid exactOid = InvalidOid;
529 Oid compatibleOid = InvalidOid;
531 /* If it's a domain, look at the base type instead */
532 attrType = getBaseType(attrType);
535 * We scan through all the opclasses available for the access method,
536 * looking for one that is marked default and matches the target type
537 * (either exactly or binary-compatibly, but prefer an exact match).
539 * We could find more than one binary-compatible match, in which case we
540 * require the user to specify which one he wants. If we find more
541 * than one exact match, then someone put bogus entries in pg_opclass.
543 * The initial search is done by namespace.c so that we only consider
544 * opclasses visible in the current namespace search path.
546 for (opclass = OpclassGetCandidates(accessMethodId);
548 opclass = opclass->next)
550 if (opclass->opcdefault)
552 if (opclass->opcintype == attrType)
555 exactOid = opclass->oid;
557 else if (IsBinaryCoercible(attrType, opclass->opcintype))
560 compatibleOid = opclass->oid;
568 elog(ERROR, "pg_opclass contains multiple default opclasses for data type %s",
569 format_type_be(attrType));
570 if (ncompatible == 1)
571 return compatibleOid;
581 RemoveIndex(RangeVar *relation, DropBehavior behavior)
585 ObjectAddress object;
587 indOid = RangeVarGetRelid(relation, false);
588 relkind = get_rel_relkind(indOid);
589 if (relkind != RELKIND_INDEX)
590 elog(ERROR, "relation \"%s\" is of type \"%c\"",
591 relation->relname, relkind);
593 object.classId = RelOid_pg_class;
594 object.objectId = indOid;
595 object.objectSubId = 0;
597 performDeletion(&object, behavior);
605 ReindexIndex(RangeVar *indexRelation, bool force /* currently unused */ )
611 /* Choose in-place-or-not mode */
612 overwrite = IsIgnoringSystemIndexes();
614 indOid = RangeVarGetRelid(indexRelation, false);
615 tuple = SearchSysCache(RELOID,
616 ObjectIdGetDatum(indOid),
618 if (!HeapTupleIsValid(tuple))
619 elog(ERROR, "index \"%s\" does not exist", indexRelation->relname);
621 if (((Form_pg_class) GETSTRUCT(tuple))->relkind != RELKIND_INDEX)
622 elog(ERROR, "relation \"%s\" is of type \"%c\"",
623 indexRelation->relname,
624 ((Form_pg_class) GETSTRUCT(tuple))->relkind);
626 if (IsSystemClass((Form_pg_class) GETSTRUCT(tuple)) &&
627 !IsToastClass((Form_pg_class) GETSTRUCT(tuple)))
629 if (!allowSystemTableMods)
630 elog(ERROR, "\"%s\" is a system index. call REINDEX under standalone postgres with -O -P options",
631 indexRelation->relname);
632 if (!IsIgnoringSystemIndexes())
633 elog(ERROR, "\"%s\" is a system index. call REINDEX under standalone postgres with -P -O options",
634 indexRelation->relname);
637 ReleaseSysCache(tuple);
640 * In-place REINDEX within a transaction block is dangerous, because
641 * if the transaction is later rolled back we have no way to undo
642 * truncation of the index's physical file. Disallow it.
645 PreventTransactionChain((void *) indexRelation, "REINDEX");
647 if (!reindex_index(indOid, force, overwrite))
648 elog(WARNING, "index \"%s\" wasn't reindexed", indexRelation->relname);
653 * Recreate indexes of a table.
656 ReindexTable(RangeVar *relation, bool force)
661 heapOid = RangeVarGetRelid(relation, false);
662 relkind = get_rel_relkind(heapOid);
664 if (relkind != RELKIND_RELATION && relkind != RELKIND_TOASTVALUE)
665 elog(ERROR, "relation \"%s\" is of type \"%c\"",
666 relation->relname, relkind);
669 * In-place REINDEX within a transaction block is dangerous, because
670 * if the transaction is later rolled back we have no way to undo
671 * truncation of the index's physical file. Disallow it.
673 * XXX we assume that in-place reindex will only be done if
674 * IsIgnoringSystemIndexes() is true.
676 if (IsIgnoringSystemIndexes())
677 PreventTransactionChain((void *) relation, "REINDEX");
679 if (!reindex_relation(heapOid, force))
680 elog(WARNING, "table \"%s\" wasn't reindexed", relation->relname);
685 * Recreate indexes of a database.
688 ReindexDatabase(const char *dbname, bool force, bool all)
690 Relation relationRelation;
693 MemoryContext private_context;
699 Oid *relids = (Oid *) NULL;
703 if (strcmp(dbname, DatabaseName) != 0)
704 elog(ERROR, "REINDEX DATABASE: Can be executed only on the currently open database.");
706 if (!(superuser() || is_dbadmin(MyDatabaseId)))
707 elog(ERROR, "REINDEX DATABASE: Permission denied.");
709 if (!allowSystemTableMods)
710 elog(ERROR, "must be called under standalone postgres with -O -P options");
711 if (!IsIgnoringSystemIndexes())
712 elog(ERROR, "must be called under standalone postgres with -P -O options");
715 * We cannot run inside a user transaction block; if we were inside a
716 * transaction, then our commit- and start-transaction-command calls
717 * would not have the intended effect!
719 PreventTransactionChain((void *) dbname, "REINDEX");
722 * Create a memory context that will survive forced transaction
723 * commits we do below. Since it is a child of QueryContext, it will
724 * go away eventually even if we suffer an error; there's no need for
725 * special abort cleanup logic.
727 private_context = AllocSetContextCreate(QueryContext,
729 ALLOCSET_DEFAULT_MINSIZE,
730 ALLOCSET_DEFAULT_INITSIZE,
731 ALLOCSET_DEFAULT_MAXSIZE);
734 * Scan pg_class to build a list of the relations we need to reindex.
736 relationRelation = heap_openr(RelationRelationName, AccessShareLock);
737 scan = heap_beginscan(relationRelation, SnapshotNow, 0, NULL);
739 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
745 if (!(IsSystemClass((Form_pg_class) GETSTRUCT(tuple)) &&
746 !IsToastClass((Form_pg_class) GETSTRUCT(tuple))))
749 relkind = ((Form_pg_class) GETSTRUCT(tuple))->relkind;
750 if (relkind == RELKIND_RELATION || relkind == RELKIND_TOASTVALUE)
752 old = MemoryContextSwitchTo(private_context);
756 relids = palloc(sizeof(Oid) * relalc);
758 else if (relcnt >= relalc)
761 relids = repalloc(relids, sizeof(Oid) * relalc);
763 MemoryContextSwitchTo(old);
764 relids[relcnt] = HeapTupleGetOid(tuple);
769 heap_close(relationRelation, AccessShareLock);
771 /* Now reindex each rel in a separate transaction */
772 CommitTransactionCommand(true);
773 for (i = 0; i < relcnt; i++)
775 StartTransactionCommand(true);
776 SetQuerySnapshot(); /* might be needed for functional index */
777 if (reindex_relation(relids[i], force))
778 elog(NOTICE, "relation %u was reindexed", relids[i]);
779 CommitTransactionCommand(true);
781 /* Tell xact.c not to chain the upcoming commit */
782 StartTransactionCommand(true);
784 MemoryContextDelete(private_context);