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.86 2002/08/30 22:18:05 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/index.h"
23 #include "catalog/namespace.h"
24 #include "catalog/pg_opclass.h"
25 #include "catalog/pg_proc.h"
26 #include "commands/defrem.h"
27 #include "miscadmin.h"
28 #include "optimizer/clauses.h"
29 #include "optimizer/planmain.h"
30 #include "optimizer/prep.h"
31 #include "parser/parsetree.h"
32 #include "parser/parse_coerce.h"
33 #include "parser/parse_func.h"
34 #include "utils/acl.h"
35 #include "utils/builtins.h"
36 #include "utils/lsyscache.h"
37 #include "utils/syscache.h"
40 #define IsFuncIndex(ATTR_LIST) (((IndexElem*)lfirst(ATTR_LIST))->funcname != NIL)
42 /* non-export function prototypes */
43 static void CheckPredicate(List *predList, List *rangeTable, Oid baseRelOid);
44 static void FuncIndexArgs(IndexInfo *indexInfo, Oid *classOidP,
47 char *accessMethodName, Oid accessMethodId);
48 static void NormIndexAttrs(IndexInfo *indexInfo, Oid *classOidP,
51 char *accessMethodName, Oid accessMethodId);
52 static Oid GetAttrOpClass(IndexElem *attribute, Oid attrType,
53 char *accessMethodName, Oid accessMethodId);
54 static Oid GetDefaultOpClass(Oid attrType, Oid accessMethodId);
58 * Creates a new index.
60 * 'attributeList' is a list of IndexElem specifying either a functional
61 * index or a list of attributes to index on.
62 * 'predicate' is the qual specified in the where clause.
63 * 'rangetable' is needed to interpret the predicate.
66 DefineIndex(RangeVar *heapRelation,
67 char *indexRelationName,
68 char *accessMethodName,
82 Form_pg_am accessMethodForm;
84 int numberOfAttributes;
88 * count attributes in index
90 numberOfAttributes = length(attributeList);
91 if (numberOfAttributes <= 0)
92 elog(ERROR, "DefineIndex: must specify at least one attribute");
93 if (numberOfAttributes > INDEX_MAX_KEYS)
94 elog(ERROR, "Cannot use more than %d attributes in an index",
98 * Open heap relation, acquire a suitable lock on it, remember its OID
100 rel = heap_openrv(heapRelation, ShareLock);
102 /* Note: during bootstrap may see uncataloged relation */
103 if (rel->rd_rel->relkind != RELKIND_RELATION &&
104 rel->rd_rel->relkind != RELKIND_UNCATALOGED)
105 elog(ERROR, "DefineIndex: relation \"%s\" is not a table",
106 heapRelation->relname);
108 relationId = RelationGetRelid(rel);
109 namespaceId = RelationGetNamespace(rel);
111 if (!IsBootstrapProcessingMode() &&
112 IsSystemRelation(rel) &&
113 !IndexesAreActive(relationId, false))
114 elog(ERROR, "Existing indexes are inactive. REINDEX first");
116 heap_close(rel, NoLock);
119 * Verify we (still) have CREATE rights in the rel's namespace.
120 * (Presumably we did when the rel was created, but maybe not anymore.)
121 * Skip check if bootstrapping, since permissions machinery may not
124 if (!IsBootstrapProcessingMode())
128 aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(),
130 if (aclresult != ACLCHECK_OK)
131 aclcheck_error(aclresult, get_namespace_name(namespaceId));
135 * look up the access method, verify it can handle the requested
138 tuple = SearchSysCache(AMNAME,
139 PointerGetDatum(accessMethodName),
141 if (!HeapTupleIsValid(tuple))
142 elog(ERROR, "DefineIndex: access method \"%s\" not found",
144 accessMethodId = HeapTupleGetOid(tuple);
145 accessMethodForm = (Form_pg_am) GETSTRUCT(tuple);
147 if (unique && !accessMethodForm->amcanunique)
148 elog(ERROR, "DefineIndex: access method \"%s\" does not support UNIQUE indexes",
150 if (numberOfAttributes > 1 && !accessMethodForm->amcanmulticol)
151 elog(ERROR, "DefineIndex: access method \"%s\" does not support multi-column indexes",
154 ReleaseSysCache(tuple);
157 * Convert the partial-index predicate from parsetree form to an
158 * implicit-AND qual expression, for easier evaluation at runtime.
159 * While we are at it, we reduce it to a canonical (CNF or DNF) form
160 * to simplify the task of proving implications.
162 if (predicate != NULL && rangetable != NIL)
164 cnfPred = canonicalize_qual((Expr *) copyObject(predicate), true);
165 fix_opids((Node *) cnfPred);
166 CheckPredicate(cnfPred, rangetable, relationId);
170 * Prepare arguments for index_create, primarily an IndexInfo
173 indexInfo = makeNode(IndexInfo);
174 indexInfo->ii_Predicate = cnfPred;
175 indexInfo->ii_FuncOid = InvalidOid;
176 indexInfo->ii_Unique = unique;
178 if (IsFuncIndex(attributeList))
180 IndexElem *funcIndex = (IndexElem *) lfirst(attributeList);
183 /* Parser should have given us only one list item, but check */
184 if (numberOfAttributes != 1)
185 elog(ERROR, "Functional index can only have one attribute");
187 nargs = length(funcIndex->args);
188 if (nargs > INDEX_MAX_KEYS)
189 elog(ERROR, "Index function can take at most %d arguments",
192 indexInfo->ii_NumIndexAttrs = 1;
193 indexInfo->ii_NumKeyAttrs = nargs;
195 classObjectId = (Oid *) palloc(sizeof(Oid));
197 FuncIndexArgs(indexInfo, classObjectId, funcIndex,
198 relationId, accessMethodName, accessMethodId);
202 indexInfo->ii_NumIndexAttrs = numberOfAttributes;
203 indexInfo->ii_NumKeyAttrs = numberOfAttributes;
205 classObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
207 NormIndexAttrs(indexInfo, classObjectId, attributeList,
208 relationId, accessMethodName, accessMethodId);
211 index_create(relationId, indexRelationName,
212 indexInfo, accessMethodId, classObjectId,
213 primary, isconstraint, allowSystemTableMods);
216 * We update the relation's pg_class tuple even if it already has
217 * relhasindex = true. This is needed to cause a shared-cache-inval
218 * message to be sent for the pg_class tuple, which will cause other
219 * backends to flush their relcache entries and in particular their
220 * cached lists of the indexes for this relation.
222 setRelhasindex(relationId, true, primary, InvalidOid);
228 * Checks that the given list of partial-index predicates refer
229 * (via the given range table) only to the given base relation oid.
231 * This used to also constrain the form of the predicate to forms that
232 * indxpath.c could do something with. However, that seems overly
233 * restrictive. One useful application of partial indexes is to apply
234 * a UNIQUE constraint across a subset of a table, and in that scenario
235 * any evaluatable predicate will work. So accept any predicate here
236 * (except ones requiring a plan), and let indxpath.c fend for itself.
240 CheckPredicate(List *predList, List *rangeTable, Oid baseRelOid)
242 if (length(rangeTable) != 1 || getrelid(1, rangeTable) != baseRelOid)
244 "Partial-index predicates may refer only to the base relation");
247 * We don't currently support generation of an actual query plan for a
248 * predicate, only simple scalar expressions; hence these
251 if (contain_subplans((Node *) predList))
252 elog(ERROR, "Cannot use subselect in index predicate");
253 if (contain_agg_clause((Node *) predList))
254 elog(ERROR, "Cannot use aggregate in index predicate");
257 * A predicate using mutable functions is probably wrong, for the
258 * same reasons that we don't allow a functional index to use one.
260 if (contain_mutable_functions((Node *) predList))
261 elog(ERROR, "Functions in index predicate must be marked isImmutable");
266 FuncIndexArgs(IndexInfo *indexInfo,
268 IndexElem *funcIndex,
270 char *accessMethodName,
273 Oid argTypes[FUNC_MAX_ARGS];
277 FuncDetailCode fdresult;
284 * process the function arguments, which are a list of T_String
285 * (someday ought to allow more general expressions?)
287 * Note caller already checked that list is not too long.
289 MemSet(argTypes, 0, sizeof(argTypes));
291 foreach(arglist, funcIndex->args)
293 char *arg = strVal(lfirst(arglist));
295 Form_pg_attribute att;
297 tuple = SearchSysCacheAttName(relId, arg);
298 if (!HeapTupleIsValid(tuple))
299 elog(ERROR, "DefineIndex: attribute \"%s\" not found", arg);
300 att = (Form_pg_attribute) GETSTRUCT(tuple);
301 indexInfo->ii_KeyAttrNumbers[nargs] = att->attnum;
302 argTypes[nargs] = att->atttypid;
303 ReleaseSysCache(tuple);
308 * Lookup the function procedure to get its OID and result type.
310 * We rely on parse_func.c to find the correct function in the possible
311 * presence of binary-compatible types. However, parse_func may do
312 * too much: it will accept a function that requires run-time coercion
313 * of input types, and the executor is not currently set up to support
314 * that. So, check to make sure that the selected function has
315 * exact-match or binary-compatible input types.
317 fdresult = func_get_detail(funcIndex->funcname, funcIndex->args,
319 &funcid, &rettype, &retset,
321 if (fdresult != FUNCDETAIL_NORMAL)
323 if (fdresult == FUNCDETAIL_AGGREGATE)
324 elog(ERROR, "DefineIndex: functional index may not use an aggregate function");
325 else if (fdresult == FUNCDETAIL_COERCION)
326 elog(ERROR, "DefineIndex: functional index must use a real function, not a type coercion"
327 "\n\tTry specifying the index opclass you want to use, instead");
329 func_error("DefineIndex", funcIndex->funcname, nargs, argTypes,
334 elog(ERROR, "DefineIndex: cannot index on a function returning a set");
336 for (i = 0; i < nargs; i++)
338 if (!IsBinaryCompatible(argTypes[i], true_typeids[i]))
339 func_error("DefineIndex", funcIndex->funcname, nargs, argTypes,
340 "Index function must be binary-compatible with table datatype");
344 * Require that the function be marked immutable. Using a mutable
345 * function for a functional index is highly questionable, since if
346 * you aren't going to get the same result for the same data every
347 * time, it's not clear what the index entries mean at all.
349 if (func_volatile(funcid) != PROVOLATILE_IMMUTABLE)
350 elog(ERROR, "DefineIndex: index function must be marked isImmutable");
352 /* Process opclass, using func return type as default type */
354 classOidP[0] = GetAttrOpClass(funcIndex, rettype,
355 accessMethodName, accessMethodId);
357 /* OK, return results */
359 indexInfo->ii_FuncOid = funcid;
360 /* Need to do the fmgr function lookup now, too */
361 fmgr_info(funcid, &indexInfo->ii_FuncInfo);
365 NormIndexAttrs(IndexInfo *indexInfo,
367 List *attList, /* list of IndexElem's */
369 char *accessMethodName,
376 * process attributeList
378 foreach(rest, attList)
380 IndexElem *attribute = (IndexElem *) lfirst(rest);
382 Form_pg_attribute attform;
384 if (attribute->name == NULL)
385 elog(ERROR, "missing attribute for define index");
387 atttuple = SearchSysCacheAttName(relId, attribute->name);
388 if (!HeapTupleIsValid(atttuple))
389 elog(ERROR, "DefineIndex: attribute \"%s\" not found",
391 attform = (Form_pg_attribute) GETSTRUCT(atttuple);
393 indexInfo->ii_KeyAttrNumbers[attn] = attform->attnum;
395 classOidP[attn] = GetAttrOpClass(attribute, attform->atttypid,
396 accessMethodName, accessMethodId);
398 ReleaseSysCache(atttuple);
404 GetAttrOpClass(IndexElem *attribute, Oid attrType,
405 char *accessMethodName, Oid accessMethodId)
413 if (attribute->opclass == NIL)
415 /* no operator class specified, so find the default */
416 opClassId = GetDefaultOpClass(attrType, accessMethodId);
417 if (!OidIsValid(opClassId))
418 elog(ERROR, "data type %s has no default operator class for access method \"%s\""
419 "\n\tYou must specify an operator class for the index or define a"
420 "\n\tdefault operator class for the data type",
421 format_type_be(attrType), accessMethodName);
426 * Specific opclass name given, so look up the opclass.
429 /* deconstruct the name list */
430 DeconstructQualifiedName(attribute->opclass, &schemaname, &opcname);
434 /* Look in specific schema only */
437 namespaceId = LookupExplicitNamespace(schemaname);
438 tuple = SearchSysCache(CLAAMNAMENSP,
439 ObjectIdGetDatum(accessMethodId),
440 PointerGetDatum(opcname),
441 ObjectIdGetDatum(namespaceId),
446 /* Unqualified opclass name, so search the search path */
447 opClassId = OpclassnameGetOpcid(accessMethodId, opcname);
448 if (!OidIsValid(opClassId))
449 elog(ERROR, "DefineIndex: operator class \"%s\" not supported by access method \"%s\"",
450 opcname, accessMethodName);
451 tuple = SearchSysCache(CLAOID,
452 ObjectIdGetDatum(opClassId),
456 if (!HeapTupleIsValid(tuple))
457 elog(ERROR, "DefineIndex: operator class \"%s\" not supported by access method \"%s\"",
458 NameListToString(attribute->opclass), accessMethodName);
461 * Verify that the index operator class accepts this
462 * datatype. Note we will accept binary compatibility.
464 opClassId = HeapTupleGetOid(tuple);
465 opInputType = ((Form_pg_opclass) GETSTRUCT(tuple))->opcintype;
467 if (!IsBinaryCompatible(attrType, opInputType))
468 elog(ERROR, "operator class \"%s\" does not accept data type %s",
469 NameListToString(attribute->opclass), format_type_be(attrType));
471 ReleaseSysCache(tuple);
477 GetDefaultOpClass(Oid attrType, Oid accessMethodId)
479 OpclassCandidateList opclass;
482 Oid exactOid = InvalidOid;
483 Oid compatibleOid = InvalidOid;
485 /* If it's a domain, look at the base type instead */
486 attrType = getBaseType(attrType);
489 * We scan through all the opclasses available for the access method,
490 * looking for one that is marked default and matches the target type
491 * (either exactly or binary-compatibly, but prefer an exact match).
493 * We could find more than one binary-compatible match, in which case we
494 * require the user to specify which one he wants. If we find more
495 * than one exact match, then someone put bogus entries in pg_opclass.
497 * The initial search is done by namespace.c so that we only consider
498 * opclasses visible in the current namespace search path.
500 for (opclass = OpclassGetCandidates(accessMethodId);
502 opclass = opclass->next)
504 if (opclass->opcdefault)
506 if (opclass->opcintype == attrType)
509 exactOid = opclass->oid;
511 else if (IsBinaryCompatible(opclass->opcintype, attrType))
514 compatibleOid = opclass->oid;
522 elog(ERROR, "pg_opclass contains multiple default opclasses for data type %s",
523 format_type_be(attrType));
524 if (ncompatible == 1)
525 return compatibleOid;
535 RemoveIndex(RangeVar *relation, DropBehavior behavior)
539 ObjectAddress object;
541 indOid = RangeVarGetRelid(relation, false);
542 tuple = SearchSysCache(RELOID,
543 ObjectIdGetDatum(indOid),
545 if (!HeapTupleIsValid(tuple))
546 elog(ERROR, "index \"%s\" does not exist", relation->relname);
548 if (((Form_pg_class) GETSTRUCT(tuple))->relkind != RELKIND_INDEX)
549 elog(ERROR, "relation \"%s\" is of type \"%c\"",
550 relation->relname, ((Form_pg_class) GETSTRUCT(tuple))->relkind);
552 ReleaseSysCache(tuple);
554 object.classId = RelOid_pg_class;
555 object.objectId = indOid;
556 object.objectSubId = 0;
558 performDeletion(&object, behavior);
566 ReindexIndex(RangeVar *indexRelation, bool force /* currently unused */ )
570 bool overwrite = false;
573 * REINDEX within a transaction block is dangerous, because if the
574 * transaction is later rolled back we have no way to undo truncation
575 * of the index's physical file. Disallow it.
577 if (IsTransactionBlock())
578 elog(ERROR, "REINDEX cannot run inside a BEGIN/END block");
580 indOid = RangeVarGetRelid(indexRelation, false);
581 tuple = SearchSysCache(RELOID,
582 ObjectIdGetDatum(indOid),
584 if (!HeapTupleIsValid(tuple))
585 elog(ERROR, "index \"%s\" does not exist", indexRelation->relname);
587 if (((Form_pg_class) GETSTRUCT(tuple))->relkind != RELKIND_INDEX)
588 elog(ERROR, "relation \"%s\" is of type \"%c\"",
589 indexRelation->relname,
590 ((Form_pg_class) GETSTRUCT(tuple))->relkind);
592 if (IsSystemClass((Form_pg_class) GETSTRUCT(tuple)) &&
593 !IsToastClass((Form_pg_class) GETSTRUCT(tuple)))
595 if (!allowSystemTableMods)
596 elog(ERROR, "\"%s\" is a system index. call REINDEX under standalone postgres with -O -P options",
597 indexRelation->relname);
598 if (!IsIgnoringSystemIndexes())
599 elog(ERROR, "\"%s\" is a system index. call REINDEX under standalone postgres with -P -O options",
600 indexRelation->relname);
603 ReleaseSysCache(tuple);
605 if (IsIgnoringSystemIndexes())
607 if (!reindex_index(indOid, force, overwrite))
608 elog(WARNING, "index \"%s\" wasn't reindexed", indexRelation->relname);
613 * Recreate indexes of a table.
616 ReindexTable(RangeVar *relation, bool force)
623 * REINDEX within a transaction block is dangerous, because if the
624 * transaction is later rolled back we have no way to undo truncation
625 * of the index's physical file. Disallow it.
627 if (IsTransactionBlock())
628 elog(ERROR, "REINDEX cannot run inside a BEGIN/END block");
630 heapOid = RangeVarGetRelid(relation, false);
631 tuple = SearchSysCache(RELOID,
632 ObjectIdGetDatum(heapOid),
634 if (!HeapTupleIsValid(tuple))
635 elog(ERROR, "table \"%s\" does not exist", relation->relname);
636 relkind = ((Form_pg_class) GETSTRUCT(tuple))->relkind;
638 if (relkind != RELKIND_RELATION && relkind != RELKIND_TOASTVALUE)
639 elog(ERROR, "relation \"%s\" is of type \"%c\"",
640 relation->relname, relkind);
642 ReleaseSysCache(tuple);
644 if (!reindex_relation(heapOid, force))
645 elog(WARNING, "table \"%s\" wasn't reindexed", relation->relname);
650 * Recreate indexes of a database.
653 ReindexDatabase(const char *dbname, bool force, bool all)
655 Relation relationRelation;
658 MemoryContext private_context;
664 Oid *relids = (Oid *) NULL;
668 if (strcmp(dbname, DatabaseName) != 0)
669 elog(ERROR, "REINDEX DATABASE: Can be executed only on the currently open database.");
671 if (!(superuser() || is_dbadmin(MyDatabaseId)))
672 elog(ERROR, "REINDEX DATABASE: Permission denied.");
674 if (!allowSystemTableMods)
675 elog(ERROR, "must be called under standalone postgres with -O -P options");
676 if (!IsIgnoringSystemIndexes())
677 elog(ERROR, "must be called under standalone postgres with -P -O options");
680 * We cannot run inside a user transaction block; if we were inside a
681 * transaction, then our commit- and start-transaction-command calls
682 * would not have the intended effect!
684 if (IsTransactionBlock())
685 elog(ERROR, "REINDEX DATABASE cannot run inside a BEGIN/END block");
688 * Create a memory context that will survive forced transaction
689 * commits we do below. Since it is a child of QueryContext, it will
690 * go away eventually even if we suffer an error; there's no need for
691 * special abort cleanup logic.
693 private_context = AllocSetContextCreate(QueryContext,
695 ALLOCSET_DEFAULT_MINSIZE,
696 ALLOCSET_DEFAULT_INITSIZE,
697 ALLOCSET_DEFAULT_MAXSIZE);
700 * Scan pg_class to build a list of the relations we need to reindex.
702 relationRelation = heap_openr(RelationRelationName, AccessShareLock);
703 scan = heap_beginscan(relationRelation, SnapshotNow, 0, NULL);
705 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
711 if (!(IsSystemClass((Form_pg_class) GETSTRUCT(tuple)) &&
712 !IsToastClass((Form_pg_class) GETSTRUCT(tuple))))
715 relkind = ((Form_pg_class) GETSTRUCT(tuple))->relkind;
716 if (relkind == RELKIND_RELATION || relkind == RELKIND_TOASTVALUE)
718 old = MemoryContextSwitchTo(private_context);
722 relids = palloc(sizeof(Oid) * relalc);
724 else if (relcnt >= relalc)
727 relids = repalloc(relids, sizeof(Oid) * relalc);
729 MemoryContextSwitchTo(old);
730 relids[relcnt] = HeapTupleGetOid(tuple);
735 heap_close(relationRelation, AccessShareLock);
737 /* Now reindex each rel in a separate transaction */
738 CommitTransactionCommand(true);
739 for (i = 0; i < relcnt; i++)
741 StartTransactionCommand(true);
742 if (reindex_relation(relids[i], force))
743 elog(NOTICE, "relation %u was reindexed", relids[i]);
744 CommitTransactionCommand(true);
746 /* Tell xact.c not to chain the upcoming commit */
747 StartTransactionCommand(true);
749 MemoryContextDelete(private_context);