* generateSerialExtraStmts * Generate CREATE SEQUENCE and ALTER SEQUENCE ... OWNED BY statements * to create the sequence for a serial or identity column. * * This includes determining the name the sequence will have. The caller * can ask to get back the name components by passing non-null pointers * for snamespace_p and sname_p. */
| 458 | * for snamespace_p and sname_p. |
| 459 | */ |
| 460 | static void |
| 461 | generateSerialExtraStmts(CreateStmtContext *cxt, ColumnDef *column, |
| 462 | Oid seqtypid, List *seqoptions, |
| 463 | bool for_identity, bool col_exists, |
| 464 | char **snamespace_p, char **sname_p) |
| 465 | { |
| 466 | ListCell *option; |
| 467 | DefElem *nameEl = NULL; |
| 468 | Oid snamespaceid; |
| 469 | char *snamespace; |
| 470 | char *sname; |
| 471 | CreateSeqStmt *seqstmt; |
| 472 | AlterSeqStmt *altseqstmt; |
| 473 | List *attnamelist; |
| 474 | |
| 475 | bool has_cache_option = false; |
| 476 | |
| 477 | int nameEl_idx = -1; |
| 478 | |
| 479 | /* |
| 480 | * Determine namespace and name to use for the sequence. |
| 481 | * |
| 482 | * First, check if a sequence name was passed in as an option. This is |
| 483 | * used by pg_dump. Else, generate a name. |
| 484 | * |
| 485 | * Although we use ChooseRelationName, it's not guaranteed that the |
| 486 | * selected sequence name won't conflict; given sufficiently long field |
| 487 | * names, two different serial columns in the same table could be assigned |
| 488 | * the same sequence name, and we'd not notice since we aren't creating |
| 489 | * the sequence quite yet. In practice this seems quite unlikely to be a |
| 490 | * problem, especially since few people would need two serial columns in |
| 491 | * one table. |
| 492 | */ |
| 493 | foreach(option, seqoptions) |
| 494 | { |
| 495 | DefElem *defel = lfirst_node(DefElem, option); |
| 496 | |
| 497 | if (strcmp(defel->defname, "sequence_name") == 0) |
| 498 | { |
| 499 | if (nameEl) |
| 500 | ereport(ERROR, |
| 501 | (errcode(ERRCODE_SYNTAX_ERROR), |
| 502 | errmsg("conflicting or redundant options"))); |
| 503 | nameEl = defel; |
| 504 | nameEl_idx = foreach_current_index(option); |
| 505 | } |
| 506 | |
| 507 | if (strcmp(defel->defname, "cache") == 0) |
| 508 | has_cache_option = true; |
| 509 | } |
| 510 | |
| 511 | if (nameEl) |
| 512 | { |
| 513 | RangeVar *rv = makeRangeVarFromNameList(castNode(List, nameEl->arg)); |
| 514 | |
| 515 | snamespace = rv->schemaname; |
| 516 | if (!snamespace) |
| 517 | { |
no test coverage detected