* transformIndexConstraints * Handle UNIQUE, PRIMARY KEY, EXCLUDE constraints, which create indexes. * We also merge in any index definitions arising from * LIKE ... INCLUDING INDEXES. */
| 3173 | * LIKE ... INCLUDING INDEXES. |
| 3174 | */ |
| 3175 | static void |
| 3176 | transformIndexConstraints(CreateStmtContext *cxt) |
| 3177 | { |
| 3178 | IndexStmt *index; |
| 3179 | List *indexlist = NIL; |
| 3180 | List *finalindexlist = NIL; |
| 3181 | ListCell *lc; |
| 3182 | |
| 3183 | /* |
| 3184 | * Run through the constraints that need to generate an index. For PRIMARY |
| 3185 | * KEY, mark each column as NOT NULL and create an index. For UNIQUE or |
| 3186 | * EXCLUDE, create an index as for PRIMARY KEY, but do not insist on NOT |
| 3187 | * NULL. |
| 3188 | */ |
| 3189 | foreach(lc, cxt->ixconstraints) |
| 3190 | { |
| 3191 | Constraint *constraint = lfirst_node(Constraint, lc); |
| 3192 | |
| 3193 | Assert(constraint->contype == CONSTR_PRIMARY || |
| 3194 | constraint->contype == CONSTR_UNIQUE || |
| 3195 | constraint->contype == CONSTR_EXCLUSION); |
| 3196 | |
| 3197 | index = transformIndexConstraint(constraint, cxt); |
| 3198 | |
| 3199 | indexlist = lappend(indexlist, index); |
| 3200 | } |
| 3201 | |
| 3202 | /* |
| 3203 | * Scan the index list and remove any redundant index specifications. This |
| 3204 | * can happen if, for instance, the user writes UNIQUE PRIMARY KEY. A |
| 3205 | * strict reading of SQL would suggest raising an error instead, but that |
| 3206 | * strikes me as too anal-retentive. - tgl 2001-02-14 |
| 3207 | * |
| 3208 | * XXX in ALTER TABLE case, it'd be nice to look for duplicate |
| 3209 | * pre-existing indexes, too. |
| 3210 | */ |
| 3211 | if (cxt->pkey != NULL) |
| 3212 | { |
| 3213 | /* Make sure we keep the PKEY index in preference to others... */ |
| 3214 | finalindexlist = list_make1(cxt->pkey); |
| 3215 | } |
| 3216 | |
| 3217 | foreach(lc, indexlist) |
| 3218 | { |
| 3219 | bool keep = true; |
| 3220 | bool defer = false; |
| 3221 | ListCell *k; |
| 3222 | |
| 3223 | index = lfirst(lc); |
| 3224 | |
| 3225 | /* if it's pkey, it's already in finalindexlist */ |
| 3226 | if (index == cxt->pkey) |
| 3227 | continue; |
| 3228 | |
| 3229 | foreach(k, finalindexlist) |
| 3230 | { |
| 3231 | IndexStmt *priorindex = lfirst(k); |
| 3232 |
no test coverage detected