1 /*-------------------------------------------------------------------------
4 * POSTGRES define and remove index code.
6 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/commands/indexcmds.c,v 1.139 2006/05/10 23:18:39 tgl Exp $
13 *-------------------------------------------------------------------------
18 #include "access/genam.h"
19 #include "access/heapam.h"
20 #include "catalog/catalog.h"
21 #include "catalog/dependency.h"
22 #include "catalog/heap.h"
23 #include "catalog/index.h"
24 #include "catalog/indexing.h"
25 #include "catalog/pg_opclass.h"
26 #include "catalog/pg_tablespace.h"
27 #include "commands/dbcommands.h"
28 #include "commands/defrem.h"
29 #include "commands/tablecmds.h"
30 #include "commands/tablespace.h"
31 #include "mb/pg_wchar.h"
32 #include "miscadmin.h"
33 #include "optimizer/clauses.h"
34 #include "parser/parsetree.h"
35 #include "parser/parse_coerce.h"
36 #include "parser/parse_expr.h"
37 #include "parser/parse_func.h"
38 #include "utils/acl.h"
39 #include "utils/builtins.h"
40 #include "utils/fmgroids.h"
41 #include "utils/lsyscache.h"
42 #include "utils/memutils.h"
43 #include "utils/relcache.h"
44 #include "utils/syscache.h"
47 /* non-export function prototypes */
48 static void CheckPredicate(Expr *predicate);
49 static void ComputeIndexAttrs(IndexInfo *indexInfo, Oid *classOidP,
52 char *accessMethodName, Oid accessMethodId,
54 static Oid GetIndexOpClass(List *opclass, Oid attrType,
55 char *accessMethodName, Oid accessMethodId);
56 static bool relationHasPrimaryKey(Relation rel);
61 * Creates a new index.
63 * 'heapRelation': the relation the index will apply to.
64 * 'indexRelationName': the name for the new index, or NULL to indicate
65 * that a nonconflicting default name should be picked.
66 * 'indexRelationId': normally InvalidOid, but during bootstrap can be
67 * nonzero to specify a preselected OID for the index.
68 * 'accessMethodName': name of the AM to use.
69 * 'tableSpaceName': name of the tablespace to create the index in.
70 * NULL specifies using the appropriate default.
71 * 'attributeList': a list of IndexElem specifying columns and expressions
73 * 'predicate': the partial-index condition, or NULL if none.
74 * 'rangetable': needed to interpret the predicate.
75 * 'unique': make the index enforce uniqueness.
76 * 'primary': mark the index as a primary key in the catalogs.
77 * 'isconstraint': index is for a PRIMARY KEY or UNIQUE constraint,
78 * so build a pg_constraint entry for it.
79 * 'is_alter_table': this is due to an ALTER rather than a CREATE operation.
80 * 'check_rights': check for CREATE rights in the namespace. (This should
81 * be true except when ALTER is deleting/recreating an index.)
82 * 'skip_build': make the catalog entries but leave the index file empty;
83 * it will be filled later.
84 * 'quiet': suppress the NOTICE chatter ordinarily provided for constraints.
87 DefineIndex(RangeVar *heapRelation,
88 char *indexRelationName,
90 char *accessMethodName,
110 Form_pg_am accessMethodForm;
111 IndexInfo *indexInfo;
112 int numberOfAttributes;
115 * count attributes in index
117 numberOfAttributes = list_length(attributeList);
118 if (numberOfAttributes <= 0)
120 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
121 errmsg("must specify at least one column")));
122 if (numberOfAttributes > INDEX_MAX_KEYS)
124 (errcode(ERRCODE_TOO_MANY_COLUMNS),
125 errmsg("cannot use more than %d columns in an index",
129 * Open heap relation, acquire a suitable lock on it, remember its OID
131 rel = heap_openrv(heapRelation, ShareLock);
133 /* Note: during bootstrap may see uncataloged relation */
134 if (rel->rd_rel->relkind != RELKIND_RELATION &&
135 rel->rd_rel->relkind != RELKIND_UNCATALOGED)
137 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
138 errmsg("\"%s\" is not a table",
139 heapRelation->relname)));
141 relationId = RelationGetRelid(rel);
142 namespaceId = RelationGetNamespace(rel);
145 * Verify we (still) have CREATE rights in the rel's namespace.
146 * (Presumably we did when the rel was created, but maybe not anymore.)
147 * Skip check if caller doesn't want it. Also skip check if
148 * bootstrapping, since permissions machinery may not be working yet.
150 if (check_rights && !IsBootstrapProcessingMode())
154 aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(),
156 if (aclresult != ACLCHECK_OK)
157 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
158 get_namespace_name(namespaceId));
162 * Select tablespace to use. If not specified, use default_tablespace
163 * (which may in turn default to database's default).
167 tablespaceId = get_tablespace_oid(tableSpaceName);
168 if (!OidIsValid(tablespaceId))
170 (errcode(ERRCODE_UNDEFINED_OBJECT),
171 errmsg("tablespace \"%s\" does not exist",
176 tablespaceId = GetDefaultTablespace();
177 /* note InvalidOid is OK in this case */
180 /* Check permissions except when using database's default */
181 if (OidIsValid(tablespaceId))
185 aclresult = pg_tablespace_aclcheck(tablespaceId, GetUserId(),
187 if (aclresult != ACLCHECK_OK)
188 aclcheck_error(aclresult, ACL_KIND_TABLESPACE,
189 get_tablespace_name(tablespaceId));
193 * Force shared indexes into the pg_global tablespace. This is a bit of a
194 * hack but seems simpler than marking them in the BKI commands.
196 if (rel->rd_rel->relisshared)
197 tablespaceId = GLOBALTABLESPACE_OID;
200 * Select name for index if caller didn't specify
202 if (indexRelationName == NULL)
205 indexRelationName = ChooseRelationName(RelationGetRelationName(rel),
211 IndexElem *iparam = (IndexElem *) linitial(attributeList);
213 indexRelationName = ChooseRelationName(RelationGetRelationName(rel),
221 * look up the access method, verify it can handle the requested features
223 tuple = SearchSysCache(AMNAME,
224 PointerGetDatum(accessMethodName),
226 if (!HeapTupleIsValid(tuple))
229 * Hack to provide more-or-less-transparent updating of old RTREE
230 * indexes to GIST: if RTREE is requested and not found, use GIST.
232 if (strcmp(accessMethodName, "rtree") == 0)
235 (errmsg("substituting access method \"gist\" for obsolete method \"rtree\"")));
236 accessMethodName = "gist";
237 tuple = SearchSysCache(AMNAME,
238 PointerGetDatum(accessMethodName),
242 if (!HeapTupleIsValid(tuple))
244 (errcode(ERRCODE_UNDEFINED_OBJECT),
245 errmsg("access method \"%s\" does not exist",
248 accessMethodId = HeapTupleGetOid(tuple);
249 accessMethodForm = (Form_pg_am) GETSTRUCT(tuple);
251 if (unique && !accessMethodForm->amcanunique)
253 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
254 errmsg("access method \"%s\" does not support unique indexes",
256 if (numberOfAttributes > 1 && !accessMethodForm->amcanmulticol)
258 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
259 errmsg("access method \"%s\" does not support multicolumn indexes",
262 ReleaseSysCache(tuple);
265 * If a range table was created then check that only the base rel is
268 if (rangetable != NIL)
270 if (list_length(rangetable) != 1 || getrelid(1, rangetable) != relationId)
272 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
273 errmsg("index expressions and predicates may refer only to the table being indexed")));
277 * Validate predicate, if given
280 CheckPredicate(predicate);
283 * Extra checks when creating a PRIMARY KEY index.
291 * If ALTER TABLE, check that there isn't already a PRIMARY KEY. In
292 * CREATE TABLE, we have faith that the parser rejected multiple pkey
293 * clauses; and CREATE INDEX doesn't have a way to say PRIMARY KEY, so
294 * it's no problem either.
296 if (is_alter_table &&
297 relationHasPrimaryKey(rel))
300 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
301 errmsg("multiple primary keys for table \"%s\" are not allowed",
302 RelationGetRelationName(rel))));
306 * Check that all of the attributes in a primary key are marked as not
307 * null, otherwise attempt to ALTER TABLE .. SET NOT NULL
310 foreach(keys, attributeList)
312 IndexElem *key = (IndexElem *) lfirst(keys);
317 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
318 errmsg("primary keys cannot be expressions")));
320 /* System attributes are never null, so no problem */
321 if (SystemAttributeByName(key->name, rel->rd_rel->relhasoids))
324 atttuple = SearchSysCacheAttName(relationId, key->name);
325 if (HeapTupleIsValid(atttuple))
327 if (!((Form_pg_attribute) GETSTRUCT(atttuple))->attnotnull)
329 /* Add a subcommand to make this one NOT NULL */
330 AlterTableCmd *cmd = makeNode(AlterTableCmd);
332 cmd->subtype = AT_SetNotNull;
333 cmd->name = key->name;
335 cmds = lappend(cmds, cmd);
337 ReleaseSysCache(atttuple);
342 * This shouldn't happen during CREATE TABLE, but can happen
343 * during ALTER TABLE. Keep message in sync with
344 * transformIndexConstraints() in parser/analyze.c.
347 (errcode(ERRCODE_UNDEFINED_COLUMN),
348 errmsg("column \"%s\" named in key does not exist",
354 * XXX: Shouldn't the ALTER TABLE .. SET NOT NULL cascade to child
355 * tables? Currently, since the PRIMARY KEY itself doesn't cascade,
356 * we don't cascade the notnull constraint(s) either; but this is
359 * XXX: possible future improvement: when being called from ALTER
360 * TABLE, it would be more efficient to merge this with the outer
361 * ALTER TABLE, so as to avoid two scans. But that seems to
362 * complicate DefineIndex's API unduly.
365 AlterTableInternal(relationId, cmds, false);
369 * Prepare arguments for index_create, primarily an IndexInfo structure.
370 * Note that ii_Predicate must be in implicit-AND format.
372 indexInfo = makeNode(IndexInfo);
373 indexInfo->ii_NumIndexAttrs = numberOfAttributes;
374 indexInfo->ii_Expressions = NIL; /* for now */
375 indexInfo->ii_ExpressionsState = NIL;
376 indexInfo->ii_Predicate = make_ands_implicit(predicate);
377 indexInfo->ii_PredicateState = NIL;
378 indexInfo->ii_Unique = unique;
380 classObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
381 ComputeIndexAttrs(indexInfo, classObjectId, attributeList,
382 relationId, accessMethodName, accessMethodId,
385 heap_close(rel, NoLock);
388 * Report index creation if appropriate (delay this till after most of the
391 if (isconstraint && !quiet)
393 (errmsg("%s %s will create implicit index \"%s\" for table \"%s\"",
394 is_alter_table ? "ALTER TABLE / ADD" : "CREATE TABLE /",
395 primary ? "PRIMARY KEY" : "UNIQUE",
396 indexRelationName, RelationGetRelationName(rel))));
398 index_create(relationId, indexRelationName, indexRelationId,
399 indexInfo, accessMethodId, tablespaceId, classObjectId,
400 primary, false, isconstraint,
401 allowSystemTableMods, skip_build);
407 * Checks that the given partial-index predicate is valid.
409 * This used to also constrain the form of the predicate to forms that
410 * indxpath.c could do something with. However, that seems overly
411 * restrictive. One useful application of partial indexes is to apply
412 * a UNIQUE constraint across a subset of a table, and in that scenario
413 * any evaluatable predicate will work. So accept any predicate here
414 * (except ones requiring a plan), and let indxpath.c fend for itself.
417 CheckPredicate(Expr *predicate)
420 * We don't currently support generation of an actual query plan for a
421 * predicate, only simple scalar expressions; hence these restrictions.
423 if (contain_subplans((Node *) predicate))
425 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
426 errmsg("cannot use subquery in index predicate")));
427 if (contain_agg_clause((Node *) predicate))
429 (errcode(ERRCODE_GROUPING_ERROR),
430 errmsg("cannot use aggregate in index predicate")));
433 * A predicate using mutable functions is probably wrong, for the same
434 * reasons that we don't allow an index expression to use one.
436 if (contain_mutable_functions((Node *) predicate))
438 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
439 errmsg("functions in index predicate must be marked IMMUTABLE")));
443 ComputeIndexAttrs(IndexInfo *indexInfo,
445 List *attList, /* list of IndexElem's */
447 char *accessMethodName,
455 * process attributeList
457 foreach(rest, attList)
459 IndexElem *attribute = (IndexElem *) lfirst(rest);
462 if (attribute->name != NULL)
464 /* Simple index attribute */
466 Form_pg_attribute attform;
468 Assert(attribute->expr == NULL);
469 atttuple = SearchSysCacheAttName(relId, attribute->name);
470 if (!HeapTupleIsValid(atttuple))
472 /* difference in error message spellings is historical */
475 (errcode(ERRCODE_UNDEFINED_COLUMN),
476 errmsg("column \"%s\" named in key does not exist",
480 (errcode(ERRCODE_UNDEFINED_COLUMN),
481 errmsg("column \"%s\" does not exist",
484 attform = (Form_pg_attribute) GETSTRUCT(atttuple);
485 indexInfo->ii_KeyAttrNumbers[attn] = attform->attnum;
486 atttype = attform->atttypid;
487 ReleaseSysCache(atttuple);
489 else if (attribute->expr && IsA(attribute->expr, Var))
491 /* Tricky tricky, he wrote (column) ... treat as simple attr */
492 Var *var = (Var *) attribute->expr;
494 indexInfo->ii_KeyAttrNumbers[attn] = var->varattno;
495 atttype = get_atttype(relId, var->varattno);
499 /* Index expression */
500 Assert(attribute->expr != NULL);
501 indexInfo->ii_KeyAttrNumbers[attn] = 0; /* marks expression */
502 indexInfo->ii_Expressions = lappend(indexInfo->ii_Expressions,
504 atttype = exprType(attribute->expr);
507 * We don't currently support generation of an actual query plan
508 * for an index expression, only simple scalar expressions; hence
509 * these restrictions.
511 if (contain_subplans(attribute->expr))
513 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
514 errmsg("cannot use subquery in index expression")));
515 if (contain_agg_clause(attribute->expr))
517 (errcode(ERRCODE_GROUPING_ERROR),
518 errmsg("cannot use aggregate function in index expression")));
521 * A expression using mutable functions is probably wrong, since
522 * if you aren't going to get the same result for the same data
523 * every time, it's not clear what the index entries mean at all.
525 if (contain_mutable_functions(attribute->expr))
527 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
528 errmsg("functions in index expression must be marked IMMUTABLE")));
531 classOidP[attn] = GetIndexOpClass(attribute->opclass,
540 * Resolve possibly-defaulted operator class specification
543 GetIndexOpClass(List *opclass, Oid attrType,
544 char *accessMethodName, Oid accessMethodId)
553 * Release 7.0 removed network_ops, timespan_ops, and datetime_ops, so we
554 * ignore those opclass names so the default *_ops is used. This can be
555 * removed in some later release. bjm 2000/02/07
557 * Release 7.1 removes lztext_ops, so suppress that too for a while. tgl
560 * Release 7.2 renames timestamp_ops to timestamptz_ops, so suppress that
561 * too for awhile. I'm starting to think we need a better approach. tgl
564 * Release 8.0 removes bigbox_ops (which was dead code for a long while
565 * anyway). tgl 2003/11/11
567 if (list_length(opclass) == 1)
569 char *claname = strVal(linitial(opclass));
571 if (strcmp(claname, "network_ops") == 0 ||
572 strcmp(claname, "timespan_ops") == 0 ||
573 strcmp(claname, "datetime_ops") == 0 ||
574 strcmp(claname, "lztext_ops") == 0 ||
575 strcmp(claname, "timestamp_ops") == 0 ||
576 strcmp(claname, "bigbox_ops") == 0)
582 /* no operator class specified, so find the default */
583 opClassId = GetDefaultOpClass(attrType, accessMethodId);
584 if (!OidIsValid(opClassId))
586 (errcode(ERRCODE_UNDEFINED_OBJECT),
587 errmsg("data type %s has no default operator class for access method \"%s\"",
588 format_type_be(attrType), accessMethodName),
589 errhint("You must specify an operator class for the index or define a default operator class for the data type.")));
594 * Specific opclass name given, so look up the opclass.
597 /* deconstruct the name list */
598 DeconstructQualifiedName(opclass, &schemaname, &opcname);
602 /* Look in specific schema only */
605 namespaceId = LookupExplicitNamespace(schemaname);
606 tuple = SearchSysCache(CLAAMNAMENSP,
607 ObjectIdGetDatum(accessMethodId),
608 PointerGetDatum(opcname),
609 ObjectIdGetDatum(namespaceId),
614 /* Unqualified opclass name, so search the search path */
615 opClassId = OpclassnameGetOpcid(accessMethodId, opcname);
616 if (!OidIsValid(opClassId))
618 (errcode(ERRCODE_UNDEFINED_OBJECT),
619 errmsg("operator class \"%s\" does not exist for access method \"%s\"",
620 opcname, accessMethodName)));
621 tuple = SearchSysCache(CLAOID,
622 ObjectIdGetDatum(opClassId),
626 if (!HeapTupleIsValid(tuple))
628 (errcode(ERRCODE_UNDEFINED_OBJECT),
629 errmsg("operator class \"%s\" does not exist for access method \"%s\"",
630 NameListToString(opclass), accessMethodName)));
633 * Verify that the index operator class accepts this datatype. Note we
634 * will accept binary compatibility.
636 opClassId = HeapTupleGetOid(tuple);
637 opInputType = ((Form_pg_opclass) GETSTRUCT(tuple))->opcintype;
639 if (!IsBinaryCoercible(attrType, opInputType))
641 (errcode(ERRCODE_DATATYPE_MISMATCH),
642 errmsg("operator class \"%s\" does not accept data type %s",
643 NameListToString(opclass), format_type_be(attrType))));
645 ReleaseSysCache(tuple);
653 * Given the OIDs of a datatype and an access method, find the default
654 * operator class, if any. Returns InvalidOid if there is none.
657 GetDefaultOpClass(Oid type_id, Oid am_id)
661 Oid exactOid = InvalidOid;
662 Oid compatibleOid = InvalidOid;
668 /* If it's a domain, look at the base type instead */
669 type_id = getBaseType(type_id);
672 * We scan through all the opclasses available for the access method,
673 * looking for one that is marked default and matches the target type
674 * (either exactly or binary-compatibly, but prefer an exact match).
676 * We could find more than one binary-compatible match, in which case we
677 * require the user to specify which one he wants. If we find more than
678 * one exact match, then someone put bogus entries in pg_opclass.
680 rel = heap_open(OperatorClassRelationId, AccessShareLock);
682 ScanKeyInit(&skey[0],
683 Anum_pg_opclass_opcamid,
684 BTEqualStrategyNumber, F_OIDEQ,
685 ObjectIdGetDatum(am_id));
687 scan = systable_beginscan(rel, OpclassAmNameNspIndexId, true,
688 SnapshotNow, 1, skey);
690 while (HeapTupleIsValid(tup = systable_getnext(scan)))
692 Form_pg_opclass opclass = (Form_pg_opclass) GETSTRUCT(tup);
694 if (opclass->opcdefault)
696 if (opclass->opcintype == type_id)
699 exactOid = HeapTupleGetOid(tup);
701 else if (IsBinaryCoercible(type_id, opclass->opcintype))
704 compatibleOid = HeapTupleGetOid(tup);
709 systable_endscan(scan);
711 heap_close(rel, AccessShareLock);
717 (errcode(ERRCODE_DUPLICATE_OBJECT),
718 errmsg("there are multiple default operator classes for data type %s",
719 format_type_be(type_id))));
720 if (ncompatible == 1)
721 return compatibleOid;
729 * Create a name for an implicitly created index, sequence, constraint, etc.
731 * The parameters are typically: the original table name, the original field
732 * name, and a "type" string (such as "seq" or "pkey"). The field name
733 * and/or type can be NULL if not relevant.
735 * The result is a palloc'd string.
737 * The basic result we want is "name1_name2_label", omitting "_name2" or
738 * "_label" when those parameters are NULL. However, we must generate
739 * a name with less than NAMEDATALEN characters! So, we truncate one or
740 * both names if necessary to make a short-enough string. The label part
741 * is never truncated (so it had better be reasonably short).
743 * The caller is responsible for checking uniqueness of the generated
744 * name and retrying as needed; retrying will be done by altering the
745 * "label" string (which is why we never truncate that part).
748 makeObjectName(const char *name1, const char *name2, const char *label)
751 int overhead = 0; /* chars needed for label and underscores */
752 int availchars; /* chars available for name(s) */
753 int name1chars; /* chars allocated to name1 */
754 int name2chars; /* chars allocated to name2 */
757 name1chars = strlen(name1);
760 name2chars = strlen(name2);
761 overhead++; /* allow for separating underscore */
766 overhead += strlen(label) + 1;
768 availchars = NAMEDATALEN - 1 - overhead;
769 Assert(availchars > 0); /* else caller chose a bad label */
772 * If we must truncate, preferentially truncate the longer name. This
773 * logic could be expressed without a loop, but it's simple and obvious as
776 while (name1chars + name2chars > availchars)
778 if (name1chars > name2chars)
784 name1chars = pg_mbcliplen(name1, name1chars, name1chars);
786 name2chars = pg_mbcliplen(name2, name2chars, name2chars);
788 /* Now construct the string using the chosen lengths */
789 name = palloc(name1chars + name2chars + overhead + 1);
790 memcpy(name, name1, name1chars);
795 memcpy(name + ndx, name2, name2chars);
801 strcpy(name + ndx, label);
810 * Select a nonconflicting name for a new relation. This is ordinarily
811 * used to choose index names (which is why it's here) but it can also
812 * be used for sequences, or any autogenerated relation kind.
814 * name1, name2, and label are used the same way as for makeObjectName(),
815 * except that the label can't be NULL; digits will be appended to the label
816 * if needed to create a name that is unique within the specified namespace.
818 * Note: it is theoretically possible to get a collision anyway, if someone
819 * else chooses the same name concurrently. This is fairly unlikely to be
820 * a problem in practice, especially if one is holding an exclusive lock on
821 * the relation identified by name1. However, if choosing multiple names
822 * within a single command, you'd better create the new object and do
823 * CommandCounterIncrement before choosing the next one!
825 * Returns a palloc'd string.
828 ChooseRelationName(const char *name1, const char *name2,
829 const char *label, Oid namespace)
832 char *relname = NULL;
833 char modlabel[NAMEDATALEN];
835 /* try the unmodified label first */
836 StrNCpy(modlabel, label, sizeof(modlabel));
840 relname = makeObjectName(name1, name2, modlabel);
842 if (!OidIsValid(get_relname_relid(relname, namespace)))
845 /* found a conflict, so try a new name component */
847 snprintf(modlabel, sizeof(modlabel), "%s%d", label, ++pass);
854 * relationHasPrimaryKey -
856 * See whether an existing relation has a primary key.
859 relationHasPrimaryKey(Relation rel)
863 ListCell *indexoidscan;
866 * Get the list of index OIDs for the table from the relcache, and look up
867 * each one in the pg_index syscache until we find one marked primary key
868 * (hopefully there isn't more than one such).
870 indexoidlist = RelationGetIndexList(rel);
872 foreach(indexoidscan, indexoidlist)
874 Oid indexoid = lfirst_oid(indexoidscan);
875 HeapTuple indexTuple;
877 indexTuple = SearchSysCache(INDEXRELID,
878 ObjectIdGetDatum(indexoid),
880 if (!HeapTupleIsValid(indexTuple)) /* should not happen */
881 elog(ERROR, "cache lookup failed for index %u", indexoid);
882 result = ((Form_pg_index) GETSTRUCT(indexTuple))->indisprimary;
883 ReleaseSysCache(indexTuple);
888 list_free(indexoidlist);
899 RemoveIndex(RangeVar *relation, DropBehavior behavior)
903 ObjectAddress object;
905 indOid = RangeVarGetRelid(relation, false);
906 relkind = get_rel_relkind(indOid);
907 if (relkind != RELKIND_INDEX)
909 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
910 errmsg("\"%s\" is not an index",
911 relation->relname)));
913 object.classId = RelationRelationId;
914 object.objectId = indOid;
915 object.objectSubId = 0;
917 performDeletion(&object, behavior);
922 * Recreate a specific index.
925 ReindexIndex(RangeVar *indexRelation)
930 indOid = RangeVarGetRelid(indexRelation, false);
931 tuple = SearchSysCache(RELOID,
932 ObjectIdGetDatum(indOid),
934 if (!HeapTupleIsValid(tuple)) /* shouldn't happen */
935 elog(ERROR, "cache lookup failed for relation %u", indOid);
937 if (((Form_pg_class) GETSTRUCT(tuple))->relkind != RELKIND_INDEX)
939 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
940 errmsg("\"%s\" is not an index",
941 indexRelation->relname)));
943 /* Check permissions */
944 if (!pg_class_ownercheck(indOid, GetUserId()))
945 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CLASS,
946 indexRelation->relname);
948 ReleaseSysCache(tuple);
950 reindex_index(indOid);
955 * Recreate all indexes of a table (and of its toast table, if any)
958 ReindexTable(RangeVar *relation)
963 heapOid = RangeVarGetRelid(relation, false);
964 tuple = SearchSysCache(RELOID,
965 ObjectIdGetDatum(heapOid),
967 if (!HeapTupleIsValid(tuple)) /* shouldn't happen */
968 elog(ERROR, "cache lookup failed for relation %u", heapOid);
970 if (((Form_pg_class) GETSTRUCT(tuple))->relkind != RELKIND_RELATION &&
971 ((Form_pg_class) GETSTRUCT(tuple))->relkind != RELKIND_TOASTVALUE)
973 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
974 errmsg("\"%s\" is not a table",
975 relation->relname)));
977 /* Check permissions */
978 if (!pg_class_ownercheck(heapOid, GetUserId()))
979 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CLASS,
982 /* Can't reindex shared tables except in standalone mode */
983 if (((Form_pg_class) GETSTRUCT(tuple))->relisshared && IsUnderPostmaster)
985 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
986 errmsg("shared table \"%s\" can only be reindexed in stand-alone mode",
987 relation->relname)));
989 ReleaseSysCache(tuple);
991 if (!reindex_relation(heapOid, true))
993 (errmsg("table \"%s\" has no indexes",
994 relation->relname)));
999 * Recreate indexes of a database.
1001 * To reduce the probability of deadlocks, each table is reindexed in a
1002 * separate transaction, so we can release the lock on it right away.
1005 ReindexDatabase(const char *databaseName, bool do_system, bool do_user)
1007 Relation relationRelation;
1010 MemoryContext private_context;
1015 AssertArg(databaseName);
1017 if (strcmp(databaseName, get_database_name(MyDatabaseId)) != 0)
1019 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1020 errmsg("can only reindex the currently open database")));
1022 if (!pg_database_ownercheck(MyDatabaseId, GetUserId()))
1023 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
1027 * We cannot run inside a user transaction block; if we were inside a
1028 * transaction, then our commit- and start-transaction-command calls would
1029 * not have the intended effect!
1031 PreventTransactionChain((void *) databaseName, "REINDEX DATABASE");
1034 * Create a memory context that will survive forced transaction commits we
1035 * do below. Since it is a child of PortalContext, it will go away
1036 * eventually even if we suffer an error; there's no need for special
1037 * abort cleanup logic.
1039 private_context = AllocSetContextCreate(PortalContext,
1041 ALLOCSET_DEFAULT_MINSIZE,
1042 ALLOCSET_DEFAULT_INITSIZE,
1043 ALLOCSET_DEFAULT_MAXSIZE);
1046 * We always want to reindex pg_class first. This ensures that if there
1047 * is any corruption in pg_class' indexes, they will be fixed before we
1048 * process any other tables. This is critical because reindexing itself
1049 * will try to update pg_class.
1053 old = MemoryContextSwitchTo(private_context);
1054 relids = lappend_oid(relids, RelationRelationId);
1055 MemoryContextSwitchTo(old);
1059 * Scan pg_class to build a list of the relations we need to reindex.
1061 * We only consider plain relations here (toast rels will be processed
1062 * indirectly by reindex_relation).
1064 relationRelation = heap_open(RelationRelationId, AccessShareLock);
1065 scan = heap_beginscan(relationRelation, SnapshotNow, 0, NULL);
1066 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1068 Form_pg_class classtuple = (Form_pg_class) GETSTRUCT(tuple);
1070 if (classtuple->relkind != RELKIND_RELATION)
1073 /* Check user/system classification, and optionally skip */
1074 if (IsSystemClass(classtuple))
1085 if (IsUnderPostmaster) /* silently ignore shared tables */
1087 if (classtuple->relisshared)
1091 if (HeapTupleGetOid(tuple) == RelationRelationId)
1092 continue; /* got it already */
1094 old = MemoryContextSwitchTo(private_context);
1095 relids = lappend_oid(relids, HeapTupleGetOid(tuple));
1096 MemoryContextSwitchTo(old);
1099 heap_close(relationRelation, AccessShareLock);
1101 /* Now reindex each rel in a separate transaction */
1102 CommitTransactionCommand();
1105 Oid relid = lfirst_oid(l);
1107 StartTransactionCommand();
1108 /* functions in indexes may want a snapshot set */
1109 ActiveSnapshot = CopySnapshot(GetTransactionSnapshot());
1110 if (reindex_relation(relid, true))
1112 (errmsg("table \"%s\" was reindexed",
1113 get_rel_name(relid))));
1114 CommitTransactionCommand();
1116 StartTransactionCommand();
1118 MemoryContextDelete(private_context);