From 6409648f3c3f9b54b20b193fbeb963281b16c895 Mon Sep 17 00:00:00 2001 From: Oleksandr Shulgin Date: Fri, 21 Aug 2015 11:54:19 +0200 Subject: [PATCH 5/5] Add optional constraint name to field IndexStmt And keep the original index name field intact when renaming. This appears to be the only way to trace back the original index name before it was renamed to match the constraint name. We need this information in the ddl_deparse module. --- src/backend/commands/tablecmds.c | 4 ++-- src/backend/nodes/copyfuncs.c | 1 + src/backend/nodes/equalfuncs.c | 1 + src/backend/nodes/outfuncs.c | 1 + src/backend/parser/parse_utilcmd.c | 16 ++++++++++++++-- src/include/nodes/parsenodes.h | 1 + 6 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 202b5fd..50fffb6 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -5935,7 +5935,7 @@ ATExecAddIndexConstraint(AlteredTableInfo *tab, Relation rel, indexRel = index_open(index_oid, AccessShareLock); - indexName = pstrdup(RelationGetRelationName(indexRel)); + indexName = stmt->idxname; indexInfo = BuildIndexInfo(indexRel); @@ -5950,7 +5950,7 @@ ATExecAddIndexConstraint(AlteredTableInfo *tab, Relation rel, * explicitly gives some other name for the constraint, rename the index * to match. */ - constraintName = stmt->idxname; + constraintName = stmt->conname; if (constraintName == NULL) constraintName = indexName; else if (strcmp(constraintName, indexName) != 0) diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c index 1c8425d..a2db234 100644 --- a/src/backend/nodes/copyfuncs.c +++ b/src/backend/nodes/copyfuncs.c @@ -3092,6 +3092,7 @@ _copyIndexStmt(const IndexStmt *from) IndexStmt *newnode = makeNode(IndexStmt); COPY_STRING_FIELD(idxname); + COPY_STRING_FIELD(conname); COPY_NODE_FIELD(relation); COPY_STRING_FIELD(accessMethod); COPY_STRING_FIELD(tableSpace); diff --git a/src/backend/nodes/equalfuncs.c b/src/backend/nodes/equalfuncs.c index 1d6c43c..04a0486 100644 --- a/src/backend/nodes/equalfuncs.c +++ b/src/backend/nodes/equalfuncs.c @@ -1234,6 +1234,7 @@ static bool _equalIndexStmt(const IndexStmt *a, const IndexStmt *b) { COMPARE_STRING_FIELD(idxname); + COMPARE_STRING_FIELD(conname); COMPARE_NODE_FIELD(relation); COMPARE_STRING_FIELD(accessMethod); COMPARE_STRING_FIELD(tableSpace); diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index a878498..4909031 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -2140,6 +2140,7 @@ _outIndexStmt(StringInfo str, const IndexStmt *node) WRITE_NODE_TYPE("INDEXSTMT"); WRITE_STRING_FIELD(idxname); + WRITE_STRING_FIELD(conname); WRITE_NODE_FIELD(relation); WRITE_STRING_FIELD(accessMethod); WRITE_STRING_FIELD(tableSpace); diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index 16d40c7..c1e5bce 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -1489,6 +1489,10 @@ transformIndexConstraints(CreateStmtContext *cxt) */ if (priorindex->idxname == NULL) priorindex->idxname = index->idxname; + + if (priorindex->conname == NULL) + priorindex->conname = index->conname; + keep = false; break; } @@ -1533,10 +1537,17 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt) index->deferrable = constraint->deferrable; index->initdeferred = constraint->initdeferred; + index->idxname = NULL; /* by default, DefineIndex will choose name */ + if (constraint->conname != NULL) - index->idxname = pstrdup(constraint->conname); + { + index->conname = pstrdup(constraint->conname); + + if (constraint->indexname == NULL) + index->idxname = pstrdup(constraint->conname); + } else - index->idxname = NULL; /* DefineIndex will choose name */ + index->conname = NULL; index->relation = cxt->relation; index->accessMethod = constraint->access_method ? constraint->access_method : DEFAULT_INDEX_TYPE; @@ -1717,6 +1728,7 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt) relation_close(index_rel, NoLock); index->indexOid = index_oid; + index->idxname = pstrdup(index_name); } /* diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index a151412..379f3f6 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -2411,6 +2411,7 @@ typedef struct IndexStmt { NodeTag type; char *idxname; /* name of new index, or NULL for default */ + char *conname; /* corresponding constraint name, or NULL */ RangeVar *relation; /* relation to build index on */ char *accessMethod; /* name of access method (eg. btree) */ char *tableSpace; /* tablespace, or NULL for default */ -- 2.1.4