64#include "utils/fmgroids.h"
84 Datum *opclassOptions,
87 const List *exclusionOpNames,
89 const char *accessMethodName,
93 bool iswithoutoverlaps,
96 int *ddl_save_nestlevel);
98 const List *colnames,
const List *exclusionOpNames,
99 bool primary,
bool isconstraint);
180 const char *accessMethodName,
181 const List *attributeList,
182 const List *exclusionOpNames,
183 bool isWithoutOverlaps)
189 Datum *opclassOptions;
200 int numberOfAttributes;
216 isconstraint =
false;
219 Assert(numberOfAttributes > 0);
226 (
errcode(ERRCODE_UNDEFINED_OBJECT),
227 errmsg(
"access method \"%s\" does not exist",
230 accessMethodId = accessMethodForm->oid;
245 indexInfo =
makeIndexInfo(numberOfAttributes, numberOfAttributes,
246 accessMethodId,
NIL,
NIL,
false,
false,
247 false,
false, amsummarizing, isWithoutOverlaps);
254 typeIds, collationIds, opclassIds, opclassOptions,
255 coloptions, attributeList,
256 exclusionOpNames, relationId,
257 accessMethodName, accessMethodId,
258 amcanorder, isconstraint, isWithoutOverlaps,
InvalidOid,
264 elog(
ERROR,
"cache lookup failed for index %u", oldId);
273 indexForm->indisvalid))
280 old_natts = indexForm->indnkeyatts;
281 Assert(old_natts == numberOfAttributes);
289 ret = (memcmp(old_indclass->
values, opclassIds, old_natts *
sizeof(
Oid)) == 0 &&
290 memcmp(old_indcollation->
values, collationIds, old_natts *
sizeof(
Oid)) == 0);
299 for (
i = 0;
i < old_natts;
i++)
314 for (
i = 0;
i < old_natts;
i++)
319 pfree(oldOpclassOptions);
331 old_natts *
sizeof(
Oid)) == 0;
336 for (
i = 0;
i < old_natts && ret;
i++)
342 if ((IsPolymorphicType(left) || IsPolymorphicType(right)) &&
368 if (!opts1 && !opts2)
372 for (
i = 0;
i < natts;
i++)
377 if (opt1 == (
Datum) 0)
379 if (opt2 == (
Datum) 0)
384 else if (opt2 == (
Datum) 0)
449 for (
i = 0;
i < n_old_snapshots;
i++)
458 int n_newer_snapshots;
467 for (
j =
i;
j < n_old_snapshots;
j++)
471 for (k = 0; k < n_newer_snapshots; k++)
477 if (k >= n_newer_snapshots)
480 pfree(newer_snapshots);
547 Oid parentConstraintId,
551 bool check_not_in_use,
556 char *indexRelationName;
557 char *accessMethodName;
561 Datum *opclassOptions;
567 List *allIndexParams;
573 bool amissummarizing;
583 int numberOfAttributes;
584 int numberOfKeyAttributes;
591 Oid root_save_userid;
592 int root_save_sec_context;
593 int root_save_nestlevel;
604 if (
stmt->reset_default_tblspc)
654 stmt->indexIncludingParams);
657 if (numberOfKeyAttributes <= 0)
659 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
660 errmsg(
"must specify at least one column")));
663 (
errcode(ERRCODE_TOO_MANY_COLUMNS),
664 errmsg(
"cannot use more than %d columns in an index",
700 exclusion =
stmt->excludeOpNames ||
stmt->iswithoutoverlaps;
703 switch (rel->
rd_rel->relkind)
705 case RELKIND_RELATION:
706 case RELKIND_MATVIEW:
707 case RELKIND_PARTITIONED_TABLE:
712 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
713 errmsg(
"cannot create index on relation \"%s\"",
727 partitioned = rel->
rd_rel->relkind == RELKIND_PARTITIONED_TABLE;
736 if (
stmt->concurrent)
738 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
739 errmsg(
"cannot create index on partitioned table \"%s\" concurrently",
748 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
749 errmsg(
"cannot create indexes on temporary tables of other sessions")));
757 if (check_not_in_use)
770 aclresult =
object_aclcheck(NamespaceRelationId, namespaceId, root_save_userid,
781 if (
stmt->tableSpace)
786 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
787 errmsg(
"cannot specify default tablespace for partitioned relations")));
802 aclresult =
object_aclcheck(TableSpaceRelationId, tablespaceId, root_save_userid,
814 if (rel->
rd_rel->relisshared)
815 tablespaceId = GLOBALTABLESPACE_OID;
816 else if (tablespaceId == GLOBALTABLESPACE_OID)
818 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
819 errmsg(
"only shared relations can be placed in pg_global tablespace")));
829 indexRelationName =
stmt->idxname;
830 if (indexRelationName == NULL)
834 stmt->excludeOpNames,
841 accessMethodName =
stmt->accessMethod;
849 if (strcmp(accessMethodName,
"rtree") == 0)
852 (
errmsg(
"substituting access method \"gist\" for obsolete method \"rtree\"")));
853 accessMethodName =
"gist";
859 (
errcode(ERRCODE_UNDEFINED_OBJECT),
860 errmsg(
"access method \"%s\" does not exist",
864 accessMethodId = accessMethodForm->oid;
872 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
873 errmsg(
"access method \"%s\" does not support unique indexes",
877 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
878 errmsg(
"access method \"%s\" does not support included columns",
882 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
883 errmsg(
"access method \"%s\" does not support multicolumn indexes",
885 if (exclusion && amRoutine->
amgettuple == NULL)
887 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
888 errmsg(
"access method \"%s\" does not support exclusion constraints",
890 if (
stmt->iswithoutoverlaps && strcmp(accessMethodName,
"gist") != 0)
892 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
893 errmsg(
"access method \"%s\" does not support WITHOUT OVERLAPS constraints",
906 if (
stmt->whereClause)
913 NULL, NULL,
false,
false);
923 numberOfKeyAttributes,
928 stmt->nulls_not_distinct,
932 stmt->iswithoutoverlaps);
940 typeIds, collationIds, opclassIds, opclassOptions,
941 coloptions, allIndexParams,
942 stmt->excludeOpNames, tableId,
943 accessMethodName, accessMethodId,
944 amcanorder,
stmt->isconstraint,
stmt->iswithoutoverlaps,
945 root_save_userid, root_save_sec_context,
946 &root_save_nestlevel);
964 if (partitioned && (
stmt->unique || exclusion))
967 const char *constraint_type;
971 constraint_type =
"PRIMARY KEY";
972 else if (
stmt->unique)
973 constraint_type =
"UNIQUE";
974 else if (
stmt->excludeOpNames)
975 constraint_type =
"EXCLUDE";
979 constraint_type = NULL;
986 for (
i = 0;
i <
key->partnatts;
i++)
1005 key->partopcintype[
i],
1006 key->partopcintype[
i],
1009 elog(
ERROR,
"missing operator %d(%u,%u) in partition opfamily %u",
1010 eq_strategy,
key->partopcintype[
i],
key->partopcintype[
i],
1011 key->partopfamily[
i]);
1021 if (
stmt->unique && !
stmt->iswithoutoverlaps && accessMethodId != BTREE_AM_OID)
1023 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1024 errmsg(
"cannot match partition key to an index using access method \"%s\"",
1025 accessMethodName)));
1031 if (
key->partattrs[
i] == 0)
1033 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1034 errmsg(
"unsupported %s constraint with partition key definition",
1036 errdetail(
"%s constraints cannot be used when partition keys include expressions.",
1051 if (
key->partcollation[
i] != collationIds[
j])
1060 if (
stmt->unique && !
stmt->iswithoutoverlaps)
1069 if (ptkey_eqop == idx_eqop)
1085 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1086 errmsg(
"cannot match partition key to index on column \"%s\" using non-equal operator \"%s\"",
1099 key->partattrs[
i] - 1);
1101 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1102 errmsg(
"unique constraint on partitioned table must include all partitioning columns"),
1103 errdetail(
"%s constraint on table \"%s\" lacks column \"%s\" which is part of the partition key.",
1121 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1122 errmsg(
"index creation on system columns is not supported")));
1140 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1141 errmsg(
"index creation on system columns is not supported")));
1153 if (
stmt->isconstraint && !quiet)
1155 const char *constraint_type;
1158 constraint_type =
"PRIMARY KEY";
1159 else if (
stmt->unique)
1160 constraint_type =
"UNIQUE";
1161 else if (
stmt->excludeOpNames)
1162 constraint_type =
"EXCLUDE";
1166 constraint_type = NULL;
1170 (
errmsg_internal(
"%s %s will create implicit index \"%s\" for table \"%s\"",
1171 is_alter_table ?
"ALTER TABLE / ADD" :
"CREATE TABLE /",
1188 flags = constr_flags = 0;
1189 if (
stmt->isconstraint)
1191 if (skip_build || concurrent || partitioned)
1193 if (
stmt->if_not_exists)
1206 if (partitioned &&
stmt->relation && !
stmt->relation->inh)
1214 if (
stmt->deferrable)
1216 if (
stmt->initdeferred)
1218 if (
stmt->iswithoutoverlaps)
1222 index_create(rel, indexRelationName, indexRelationId, parentIndexId,
1224 stmt->oldNumber, indexInfo, indexColNames,
1225 accessMethodId, tablespaceId,
1226 collationIds, opclassIds, opclassOptions,
1227 coloptions, NULL, reloptions,
1228 flags, constr_flags,
1230 &createdConstraintId);
1264 if (
stmt->idxcomment != NULL)
1279 if ((!
stmt->relation ||
stmt->relation->inh) && partdesc->
nparts > 0)
1281 int nparts = partdesc->
nparts;
1283 bool invalidate_parent =
false;
1306 if (total_parts < 0)
1319 memcpy(part_oids, partdesc->
oids,
sizeof(
Oid) * nparts);
1329 parentIndex =
index_open(indexRelationId, lockmode);
1342 for (
int i = 0;
i < nparts;
i++)
1344 Oid childRelid = part_oids[
i];
1346 Oid child_save_userid;
1347 int child_save_sec_context;
1348 int child_save_nestlevel;
1357 &child_save_sec_context);
1368 if (childrel->
rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1372 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1373 errmsg(
"cannot create unique index on partitioned table \"%s\"",
1375 errdetail(
"Table \"%s\" contains partitions that are foreign tables.",
1380 child_save_sec_context);
1391 foreach(cell, childidxs)
1437 createdConstraintId,
1441 invalidate_parent =
true;
1467 child_save_sec_context);
1496 root_save_sec_context);
1501 createdConstraintId,
1503 is_alter_table, check_rights,
1507 child_save_sec_context);
1515 invalidate_parent =
true;
1528 if (invalidate_parent)
1537 elog(
ERROR,
"cache lookup failed for index %u",
1633 const int progress_cols[] = {
1637 const int64 progress_vals[] = {
1740 limitXmin = snapshot->
xmin;
1832 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1833 errmsg(
"functions in index predicate must be marked IMMUTABLE")));
1850 Datum *opclassOptions,
1852 const List *attList,
1853 const List *exclusionOpNames,
1855 const char *accessMethodName,
1859 bool iswithoutoverlaps,
1861 int ddl_sec_context,
1862 int *ddl_save_nestlevel)
1869 int save_sec_context;
1872 if (exclusionOpNames)
1878 nextExclOp =
list_head(exclusionOpNames);
1889 if (iswithoutoverlaps)
1891 if (exclusionOpNames ==
NIL)
1907 foreach(lc, attList)
1916 if (attribute->
name != NULL)
1929 (
errcode(ERRCODE_UNDEFINED_COLUMN),
1930 errmsg(
"column \"%s\" named in key does not exist",
1934 (
errcode(ERRCODE_UNDEFINED_COLUMN),
1935 errmsg(
"column \"%s\" does not exist",
1940 atttype = attform->atttypid;
1941 attcollation = attform->attcollation;
1951 if (attn >= nkeycols)
1953 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1954 errmsg(
"expressions are not supported in included columns")));
1994 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1995 errmsg(
"functions in index expression must be marked IMMUTABLE")));
1999 typeOids[attn] = atttype;
2005 if (attn >= nkeycols)
2009 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2010 errmsg(
"including column does not support a collation")));
2013 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2014 errmsg(
"including column does not support an operator class")));
2017 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2018 errmsg(
"including column does not support ASC/DESC options")));
2021 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2022 errmsg(
"including column does not support NULLS FIRST/LAST options")));
2025 opclassOptions[attn] = (
Datum) 0;
2026 colOptions[attn] = 0;
2064 (
errcode(ERRCODE_INDETERMINATE_COLLATION),
2065 errmsg(
"could not determine which collation to use for index expression"),
2066 errhint(
"Use the COLLATE clause to set the collation explicitly.")));
2072 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2073 errmsg(
"collations are not supported by type %s",
2077 collationOids[attn] = attcollation;
2139 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
2140 errmsg(
"operator %s is not commutative",
2142 errdetail(
"Only commutative operators can be used in exclusion constraints.")));
2162 elog(
ERROR,
"cache lookup failed for opfamily %u",
2167 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
2168 errmsg(
"operator %s is not a member of operator family \"%s\"",
2171 errdetail(
"The exclusion operator must be related to the index operator class for the constraint.")));
2177 nextExclOp =
lnext(exclusionOpNames, nextExclOp);
2179 else if (iswithoutoverlaps)
2185 if (attn == nkeycols - 1)
2200 colOptions[attn] = 0;
2205 colOptions[attn] |= INDOPTION_DESC;
2210 colOptions[attn] |= INDOPTION_NULLS_FIRST;
2213 colOptions[attn] |= INDOPTION_NULLS_FIRST;
2220 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2221 errmsg(
"access method \"%s\" does not support ASC/DESC options",
2222 accessMethodName)));
2225 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2226 errmsg(
"access method \"%s\" does not support NULLS FIRST/LAST options",
2227 accessMethodName)));
2235 opclassOptions[attn] =
2237 NULL, NULL,
false,
false);
2240 opclassOptions[attn] = (
Datum) 0;
2254 const char *accessMethodName,
Oid accessMethodId)
2269 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2270 errmsg(
"data type %s has no default operator class for access method \"%s\"",
2272 errhint(
"You must specify an operator class for the index or define a default operator class for the data type.")));
2300 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2301 errmsg(
"operator class \"%s\" does not exist for access method \"%s\"",
2302 opcname, accessMethodName)));
2308 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2309 errmsg(
"operator class \"%s\" does not exist for access method \"%s\"",
2317 opClassId = opform->oid;
2318 opInputType = opform->opcintype;
2322 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2323 errmsg(
"operator class \"%s\" does not accept data type %s",
2342 int ncompatible = 0;
2343 int ncompatiblepreferred = 0;
2370 Anum_pg_opclass_opcmethod,
2382 if (!opclass->opcdefault)
2384 if (opclass->opcintype == type_id)
2387 result = opclass->oid;
2389 else if (nexact == 0 &&
2394 ncompatiblepreferred++;
2395 result = opclass->oid;
2397 else if (ncompatiblepreferred == 0)
2400 result = opclass->oid;
2413 errmsg(
"there are multiple default operator classes for data type %s",
2417 ncompatiblepreferred == 1 ||
2418 (ncompatiblepreferred == 0 && ncompatible == 1))
2465 elog(
ERROR,
"cache lookup failed for operator class %u", opclass);
2468 errcode(ERRCODE_UNDEFINED_OBJECT),
2472 errdetail(
"Could not translate compare type %d for operator class \"%s\" for access method \"%s\".",
2482 rhstype = opcintype;
2492 elog(
ERROR,
"cache lookup failed for operator family %u", opfamily);
2495 errcode(ERRCODE_UNDEFINED_OBJECT),
2499 errdetail(
"There is no suitable operator in operator family \"%s\" for access method \"%s\".",
2536 name1chars = strlen(name1);
2539 name2chars = strlen(name2);
2545 overhead += strlen(
label) + 1;
2555 while (name1chars + name2chars > availchars)
2557 if (name1chars > name2chars)
2563 name1chars =
pg_mbcliplen(name1, name1chars, name1chars);
2565 name2chars =
pg_mbcliplen(name2, name2chars, name2chars);
2568 name =
palloc(name1chars + name2chars + overhead + 1);
2569 memcpy(
name, name1, name1chars);
2574 memcpy(
name + ndx, name2, name2chars);
2614 const char *
label,
Oid namespaceid,
2630 if (!isconstraint ||
2637 snprintf(modlabel,
sizeof(modlabel),
"%s%d",
label, ++pass);
2650 const List *colnames,
const List *exclusionOpNames,
2651 bool primary,
bool isconstraint)
2664 else if (exclusionOpNames !=
NIL)
2672 else if (isconstraint)
2711 foreach(lc, colnames)
2713 const char *
name = (
const char *)
lfirst(lc);
2716 buf[buflen++] =
'_';
2723 buflen += strlen(
buf + buflen);
2743 foreach(lc, indexElems)
2746 const char *origname;
2747 const char *curname;
2754 else if (ielem->
name)
2755 origname = ielem->
name;
2767 foreach(lc2, result)
2769 if (strcmp(curname, (
char *)
lfirst(lc2)) == 0)
2780 memcpy(
buf, origname, nlen);
2781 strcpy(
buf + nlen, nbuf);
2803 bool concurrently =
false;
2805 char *tablespacename = NULL;
2808 foreach(lc,
stmt->params)
2812 if (strcmp(opt->
defname,
"verbose") == 0)
2814 else if (strcmp(opt->
defname,
"concurrently") == 0)
2816 else if (strcmp(opt->
defname,
"tablespace") == 0)
2820 (
errcode(ERRCODE_SYNTAX_ERROR),
2821 errmsg(
"unrecognized REINDEX option \"%s\"",
2828 "REINDEX CONCURRENTLY");
2838 if (tablespacename != NULL)
2879 "REINDEX DATABASE");
2883 elog(
ERROR,
"unrecognized object type: %d",
2928 if (relkind == RELKIND_PARTITIONED_INDEX)
2931 persistence != RELPERSISTENCE_TEMP)
2969 if (relId != oldRelId &&
OidIsValid(oldRelId))
2987 if (relkind != RELKIND_INDEX &&
2988 relkind != RELKIND_PARTITIONED_INDEX)
2990 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
3005 if (relId != oldRelId)
3014 state->locked_table_oid = table_oid;
3053 (
errmsg(
"table \"%s\" has no indexes that can be reindexed concurrently",
3067 (
errmsg(
"table \"%s\" has no indexes to reindex",
3095 bool concurrent_warning =
false;
3096 bool tablespace_warning =
false;
3097 const char *objectName =
stmt->name;
3113 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3114 errmsg(
"cannot reindex system catalogs concurrently")));
3137 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3138 errmsg(
"can only reindex the currently open database")));
3152 "ReindexMultipleTables",
3164 Anum_pg_class_relnamespace,
3182 Oid relid = classtuple->oid;
3191 if (classtuple->relkind != RELKIND_RELATION &&
3192 classtuple->relkind != RELKIND_MATVIEW)
3196 if (classtuple->relpersistence == RELPERSISTENCE_TEMP &&
3216 if (classtuple->relisshared &&
3227 if (!concurrent_warning)
3229 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3230 errmsg(
"cannot reindex system catalogs concurrently, skipping all")));
3231 concurrent_warning =
true;
3241 bool skip_rel =
false;
3247 if (RELKIND_HAS_STORAGE(classtuple->relkind) &&
3260 if (!tablespace_warning)
3262 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3263 errmsg(
"cannot move system relations, skipping all")));
3264 tablespace_warning =
true;
3279 if (relid == RelationRelationId)
3308 if (errinfo->
relkind == RELKIND_PARTITIONED_TABLE)
3309 errcontext(
"while reindexing partitioned table \"%s.%s\"",
3311 else if (errinfo->
relkind == RELKIND_PARTITIONED_INDEX)
3312 errcontext(
"while reindexing partitioned index \"%s.%s\"",
3335 Assert(RELKIND_HAS_PARTITIONS(relkind));
3345 errcallback.
arg = &errinfo;
3350 relkind == RELKIND_PARTITIONED_TABLE ?
3351 "REINDEX TABLE" :
"REINDEX INDEX");
3373 foreach(lc, inhoids)
3383 if (!RELKIND_HAS_STORAGE(partkind))
3386 Assert(partkind == RELKIND_INDEX ||
3387 partkind == RELKIND_RELATION);
3428 char relpersistence;
3468 Assert(!RELKIND_HAS_PARTITIONS(relkind));
3471 relpersistence != RELPERSISTENCE_TEMP)
3481 else if (relkind == RELKIND_INDEX)
3505 (
errmsg(
"table \"%s.%s\" was reindexed",
3545 typedef struct ReindexIndexInfo
3562 char *relationName = NULL;
3563 char *relationNamespace = NULL;
3565 const int progress_index[] = {
3571 int64 progress_vals[4];
3580 "ReindexConcurrent",
3604 case RELKIND_RELATION:
3605 case RELKIND_MATVIEW:
3606 case RELKIND_TOASTVALUE:
3618 heapRelationIds =
lappend_oid(heapRelationIds, relationOid);
3624 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3625 errmsg(
"cannot reindex system catalogs concurrently")));
3643 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3644 errmsg(
"cannot move system relation \"%s\"",
3654 if (!indexRelation->
rd_index->indisvalid)
3656 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3657 errmsg(
"skipping reindex of invalid index \"%s.%s\"",
3660 errhint(
"Use DROP INDEX or REINDEX INDEX.")));
3661 else if (indexRelation->
rd_index->indisexclusion)
3663 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3664 errmsg(
"cannot reindex exclusion constraint index \"%s.%s\" concurrently, skipping",
3669 ReindexIndexInfo *
idx;
3675 idx->indexId = cellOid;
3689 Oid toastOid = heapRelation->
rd_rel->reltoastrelid;
3697 heapRelationIds =
lappend_oid(heapRelationIds, toastOid);
3707 if (!indexRelation->
rd_index->indisvalid)
3709 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3710 errmsg(
"skipping reindex of invalid index \"%s.%s\"",
3713 errhint(
"Use DROP INDEX or REINDEX INDEX.")));
3716 ReindexIndexInfo *
idx;
3725 idx->indexId = cellOid;
3746 ReindexIndexInfo *
idx;
3754 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3755 errmsg(
"cannot reindex system catalogs concurrently")));
3765 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3766 errmsg(
"cannot reindex invalid index on TOAST table")));
3789 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3790 errmsg(
"cannot move system relation \"%s\"",
3806 idx->indexId = relationOid;
3814 case RELKIND_PARTITIONED_TABLE:
3815 case RELKIND_PARTITIONED_INDEX:
3819 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
3820 errmsg(
"cannot reindex this type of relation concurrently")));
3831 if (indexIds ==
NIL)
3837 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3838 errmsg(
"cannot move non-shared relation to tablespace \"%s\"",
3868 foreach(lc, indexIds)
3870 char *concurrentName;
3872 ReindexIndexInfo *newidx;
3877 int save_sec_context;
3902#ifdef USE_INJECTION_POINTS
3913 if (indexRel->
rd_rel->relpersistence == RELPERSISTENCE_TEMP)
3914 elog(
ERROR,
"cannot reindex a temporary table concurrently");
3919 progress_vals[1] = 0;
3920 progress_vals[2] =
idx->indexId;
3921 progress_vals[3] =
idx->amId;
3933 heapRel->
rd_rel->relkind != RELKIND_TOASTVALUE)
3936 tablespaceid = indexRel->
rd_rel->reltablespace;
3956 newidx->indexId = newIndexId;
3957 newidx->safe =
idx->safe;
3958 newidx->tableId =
idx->tableId;
3959 newidx->amId =
idx->amId;
3961 newIndexIds =
lappend(newIndexIds, newidx);
3971 relationLocks =
lappend(relationLocks, lockrelid);
3974 relationLocks =
lappend(relationLocks, lockrelid);
4008 foreach(lc, heapRelationIds)
4020 relationLocks =
lappend(relationLocks, lockrelid);
4026 lockTags =
lappend(lockTags, heaplocktag);
4035 foreach(lc, relationLocks)
4066 foreach(lc, newIndexIds)
4068 ReindexIndexInfo *newidx =
lfirst(lc);
4094 progress_vals[2] = newidx->indexId;
4095 progress_vals[3] = newidx->amId;
4125 foreach(lc, newIndexIds)
4127 ReindexIndexInfo *newidx =
lfirst(lc);
4158 progress_vals[2] = newidx->indexId;
4159 progress_vals[3] = newidx->amId;
4168 limitXmin = snapshot->
xmin;
4217 forboth(lc, indexIds, lc2, newIndexIds)
4219 ReindexIndexInfo *oldidx =
lfirst(lc);
4220 ReindexIndexInfo *newidx =
lfirst(lc2);
4290 foreach(lc, indexIds)
4292 ReindexIndexInfo *oldidx =
lfirst(lc);
4337 foreach(lc, indexIds)
4342 object.
classId = RelationRelationId;
4343 object.objectId =
idx->indexId;
4344 object.objectSubId = 0;
4363 foreach(lc, relationLocks)
4376 if (relkind == RELKIND_INDEX)
4378 (
errmsg(
"index \"%s.%s\" was reindexed",
4379 relationNamespace, relationName),
4384 foreach(lc, newIndexIds)
4387 Oid indOid =
idx->indexId;
4390 (
errmsg(
"index \"%s.%s\" was reindexed",
4397 (
errmsg(
"table \"%s.%s\" was reindexed",
4398 relationNamespace, relationName),
4428 Assert(partitionIdx->
rd_rel->relkind == RELKIND_INDEX ||
4429 partitionIdx->
rd_rel->relkind == RELKIND_PARTITIONED_INDEX);
4436 Anum_pg_inherits_inhrelid,
4440 Anum_pg_inherits_inhseqno,
4482 if (inhForm->inhparent != parentOid)
4485 elog(
ERROR,
"bogus pg_inherit row: inhrelid %u inhparent %u",
4486 inhForm->inhrelid, inhForm->inhparent);
4559 elog(
ERROR,
"cache lookup failed for relation %u", relationId);
Datum idx(PG_FUNCTION_ARGS)
bool has_privs_of_role(Oid member, Oid role)
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
IndexAmRoutine * GetIndexAmRoutine(Oid amhandler)
bytea *(* amoptions_function)(Datum reloptions, bool validate)
void free_attrmap(AttrMap *map)
AttrMap * build_attrmap_by_name(TupleDesc indesc, TupleDesc outdesc, bool missing_ok)
#define InvalidAttrNumber
char * get_tablespace_name(Oid spc_oid)
Oid get_tablespace_oid(const char *tablespacename, bool missing_ok)
Oid GetDefaultTablespace(char relpersistence, bool partitioned)
void pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
void pgstat_progress_incr_param(int index, int64 incr)
void pgstat_progress_update_param(int index, int64 val)
void pgstat_progress_update_multi_param(int nparam, const int *index, const int64 *val)
void pgstat_progress_end_command(void)
@ PROGRESS_COMMAND_CREATE_INDEX
bool bms_is_member(int x, const Bitmapset *a)
#define Assert(condition)
#define OidIsValid(objectId)
bool IsToastNamespace(Oid namespaceId)
bool IsSystemRelation(Relation relation)
bool IsCatalogRelationOid(Oid relid)
bool IsSystemClass(Oid relid, Form_pg_class reltuple)
bool contain_mutable_functions_after_planning(Expr *expr)
char * get_database_name(Oid dbid)
char * defGetString(DefElem *def)
bool defGetBoolean(DefElem *def)
void performMultipleDeletions(const ObjectAddresses *objects, DropBehavior behavior, int flags)
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
ObjectAddresses * new_object_addresses(void)
@ DEPENDENCY_PARTITION_PRI
@ DEPENDENCY_PARTITION_SEC
#define PERFORM_DELETION_CONCURRENT_LOCK
#define PERFORM_DELETION_INTERNAL
int errmsg_internal(const char *fmt,...)
int errdetail(const char *fmt,...)
ErrorContextCallback * error_context_stack
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
void EventTriggerCollectSimpleCommand(ObjectAddress address, ObjectAddress secondaryObject, Node *parsetree)
#define palloc_object(type)
#define palloc_array(type, count)
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
void fmgr_info(Oid functionId, FmgrInfo *finfo)
void systable_endscan(SysScanDesc sysscan)
HeapTuple systable_getnext(SysScanDesc sysscan)
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
StrategyNumber GistTranslateStratnum(Oid opclass, CompareType cmptype)
bool allowSystemTableMods
int NewGUCNestLevel(void)
void RestrictSearchPath(void)
void AtEOXact_GUC(bool isCommit, int nestLevel)
int set_config_option(const char *name, const char *value, GucContext context, GucSource source, GucAction action, bool changeVal, int elevel, bool is_reload)
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
HeapTuple heap_copytuple(HeapTuple tuple)
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
void heap_freetuple(HeapTuple htup)
#define HeapTupleIsValid(tuple)
void validate_index(Oid heapId, Oid indexId, Snapshot snapshot)
Oid IndexGetRelation(Oid indexId, bool missing_ok)
void index_concurrently_set_dead(Oid heapId, Oid indexId)
Oid index_create(Relation heapRelation, const char *indexRelationName, Oid indexRelationId, Oid parentIndexRelid, Oid parentConstraintId, RelFileNumber relFileNumber, IndexInfo *indexInfo, const List *indexColNames, Oid accessMethodId, Oid tableSpaceId, const Oid *collationIds, const Oid *opclassIds, const Datum *opclassOptions, const int16 *coloptions, const NullableDatum *stattargets, Datum reloptions, bits16 flags, bits16 constr_flags, bool allow_system_table_mods, bool is_internal, Oid *constraintId)
void index_concurrently_swap(Oid newIndexId, Oid oldIndexId, const char *oldName)
void index_set_state_flags(Oid indexId, IndexStateFlagsAction action)
bool CompareIndexInfo(const IndexInfo *info1, const IndexInfo *info2, const Oid *collations1, const Oid *collations2, const Oid *opfamilies1, const Oid *opfamilies2, const AttrMap *attmap)
bool reindex_relation(const ReindexStmt *stmt, Oid relid, int flags, const ReindexParams *params)
IndexInfo * BuildIndexInfo(Relation index)
Oid index_concurrently_create_copy(Relation heapRelation, Oid oldIndexId, Oid tablespaceOid, const char *newName)
void index_check_primary_key(Relation heapRel, const IndexInfo *indexInfo, bool is_alter_table, const IndexStmt *stmt)
void index_concurrently_build(Oid heapRelationId, Oid indexRelationId)
void reindex_index(const ReindexStmt *stmt, Oid indexId, bool skip_constraint_checks, char persistence, const ReindexParams *params)
#define INDEX_CREATE_IS_PRIMARY
#define INDEX_CREATE_IF_NOT_EXISTS
#define REINDEX_REL_PROCESS_TOAST
#define INDEX_CREATE_PARTITIONED
#define REINDEXOPT_CONCURRENTLY
#define REINDEXOPT_MISSING_OK
#define INDEX_CREATE_INVALID
#define INDEX_CONSTR_CREATE_WITHOUT_OVERLAPS
#define INDEX_CREATE_ADD_CONSTRAINT
#define INDEX_CREATE_SKIP_BUILD
#define INDEX_CONSTR_CREATE_DEFERRABLE
#define REINDEXOPT_REPORT_PROGRESS
#define INDEX_CONSTR_CREATE_INIT_DEFERRED
#define INDEX_CREATE_CONCURRENT
#define REINDEXOPT_VERBOSE
#define REINDEX_REL_CHECK_CONSTRAINTS
void index_close(Relation relation, LOCKMODE lockmode)
Relation index_open(Oid relationId, LOCKMODE lockmode)
static bool ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const ReindexParams *params)
void ExecReindex(ParseState *pstate, const ReindexStmt *stmt, bool isTopLevel)
ObjectAddress DefineIndex(Oid tableId, IndexStmt *stmt, Oid indexRelationId, Oid parentIndexId, Oid parentConstraintId, int total_parts, bool is_alter_table, bool check_rights, bool check_not_in_use, bool skip_build, bool quiet)
static void set_indexsafe_procflags(void)
char * ChooseRelationName(const char *name1, const char *name2, const char *label, Oid namespaceid, bool isconstraint)
static void reindex_error_callback(void *arg)
static void ComputeIndexAttrs(IndexInfo *indexInfo, Oid *typeOids, Oid *collationOids, Oid *opclassOids, Datum *opclassOptions, int16 *colOptions, const List *attList, const List *exclusionOpNames, Oid relId, const char *accessMethodName, Oid accessMethodId, bool amcanorder, bool isconstraint, bool iswithoutoverlaps, Oid ddl_userid, int ddl_sec_context, int *ddl_save_nestlevel)
static void ReindexIndex(const ReindexStmt *stmt, const ReindexParams *params, bool isTopLevel)
void IndexSetParentIndex(Relation partitionIdx, Oid parentOid)
char * makeObjectName(const char *name1, const char *name2, const char *label)
Oid GetDefaultOpClass(Oid type_id, Oid am_id)
static char * ChooseIndexNameAddition(const List *colnames)
static void ReindexMultipleTables(const ReindexStmt *stmt, const ReindexParams *params)
static bool CompareOpclassOptions(const Datum *opts1, const Datum *opts2, int natts)
static void update_relispartition(Oid relationId, bool newval)
bool CheckIndexCompatible(Oid oldId, const char *accessMethodName, const List *attributeList, const List *exclusionOpNames, bool isWithoutOverlaps)
void WaitForOlderSnapshots(TransactionId limitXmin, bool progress)
Oid ResolveOpClass(const List *opclass, Oid attrType, const char *accessMethodName, Oid accessMethodId)
static void ReindexPartitions(const ReindexStmt *stmt, Oid relid, const ReindexParams *params, bool isTopLevel)
struct ReindexErrorInfo ReindexErrorInfo
static Oid ReindexTable(const ReindexStmt *stmt, const ReindexParams *params, bool isTopLevel)
static void CheckPredicate(Expr *predicate)
static void ReindexMultipleInternal(const ReindexStmt *stmt, const List *relids, const ReindexParams *params)
static void RangeVarCallbackForReindexIndex(const RangeVar *relation, Oid relId, Oid oldRelId, void *arg)
static List * ChooseIndexColumnNames(const List *indexElems)
void GetOperatorFromCompareType(Oid opclass, Oid rhstype, CompareType cmptype, Oid *opid, StrategyNumber *strat)
static char * ChooseIndexName(const char *tabname, Oid namespaceId, const List *colnames, const List *exclusionOpNames, bool primary, bool isconstraint)
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
#define INJECTION_POINT(name)
void CacheInvalidateRelcacheByRelid(Oid relid)
List * lcons_oid(Oid datum, List *list)
List * lappend(List *list, void *datum)
List * list_concat_copy(const List *list1, const List *list2)
List * lappend_oid(List *list, Oid datum)
void list_free(List *list)
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
void WaitForLockersMultiple(List *locktags, LOCKMODE lockmode, bool progress)
void LockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
void LockRelationOid(Oid relid, LOCKMODE lockmode)
void WaitForLockers(LOCKTAG heaplocktag, LOCKMODE lockmode, bool progress)
void UnlockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
void UnlockTuple(Relation relation, ItemPointer tid, LOCKMODE lockmode)
bool VirtualXactLock(VirtualTransactionId vxid, bool wait)
#define VirtualTransactionIdIsValid(vxid)
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
#define VirtualTransactionIdEquals(vxid1, vxid2)
#define SetInvalidVirtualTransactionId(vxid)
#define AccessExclusiveLock
#define InplaceUpdateTupleLock
#define ShareUpdateExclusiveLock
char * get_rel_name(Oid relid)
char get_rel_persistence(Oid relid)
bool get_index_isvalid(Oid index_oid)
Oid get_opclass_input_type(Oid opclass)
Oid get_opclass_family(Oid opclass)
bool get_opclass_opfamily_and_input_type(Oid opclass, Oid *opfamily, Oid *opcintype)
char * get_opname(Oid opno)
Datum get_attoptions(Oid relid, int16 attnum)
char get_rel_relkind(Oid relid)
Oid get_rel_namespace(Oid relid)
RegProcedure get_opcode(Oid opno)
int get_op_opfamily_strategy(Oid opno, Oid opfamily)
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
bool type_is_collatable(Oid typid)
Oid getBaseType(Oid typid)
char * get_namespace_name(Oid nspid)
Oid get_relname_relid(const char *relname, Oid relnamespace)
Oid get_commutator(Oid opno)
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
void LWLockRelease(LWLock *lock)
IndexInfo * makeIndexInfo(int numattrs, int numkeyattrs, Oid amoid, List *expressions, List *predicates, bool unique, bool nulls_not_distinct, bool isready, bool concurrent, bool summarizing, bool withoutoverlaps)
List * make_ands_implicit(Expr *clause)
int pg_mbcliplen(const char *mbstr, int len, int limit)
char * pstrdup(const char *in)
void pfree(void *pointer)
void MemoryContextDelete(MemoryContext context)
MemoryContext PortalContext
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
#define ALLOCSET_SMALL_SIZES
#define IsBootstrapProcessingMode()
#define SECURITY_RESTRICTED_OPERATION
#define CHECK_FOR_INTERRUPTS()
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
void SetUserIdAndSecContext(Oid userid, int sec_context)
Oid OpclassnameGetOpcid(Oid amid, const char *opcname)
char * NameListToString(const List *names)
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
bool isTempNamespace(Oid namespaceId)
Oid get_collation_oid(List *collname, bool missing_ok)
void DeconstructQualifiedName(const List *names, char **nspname_p, char **objname_p)
Oid get_namespace_oid(const char *nspname, bool missing_ok)
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
Oid exprType(const Node *expr)
Oid exprCollation(const Node *expr)
#define IsA(nodeptr, _type_)
const ObjectAddress InvalidObjectAddress
#define ObjectAddressSet(addr, class_id, object_id)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
TYPCATEGORY TypeCategory(Oid type)
bool IsBinaryCoercible(Oid srctype, Oid targettype)
bool IsPreferredType(TYPCATEGORY category, Oid type)
int parser_errposition(ParseState *pstate, int location)
Oid compatible_oper_opid(List *op, Oid arg1, Oid arg2, bool noError)
IndexStmt * generateClonedIndexStmt(RangeVar *heapRel, Relation source_idx, const AttrMap *attmap, Oid *constraintOid)
@ PARTITION_STRATEGY_HASH
@ REINDEX_OBJECT_DATABASE
PartitionKey RelationGetPartitionKey(Relation rel)
PartitionDesc RelationGetPartitionDesc(Relation rel, bool omit_detached)
FormData_pg_am * Form_pg_am
FormData_pg_attribute * Form_pg_attribute
static void fix_dependencies(ArchiveHandle *AH)
int errdetail_relkind_not_supported(char relkind)
FormData_pg_class * Form_pg_class
Oid get_relation_idx_constraint_oid(Oid relationId, Oid indexId)
void ConstraintSetParentConstraint(Oid childConstrId, Oid parentConstrId, Oid childTableId)
bool ConstraintNameExists(const char *conname, Oid namespaceid)
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
long deleteDependencyRecordsForClass(Oid classId, Oid objectId, Oid refclassId, char deptype)
FormData_pg_index * Form_pg_index
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
void StoreSingleInheritance(Oid relationId, Oid parentOid, int32 seqNumber)
bool has_superclass(Oid relationId)
FormData_pg_inherits * Form_pg_inherits
static int list_length(const List *l)
#define forboth(cell1, list1, cell2, list2)
#define list_make1_oid(x1)
static ListCell * list_head(const List *l)
static ListCell * lnext(const List *l, const ListCell *c)
FormData_pg_opclass * Form_pg_opclass
FormData_pg_opfamily * Form_pg_opfamily
const char * pg_rusage_show(const PGRUsage *ru0)
void pg_rusage_init(PGRUsage *ru0)
size_t strlcpy(char *dst, const char *src, size_t siz)
static bool DatumGetBool(Datum X)
static Datum PointerGetDatum(const void *X)
static Datum ObjectIdGetDatum(Oid X)
static Pointer DatumGetPointer(Datum X)
static Datum Int32GetDatum(int32 X)
#define PROC_IS_AUTOVACUUM
VirtualTransactionId * GetCurrentVirtualXIDs(TransactionId limitXmin, bool excludeXmin0, bool allDbs, int excludeVacuum, int *nvxids)
PGPROC * ProcNumberGetProc(ProcNumber procNumber)
#define PROGRESS_CREATEIDX_PHASE_WAIT_4
#define PROGRESS_CREATEIDX_PHASE_BUILD
#define PROGRESS_CREATEIDX_PARTITIONS_DONE
#define PROGRESS_CREATEIDX_PHASE_WAIT_1
#define PROGRESS_CREATEIDX_COMMAND_CREATE_CONCURRENTLY
#define PROGRESS_CREATEIDX_ACCESS_METHOD_OID
#define PROGRESS_WAITFOR_DONE
#define PROGRESS_CREATEIDX_PHASE_WAIT_3
#define PROGRESS_WAITFOR_TOTAL
#define PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY
#define PROGRESS_CREATEIDX_COMMAND_CREATE
#define PROGRESS_WAITFOR_CURRENT_PID
#define PROGRESS_CREATEIDX_PHASE_WAIT_2
#define PROGRESS_CREATEIDX_PHASE
#define PROGRESS_CREATEIDX_PHASE_VALIDATE_IDXSCAN
#define PROGRESS_CREATEIDX_PHASE_WAIT_5
#define PROGRESS_CREATEIDX_INDEX_OID
#define PROGRESS_CREATEIDX_PARTITIONS_TOTAL
#define PROGRESS_CREATEIDX_COMMAND
char * format_operator(Oid operator_oid)
#define RelationGetRelid(relation)
#define RelationGetDescr(relation)
#define RelationGetRelationName(relation)
#define RELATION_IS_OTHER_TEMP(relation)
#define RelationGetNamespace(relation)
List * RelationGetIndexList(Relation relation)
List * RelationGetIndexPredicate(Relation relation)
List * RelationGetIndexExpressions(Relation relation)
void RelationGetExclusionInfo(Relation indexRelation, Oid **operators, Oid **procs, uint16 **strategies)
Datum transformRelOptions(Datum oldOptions, List *defList, const char *namspace, const char *const validnsps[], bool acceptOidsOff, bool isReset)
bytea * index_reloptions(amoptions_function amoptions, Datum reloptions, bool validate)
#define RelFileNumberIsValid(relnumber)
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Snapshot GetTransactionSnapshot(void)
void UnregisterSnapshot(Snapshot snapshot)
void PushActiveSnapshot(Snapshot snapshot)
bool ActiveSnapshotSet(void)
Snapshot RegisterSnapshot(Snapshot snapshot)
void PopActiveSnapshot(void)
void relation_close(Relation relation, LOCKMODE lockmode)
Relation relation_open(Oid relationId, LOCKMODE lockmode)
#define HTEqualStrategyNumber
#define BTEqualStrategyNumber
#define ERRCODE_DUPLICATE_OBJECT
struct ErrorContextCallback * previous
void(* callback)(void *arg)
amoptions_function amoptions
amgettuple_function amgettuple
SortByNulls nulls_ordering
uint16 * ii_ExclusionStrats
AttrNumber ii_IndexAttrNumbers[INDEX_MAX_KEYS]