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.75 2002/06/20 20:29:27 momjian Exp $
13 *-------------------------------------------------------------------------
18 #include "access/heapam.h"
19 #include "catalog/catalog.h"
20 #include "catalog/catname.h"
21 #include "catalog/index.h"
22 #include "catalog/namespace.h"
23 #include "catalog/pg_opclass.h"
24 #include "catalog/pg_proc.h"
25 #include "commands/defrem.h"
26 #include "miscadmin.h"
27 #include "optimizer/clauses.h"
28 #include "optimizer/planmain.h"
29 #include "optimizer/prep.h"
30 #include "parser/parsetree.h"
31 #include "parser/parse_coerce.h"
32 #include "parser/parse_func.h"
33 #include "utils/acl.h"
34 #include "utils/builtins.h"
35 #include "utils/lsyscache.h"
36 #include "utils/syscache.h"
39 #define IsFuncIndex(ATTR_LIST) (((IndexElem*)lfirst(ATTR_LIST))->funcname != NIL)
41 /* non-export function prototypes */
42 static void CheckPredicate(List *predList, List *rangeTable, Oid baseRelOid);
43 static void FuncIndexArgs(IndexInfo *indexInfo, Oid *classOidP,
46 char *accessMethodName, Oid accessMethodId);
47 static void NormIndexAttrs(IndexInfo *indexInfo, Oid *classOidP,
50 char *accessMethodName, Oid accessMethodId);
51 static Oid GetAttrOpClass(IndexElem *attribute, Oid attrType,
52 char *accessMethodName, Oid accessMethodId);
53 static Oid GetDefaultOpClass(Oid attrType, Oid accessMethodId);
57 * Creates a new index.
59 * 'attributeList' is a list of IndexElem specifying either a functional
60 * index or a list of attributes to index on.
61 * 'predicate' is the qual specified in the where clause.
62 * 'rangetable' is needed to interpret the predicate.
65 DefineIndex(RangeVar *heapRelation,
66 char *indexRelationName,
67 char *accessMethodName,
80 Form_pg_am accessMethodForm;
82 int numberOfAttributes;
86 * count attributes in index
88 numberOfAttributes = length(attributeList);
89 if (numberOfAttributes <= 0)
90 elog(ERROR, "DefineIndex: must specify at least one attribute");
91 if (numberOfAttributes > INDEX_MAX_KEYS)
92 elog(ERROR, "Cannot use more than %d attributes in an index",
96 * Open heap relation, acquire a suitable lock on it, remember its OID
98 rel = heap_openrv(heapRelation, ShareLock);
100 /* Note: during bootstrap may see uncataloged relation */
101 if (rel->rd_rel->relkind != RELKIND_RELATION &&
102 rel->rd_rel->relkind != RELKIND_UNCATALOGED)
103 elog(ERROR, "DefineIndex: relation \"%s\" is not a table",
104 heapRelation->relname);
106 relationId = RelationGetRelid(rel);
107 namespaceId = RelationGetNamespace(rel);
109 if (!IsBootstrapProcessingMode() &&
110 IsSystemRelation(rel) &&
111 !IndexesAreActive(relationId, false))
112 elog(ERROR, "Existing indexes are inactive. REINDEX first");
114 heap_close(rel, NoLock);
117 * Verify we (still) have CREATE rights in the rel's namespace.
118 * (Presumably we did when the rel was created, but maybe not anymore.)
119 * Skip check if bootstrapping, since permissions machinery may not
120 * be working yet; also, always allow if it's a temp table.
122 if (!IsBootstrapProcessingMode() && !isTempNamespace(namespaceId))
126 aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(),
128 if (aclresult != ACLCHECK_OK)
129 aclcheck_error(aclresult, get_namespace_name(namespaceId));
133 * look up the access method, verify it can handle the requested
136 tuple = SearchSysCache(AMNAME,
137 PointerGetDatum(accessMethodName),
139 if (!HeapTupleIsValid(tuple))
140 elog(ERROR, "DefineIndex: access method \"%s\" not found",
142 accessMethodId = tuple->t_data->t_oid;
143 accessMethodForm = (Form_pg_am) GETSTRUCT(tuple);
145 if (unique && !accessMethodForm->amcanunique)
146 elog(ERROR, "DefineIndex: access method \"%s\" does not support UNIQUE indexes",
148 if (numberOfAttributes > 1 && !accessMethodForm->amcanmulticol)
149 elog(ERROR, "DefineIndex: access method \"%s\" does not support multi-column indexes",
152 ReleaseSysCache(tuple);
155 * Convert the partial-index predicate from parsetree form to an
156 * implicit-AND qual expression, for easier evaluation at runtime.
157 * While we are at it, we reduce it to a canonical (CNF or DNF) form
158 * to simplify the task of proving implications.
160 if (predicate != NULL && rangetable != NIL)
162 cnfPred = canonicalize_qual((Expr *) copyObject(predicate), true);
163 fix_opids((Node *) cnfPred);
164 CheckPredicate(cnfPred, rangetable, relationId);
168 * Prepare arguments for index_create, primarily an IndexInfo
171 indexInfo = makeNode(IndexInfo);
172 indexInfo->ii_Predicate = cnfPred;
173 indexInfo->ii_FuncOid = InvalidOid;
174 indexInfo->ii_Unique = unique;
176 if (IsFuncIndex(attributeList))
178 IndexElem *funcIndex = (IndexElem *) lfirst(attributeList);
181 /* Parser should have given us only one list item, but check */
182 if (numberOfAttributes != 1)
183 elog(ERROR, "Functional index can only have one attribute");
185 nargs = length(funcIndex->args);
186 if (nargs > INDEX_MAX_KEYS)
187 elog(ERROR, "Index function can take at most %d arguments",
190 indexInfo->ii_NumIndexAttrs = 1;
191 indexInfo->ii_NumKeyAttrs = nargs;
193 classObjectId = (Oid *) palloc(sizeof(Oid));
195 FuncIndexArgs(indexInfo, classObjectId, funcIndex,
196 relationId, accessMethodName, accessMethodId);
200 indexInfo->ii_NumIndexAttrs = numberOfAttributes;
201 indexInfo->ii_NumKeyAttrs = numberOfAttributes;
203 classObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
205 NormIndexAttrs(indexInfo, classObjectId, attributeList,
206 relationId, accessMethodName, accessMethodId);
209 index_create(relationId, indexRelationName,
210 indexInfo, accessMethodId, classObjectId,
211 primary, allowSystemTableMods);
214 * We update the relation's pg_class tuple even if it already has
215 * relhasindex = true. This is needed to cause a shared-cache-inval
216 * message to be sent for the pg_class tuple, which will cause other
217 * backends to flush their relcache entries and in particular their
218 * cached lists of the indexes for this relation.
220 setRelhasindex(relationId, true, primary, InvalidOid);
226 * Checks that the given list of partial-index predicates refer
227 * (via the given range table) only to the given base relation oid.
229 * This used to also constrain the form of the predicate to forms that
230 * indxpath.c could do something with. However, that seems overly
231 * restrictive. One useful application of partial indexes is to apply
232 * a UNIQUE constraint across a subset of a table, and in that scenario
233 * any evaluatable predicate will work. So accept any predicate here
234 * (except ones requiring a plan), and let indxpath.c fend for itself.
238 CheckPredicate(List *predList, List *rangeTable, Oid baseRelOid)
240 if (length(rangeTable) != 1 || getrelid(1, rangeTable) != baseRelOid)
242 "Partial-index predicates may refer only to the base relation");
245 * We don't currently support generation of an actual query plan for a
246 * predicate, only simple scalar expressions; hence these
249 if (contain_subplans((Node *) predList))
250 elog(ERROR, "Cannot use subselect in index predicate");
251 if (contain_agg_clause((Node *) predList))
252 elog(ERROR, "Cannot use aggregate in index predicate");
255 * A predicate using mutable functions is probably wrong, for the
256 * same reasons that we don't allow a functional index to use one.
258 if (contain_mutable_functions((Node *) predList))
259 elog(ERROR, "Functions in index predicate must be marked isImmutable");
264 FuncIndexArgs(IndexInfo *indexInfo,
266 IndexElem *funcIndex,
268 char *accessMethodName,
271 Oid argTypes[FUNC_MAX_ARGS];
275 FuncDetailCode fdresult;
282 * process the function arguments, which are a list of T_String
283 * (someday ought to allow more general expressions?)
285 * Note caller already checked that list is not too long.
287 MemSet(argTypes, 0, sizeof(argTypes));
289 foreach(arglist, funcIndex->args)
291 char *arg = strVal(lfirst(arglist));
293 Form_pg_attribute att;
295 tuple = SearchSysCache(ATTNAME,
296 ObjectIdGetDatum(relId),
297 PointerGetDatum(arg),
299 if (!HeapTupleIsValid(tuple))
300 elog(ERROR, "DefineIndex: attribute \"%s\" not found", arg);
301 att = (Form_pg_attribute) GETSTRUCT(tuple);
302 indexInfo->ii_KeyAttrNumbers[nargs] = att->attnum;
303 argTypes[nargs] = att->atttypid;
304 ReleaseSysCache(tuple);
309 * Lookup the function procedure to get its OID and result type.
311 * We rely on parse_func.c to find the correct function in the possible
312 * presence of binary-compatible types. However, parse_func may do
313 * too much: it will accept a function that requires run-time coercion
314 * of input types, and the executor is not currently set up to support
315 * that. So, check to make sure that the selected function has
316 * exact-match or binary-compatible input types.
318 fdresult = func_get_detail(funcIndex->funcname, funcIndex->args,
320 &funcid, &rettype, &retset,
322 if (fdresult != FUNCDETAIL_NORMAL)
324 if (fdresult == FUNCDETAIL_AGGREGATE)
325 elog(ERROR, "DefineIndex: functional index may not use an aggregate function");
326 else if (fdresult == FUNCDETAIL_COERCION)
327 elog(ERROR, "DefineIndex: functional index must use a real function, not a type coercion"
328 "\n\tTry specifying the index opclass you want to use, instead");
330 func_error("DefineIndex", funcIndex->funcname, nargs, argTypes,
335 elog(ERROR, "DefineIndex: cannot index on a function returning a set");
337 for (i = 0; i < nargs; i++)
339 if (!IsBinaryCompatible(argTypes[i], true_typeids[i]))
340 func_error("DefineIndex", funcIndex->funcname, nargs, argTypes,
341 "Index function must be binary-compatible with table datatype");
345 * Require that the function be marked immutable. Using a mutable
346 * function for a functional index is highly questionable, since if
347 * you aren't going to get the same result for the same data every
348 * time, it's not clear what the index entries mean at all.
350 if (func_volatile(funcid) != PROVOLATILE_IMMUTABLE)
351 elog(ERROR, "DefineIndex: index function must be marked isImmutable");
353 /* Process opclass, using func return type as default type */
355 classOidP[0] = GetAttrOpClass(funcIndex, rettype,
356 accessMethodName, accessMethodId);
358 /* OK, return results */
360 indexInfo->ii_FuncOid = funcid;
361 /* Need to do the fmgr function lookup now, too */
362 fmgr_info(funcid, &indexInfo->ii_FuncInfo);
366 NormIndexAttrs(IndexInfo *indexInfo,
368 List *attList, /* list of IndexElem's */
370 char *accessMethodName,
377 * process attributeList
379 foreach(rest, attList)
381 IndexElem *attribute = (IndexElem *) lfirst(rest);
383 Form_pg_attribute attform;
385 if (attribute->name == NULL)
386 elog(ERROR, "missing attribute for define index");
388 atttuple = SearchSysCache(ATTNAME,
389 ObjectIdGetDatum(relId),
390 PointerGetDatum(attribute->name),
392 if (!HeapTupleIsValid(atttuple))
393 elog(ERROR, "DefineIndex: attribute \"%s\" not found",
395 attform = (Form_pg_attribute) GETSTRUCT(atttuple);
397 indexInfo->ii_KeyAttrNumbers[attn] = attform->attnum;
399 classOidP[attn] = GetAttrOpClass(attribute, attform->atttypid,
400 accessMethodName, accessMethodId);
402 ReleaseSysCache(atttuple);
408 GetAttrOpClass(IndexElem *attribute, Oid attrType,
409 char *accessMethodName, Oid accessMethodId)
412 char *schemaname = NULL;
413 char *opcname = NULL;
418 if (attribute->opclass == NIL)
420 /* no operator class specified, so find the default */
421 opClassId = GetDefaultOpClass(attrType, accessMethodId);
422 if (!OidIsValid(opClassId))
423 elog(ERROR, "data type %s has no default operator class for access method \"%s\""
424 "\n\tYou must specify an operator class for the index or define a"
425 "\n\tdefault operator class for the data type",
426 format_type_be(attrType), accessMethodName);
431 * Specific opclass name given, so look up the opclass.
434 /* deconstruct the name list */
435 switch (length(attribute->opclass))
438 opcname = strVal(lfirst(attribute->opclass));
441 schemaname = strVal(lfirst(attribute->opclass));
442 opcname = strVal(lsecond(attribute->opclass));
445 catalogname = strVal(lfirst(attribute->opclass));
446 schemaname = strVal(lsecond(attribute->opclass));
447 opcname = strVal(lfirst(lnext(lnext(attribute->opclass))));
449 * We check the catalog name and then ignore it.
451 if (strcmp(catalogname, DatabaseName) != 0)
452 elog(ERROR, "Cross-database references are not implemented");
455 elog(ERROR, "Improper opclass name (too many dotted names): %s",
456 NameListToString(attribute->opclass));
462 /* Look in specific schema only */
465 namespaceId = GetSysCacheOid(NAMESPACENAME,
466 CStringGetDatum(schemaname),
468 if (!OidIsValid(namespaceId))
469 elog(ERROR, "Namespace \"%s\" does not exist",
471 tuple = SearchSysCache(CLAAMNAMENSP,
472 ObjectIdGetDatum(accessMethodId),
473 PointerGetDatum(opcname),
474 ObjectIdGetDatum(namespaceId),
479 /* Unqualified opclass name, so search the search path */
480 opClassId = OpclassnameGetOpcid(accessMethodId, opcname);
481 if (!OidIsValid(opClassId))
482 elog(ERROR, "DefineIndex: operator class \"%s\" not supported by access method \"%s\"",
483 opcname, accessMethodName);
484 tuple = SearchSysCache(CLAOID,
485 ObjectIdGetDatum(opClassId),
489 if (!HeapTupleIsValid(tuple))
490 elog(ERROR, "DefineIndex: operator class \"%s\" not supported by access method \"%s\"",
491 NameListToString(attribute->opclass), accessMethodName);
494 * Verify that the index operator class accepts this
495 * datatype. Note we will accept binary compatibility.
497 opClassId = tuple->t_data->t_oid;
498 opInputType = ((Form_pg_opclass) GETSTRUCT(tuple))->opcintype;
500 if (!IsBinaryCompatible(attrType, opInputType))
501 elog(ERROR, "operator class \"%s\" does not accept data type %s",
502 NameListToString(attribute->opclass), format_type_be(attrType));
504 ReleaseSysCache(tuple);
510 GetDefaultOpClass(Oid attrType, Oid accessMethodId)
512 OpclassCandidateList opclass;
515 Oid exactOid = InvalidOid;
516 Oid compatibleOid = InvalidOid;
519 * We scan through all the opclasses available for the access method,
520 * looking for one that is marked default and matches the target type
521 * (either exactly or binary-compatibly, but prefer an exact match).
523 * We could find more than one binary-compatible match, in which case we
524 * require the user to specify which one he wants. If we find more
525 * than one exact match, then someone put bogus entries in pg_opclass.
527 * The initial search is done by namespace.c so that we only consider
528 * opclasses visible in the current namespace search path.
530 for (opclass = OpclassGetCandidates(accessMethodId);
532 opclass = opclass->next)
534 if (opclass->opcdefault)
536 if (opclass->opcintype == attrType)
539 exactOid = opclass->oid;
541 else if (IsBinaryCompatible(opclass->opcintype, attrType))
544 compatibleOid = opclass->oid;
552 elog(ERROR, "pg_opclass contains multiple default opclasses for data type %s",
553 format_type_be(attrType));
554 if (ncompatible == 1)
555 return compatibleOid;
565 * BadArg if name is invalid.
566 * "ERROR" if index nonexistent.
570 RemoveIndex(RangeVar *relation)
575 indOid = RangeVarGetRelid(relation, false);
576 tuple = SearchSysCache(RELOID,
577 ObjectIdGetDatum(indOid),
579 if (!HeapTupleIsValid(tuple))
580 elog(ERROR, "index \"%s\" does not exist", relation->relname);
582 if (((Form_pg_class) GETSTRUCT(tuple))->relkind != RELKIND_INDEX)
583 elog(ERROR, "relation \"%s\" is of type \"%c\"",
584 relation->relname, ((Form_pg_class) GETSTRUCT(tuple))->relkind);
586 ReleaseSysCache(tuple);
596 * "ERROR" if index nonexistent.
600 ReindexIndex(RangeVar *indexRelation, bool force /* currently unused */ )
604 bool overwrite = false;
607 * REINDEX within a transaction block is dangerous, because if the
608 * transaction is later rolled back we have no way to undo truncation
609 * of the index's physical file. Disallow it.
611 if (IsTransactionBlock())
612 elog(ERROR, "REINDEX cannot run inside a BEGIN/END block");
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)))
628 if (!allowSystemTableMods)
629 elog(ERROR, "\"%s\" is a system index. call REINDEX under standalone postgres with -O -P options",
630 indexRelation->relname);
631 if (!IsIgnoringSystemIndexes())
632 elog(ERROR, "\"%s\" is a system index. call REINDEX under standalone postgres with -P -O options",
633 indexRelation->relname);
636 ReleaseSysCache(tuple);
638 if (IsIgnoringSystemIndexes())
640 if (!reindex_index(indOid, force, overwrite))
641 elog(WARNING, "index \"%s\" wasn't reindexed", indexRelation->relname);
646 * Recreate indexes of a table.
649 * "ERROR" if table nonexistent.
653 ReindexTable(RangeVar *relation, bool force)
659 * REINDEX within a transaction block is dangerous, because if the
660 * transaction is later rolled back we have no way to undo truncation
661 * of the index's physical file. Disallow it.
663 if (IsTransactionBlock())
664 elog(ERROR, "REINDEX cannot run inside a BEGIN/END block");
666 heapOid = RangeVarGetRelid(relation, false);
667 tuple = SearchSysCache(RELOID,
668 ObjectIdGetDatum(heapOid),
670 if (!HeapTupleIsValid(tuple))
671 elog(ERROR, "table \"%s\" does not exist", relation->relname);
673 if (((Form_pg_class) GETSTRUCT(tuple))->relkind != RELKIND_RELATION)
674 elog(ERROR, "relation \"%s\" is of type \"%c\"",
676 ((Form_pg_class) GETSTRUCT(tuple))->relkind);
678 ReleaseSysCache(tuple);
680 if (!reindex_relation(heapOid, force))
681 elog(WARNING, "table \"%s\" wasn't reindexed", relation->relname);
686 * Recreate indexes of a database.
689 ReindexDatabase(const char *dbname, bool force, bool all)
691 Relation relationRelation;
694 MemoryContext private_context;
700 Oid *relids = (Oid *) NULL;
704 if (strcmp(dbname, DatabaseName) != 0)
705 elog(ERROR, "REINDEX DATABASE: Can be executed only on the currently open database.");
707 if (!(superuser() || is_dbadmin(MyDatabaseId)))
708 elog(ERROR, "REINDEX DATABASE: Permission denied.");
710 if (!allowSystemTableMods)
711 elog(ERROR, "must be called under standalone postgres with -O -P options");
712 if (!IsIgnoringSystemIndexes())
713 elog(ERROR, "must be called under standalone postgres with -P -O options");
716 * We cannot run inside a user transaction block; if we were inside a
717 * transaction, then our commit- and start-transaction-command calls
718 * would not have the intended effect!
720 if (IsTransactionBlock())
721 elog(ERROR, "REINDEX DATABASE cannot run inside a BEGIN/END block");
724 * Create a memory context that will survive forced transaction
725 * commits we do below. Since it is a child of QueryContext, it will
726 * go away eventually even if we suffer an error; there's no need for
727 * special abort cleanup logic.
729 private_context = AllocSetContextCreate(QueryContext,
731 ALLOCSET_DEFAULT_MINSIZE,
732 ALLOCSET_DEFAULT_INITSIZE,
733 ALLOCSET_DEFAULT_MAXSIZE);
736 * Scan pg_class to build a list of the relations we need to reindex.
738 relationRelation = heap_openr(RelationRelationName, AccessShareLock);
739 scan = heap_beginscan(relationRelation, SnapshotNow, 0, NULL);
741 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
745 if (!IsSystemClass((Form_pg_class) GETSTRUCT(tuple)))
748 if (((Form_pg_class) GETSTRUCT(tuple))->relkind == RELKIND_RELATION)
750 old = MemoryContextSwitchTo(private_context);
754 relids = palloc(sizeof(Oid) * relalc);
756 else if (relcnt >= relalc)
759 relids = repalloc(relids, sizeof(Oid) * relalc);
761 MemoryContextSwitchTo(old);
762 relids[relcnt] = tuple->t_data->t_oid;
767 heap_close(relationRelation, AccessShareLock);
769 /* Now reindex each rel in a separate transaction */
770 CommitTransactionCommand();
771 for (i = 0; i < relcnt; i++)
773 StartTransactionCommand();
774 if (reindex_relation(relids[i], force))
775 elog(WARNING, "relation %u was reindexed", relids[i]);
776 CommitTransactionCommand();
778 StartTransactionCommand();
780 MemoryContextDelete(private_context);