Skip to content

Commit fc2d1ac

Browse files
committed
Repair pg_upgrade for identity sequences with non-default persistence.
Since we introduced unlogged sequences in v15, identity sequences have defaulted to having the same persistence as their owning table. However, it is possible to change that with ALTER SEQUENCE, and pg_dump tries to preserve the logged-ness of sequences when it doesn't match (as indeed it wouldn't for an unlogged table from before v15). The fly in the ointment is that ALTER SEQUENCE SET [UN]LOGGED fails in binary-upgrade mode, because it needs to assign a new relfilenode which we cannot permit in that mode. Thus, trying to pg_upgrade a database containing a mismatching identity sequence failed. To fix, add syntax to ADD/ALTER COLUMN GENERATED AS IDENTITY to allow the sequence's persistence to be set correctly at creation, and use that instead of ALTER SEQUENCE SET [UN]LOGGED in pg_dump. (I tried to make SET [UN]LOGGED work without any pg_dump modifications, but that seems too fragile to be a desirable answer. This way should be markedly faster anyhow.) In passing, document the previously-undocumented SEQUENCE NAME option that pg_dump also relies on for identity sequences; I see no value in trying to pretend it doesn't exist. Per bug #18618 from Anthony Hsu. Back-patch to v15 where we invented this stuff. Discussion: https://postgr.es/m/18618-d4eb26d669ed110a@postgresql.org
1 parent 6f4e1fc commit fc2d1ac

File tree

7 files changed

+112
-41
lines changed

7 files changed

+112
-41
lines changed

doc/src/sgml/ref/create_table.sgml

+12-5
Original file line numberDiff line numberDiff line change
@@ -895,8 +895,8 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
895895
<para>
896896
This clause creates the column as an <firstterm>identity
897897
column</firstterm>. It will have an implicit sequence attached to it
898-
and the column in new rows will automatically have values from the
899-
sequence assigned to it.
898+
and in newly-inserted rows the column will automatically have values
899+
from the sequence assigned to it.
900900
Such a column is implicitly <literal>NOT NULL</literal>.
901901
</para>
902902

@@ -926,9 +926,16 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
926926
</para>
927927

928928
<para>
929-
The optional <replaceable>sequence_options</replaceable> clause can be
930-
used to override the options of the sequence.
931-
See <xref linkend="sql-createsequence"/> for details.
929+
The optional <replaceable>sequence_options</replaceable> clause can
930+
be used to override the parameters of the sequence. The available
931+
options include those shown for <xref linkend="sql-createsequence"/>,
932+
plus <literal>SEQUENCE NAME <replaceable>name</replaceable></literal>,
933+
<literal>LOGGED</literal>, and <literal>UNLOGGED</literal>, which
934+
allow selection of the name and persistence level of the
935+
sequence. Without <literal>SEQUENCE NAME</literal>, the system
936+
chooses an unused name for the sequence.
937+
Without <literal>LOGGED</literal> or <literal>UNLOGGED</literal>,
938+
the sequence will have the same persistence level as the table.
932939
</para>
933940
</listitem>
934941
</varlistentry>

src/backend/commands/sequence.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,10 @@ init_params(ParseState *pstate, List *options, bool for_identity,
13651365
/*
13661366
* The parser allows this, but it is only for identity columns, in
13671367
* which case it is filtered out in parse_utilcmd.c. We only get
1368-
* here if someone puts it into a CREATE SEQUENCE.
1368+
* here if someone puts it into a CREATE SEQUENCE, where it'd be
1369+
* redundant. (The same is true for the equally-nonstandard
1370+
* LOGGED and UNLOGGED options, but for those, the default error
1371+
* below seems sufficient.)
13691372
*/
13701373
ereport(ERROR,
13711374
(errcode(ERRCODE_SYNTAX_ERROR),

src/backend/parser/gram.y

+8-1
Original file line numberDiff line numberDiff line change
@@ -4738,6 +4738,10 @@ SeqOptElem: AS SimpleTypename
47384738
{
47394739
$$ = makeDefElem("increment", (Node *) $3, @1);
47404740
}
4741+
| LOGGED
4742+
{
4743+
$$ = makeDefElem("logged", NULL, @1);
4744+
}
47414745
| MAXVALUE NumericOnly
47424746
{
47434747
$$ = makeDefElem("maxvalue", (Node *) $2, @1);
@@ -4760,7 +4764,6 @@ SeqOptElem: AS SimpleTypename
47604764
}
47614765
| SEQUENCE NAME_P any_name
47624766
{
4763-
/* not documented, only used by pg_dump */
47644767
$$ = makeDefElem("sequence_name", (Node *) $3, @1);
47654768
}
47664769
| START opt_with NumericOnly
@@ -4775,6 +4778,10 @@ SeqOptElem: AS SimpleTypename
47754778
{
47764779
$$ = makeDefElem("restart", (Node *) $3, @1);
47774780
}
4781+
| UNLOGGED
4782+
{
4783+
$$ = makeDefElem("unlogged", NULL, @1);
4784+
}
47784785
;
47794786

47804787
opt_by: BY

src/backend/parser/parse_utilcmd.c

+54-26
Original file line numberDiff line numberDiff line change
@@ -365,30 +365,22 @@ generateSerialExtraStmts(CreateStmtContext *cxt, ColumnDef *column,
365365
{
366366
ListCell *option;
367367
DefElem *nameEl = NULL;
368+
DefElem *loggedEl = NULL;
368369
Oid snamespaceid;
369370
char *snamespace;
370371
char *sname;
372+
char seqpersistence;
371373
CreateSeqStmt *seqstmt;
372374
AlterSeqStmt *altseqstmt;
373375
List *attnamelist;
374-
int nameEl_idx = -1;
375376

376377
/* Make a copy of this as we may end up modifying it in the code below */
377378
seqoptions = list_copy(seqoptions);
378379

379380
/*
380-
* Determine namespace and name to use for the sequence.
381-
*
382-
* First, check if a sequence name was passed in as an option. This is
383-
* used by pg_dump. Else, generate a name.
384-
*
385-
* Although we use ChooseRelationName, it's not guaranteed that the
386-
* selected sequence name won't conflict; given sufficiently long field
387-
* names, two different serial columns in the same table could be assigned
388-
* the same sequence name, and we'd not notice since we aren't creating
389-
* the sequence quite yet. In practice this seems quite unlikely to be a
390-
* problem, especially since few people would need two serial columns in
391-
* one table.
381+
* Check for non-SQL-standard options (not supported within CREATE
382+
* SEQUENCE, because they'd be redundant), and remove them from the
383+
* seqoptions list if found.
392384
*/
393385
foreach(option, seqoptions)
394386
{
@@ -399,12 +391,24 @@ generateSerialExtraStmts(CreateStmtContext *cxt, ColumnDef *column,
399391
if (nameEl)
400392
errorConflictingDefElem(defel, cxt->pstate);
401393
nameEl = defel;
402-
nameEl_idx = foreach_current_index(option);
394+
seqoptions = foreach_delete_current(seqoptions, option);
395+
}
396+
else if (strcmp(defel->defname, "logged") == 0 ||
397+
strcmp(defel->defname, "unlogged") == 0)
398+
{
399+
if (loggedEl)
400+
errorConflictingDefElem(defel, cxt->pstate);
401+
loggedEl = defel;
402+
seqoptions = foreach_delete_current(seqoptions, option);
403403
}
404404
}
405405

406+
/*
407+
* Determine namespace and name to use for the sequence.
408+
*/
406409
if (nameEl)
407410
{
411+
/* Use specified name */
408412
RangeVar *rv = makeRangeVarFromNameList(castNode(List, nameEl->arg));
409413

410414
snamespace = rv->schemaname;
@@ -418,11 +422,20 @@ generateSerialExtraStmts(CreateStmtContext *cxt, ColumnDef *column,
418422
snamespace = get_namespace_name(snamespaceid);
419423
}
420424
sname = rv->relname;
421-
/* Remove the SEQUENCE NAME item from seqoptions */
422-
seqoptions = list_delete_nth_cell(seqoptions, nameEl_idx);
423425
}
424426
else
425427
{
428+
/*
429+
* Generate a name.
430+
*
431+
* Although we use ChooseRelationName, it's not guaranteed that the
432+
* selected sequence name won't conflict; given sufficiently long
433+
* field names, two different serial columns in the same table could
434+
* be assigned the same sequence name, and we'd not notice since we
435+
* aren't creating the sequence quite yet. In practice this seems
436+
* quite unlikely to be a problem, especially since few people would
437+
* need two serial columns in one table.
438+
*/
426439
if (cxt->rel)
427440
snamespaceid = RelationGetNamespace(cxt->rel);
428441
else
@@ -443,23 +456,38 @@ generateSerialExtraStmts(CreateStmtContext *cxt, ColumnDef *column,
443456
cxt->stmtType, sname,
444457
cxt->relation->relname, column->colname)));
445458

459+
/*
460+
* Determine the persistence of the sequence. By default we copy the
461+
* persistence of the table, but if LOGGED or UNLOGGED was specified, use
462+
* that (as long as the table isn't TEMP).
463+
*
464+
* For CREATE TABLE, we get the persistence from cxt->relation, which
465+
* comes from the CreateStmt in progress. For ALTER TABLE, the parser
466+
* won't set cxt->relation->relpersistence, but we have cxt->rel as the
467+
* existing table, so we copy the persistence from there.
468+
*/
469+
seqpersistence = cxt->rel ? cxt->rel->rd_rel->relpersistence : cxt->relation->relpersistence;
470+
if (loggedEl)
471+
{
472+
if (seqpersistence == RELPERSISTENCE_TEMP)
473+
ereport(ERROR,
474+
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
475+
errmsg("cannot set logged status of a temporary sequence"),
476+
parser_errposition(cxt->pstate, loggedEl->location)));
477+
else if (strcmp(loggedEl->defname, "logged") == 0)
478+
seqpersistence = RELPERSISTENCE_PERMANENT;
479+
else
480+
seqpersistence = RELPERSISTENCE_UNLOGGED;
481+
}
482+
446483
/*
447484
* Build a CREATE SEQUENCE command to create the sequence object, and add
448485
* it to the list of things to be done before this CREATE/ALTER TABLE.
449486
*/
450487
seqstmt = makeNode(CreateSeqStmt);
451488
seqstmt->for_identity = for_identity;
452489
seqstmt->sequence = makeRangeVar(snamespace, sname, -1);
453-
454-
/*
455-
* Copy the persistence of the table. For CREATE TABLE, we get the
456-
* persistence from cxt->relation, which comes from the CreateStmt in
457-
* progress. For ALTER TABLE, the parser won't set
458-
* cxt->relation->relpersistence, but we have cxt->rel as the existing
459-
* table, so we copy the persistence from there.
460-
*/
461-
seqstmt->sequence->relpersistence = cxt->rel ? cxt->rel->rd_rel->relpersistence : cxt->relation->relpersistence;
462-
490+
seqstmt->sequence->relpersistence = seqpersistence;
463491
seqstmt->options = seqoptions;
464492

465493
/*

src/bin/pg_dump/pg_dump.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -16980,6 +16980,15 @@ dumpSequence(Archive *fout, const TableInfo *tbinfo)
1698016980
appendPQExpBufferStr(query, "BY DEFAULT");
1698116981
appendPQExpBuffer(query, " AS IDENTITY (\n SEQUENCE NAME %s\n",
1698216982
fmtQualifiedDumpable(tbinfo));
16983+
16984+
/*
16985+
* Emit persistence option only if it's different from the owning
16986+
* table's. This avoids using this new syntax unnecessarily.
16987+
*/
16988+
if (tbinfo->relpersistence != owning_tab->relpersistence)
16989+
appendPQExpBuffer(query, " %s\n",
16990+
tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ?
16991+
"UNLOGGED" : "LOGGED");
1698316992
}
1698416993
else
1698516994
{
@@ -17012,15 +17021,7 @@ dumpSequence(Archive *fout, const TableInfo *tbinfo)
1701217021
cache, (cycled ? "\n CYCLE" : ""));
1701317022

1701417023
if (tbinfo->is_identity_sequence)
17015-
{
1701617024
appendPQExpBufferStr(query, "\n);\n");
17017-
if (tbinfo->relpersistence != owning_tab->relpersistence)
17018-
appendPQExpBuffer(query,
17019-
"ALTER SEQUENCE %s SET %s;\n",
17020-
fmtQualifiedDumpable(tbinfo),
17021-
tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ?
17022-
"UNLOGGED" : "LOGGED");
17023-
}
1702417025
else
1702517026
appendPQExpBufferStr(query, ";\n");
1702617027

src/test/regress/expected/identity.out

+16
Original file line numberDiff line numberDiff line change
@@ -686,3 +686,19 @@ SELECT * FROM itest16;
686686

687687
DROP TABLE itest15;
688688
DROP TABLE itest16;
689+
-- For testing of pg_dump and pg_upgrade, leave behind some identity
690+
-- sequences whose logged-ness doesn't match their owning table's.
691+
CREATE TABLE identity_dump_logged (a INT GENERATED ALWAYS AS IDENTITY);
692+
ALTER SEQUENCE identity_dump_logged_a_seq SET UNLOGGED;
693+
CREATE UNLOGGED TABLE identity_dump_unlogged (a INT GENERATED ALWAYS AS IDENTITY);
694+
ALTER SEQUENCE identity_dump_unlogged_a_seq SET LOGGED;
695+
SELECT relname, relpersistence FROM pg_class
696+
WHERE relname ~ '^identity_dump_' ORDER BY 1;
697+
relname | relpersistence
698+
------------------------------+----------------
699+
identity_dump_logged | p
700+
identity_dump_logged_a_seq | u
701+
identity_dump_unlogged | u
702+
identity_dump_unlogged_a_seq | p
703+
(4 rows)
704+

src/test/regress/sql/identity.sql

+9
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,12 @@ SELECT * FROM itest15;
419419
SELECT * FROM itest16;
420420
DROP TABLE itest15;
421421
DROP TABLE itest16;
422+
423+
-- For testing of pg_dump and pg_upgrade, leave behind some identity
424+
-- sequences whose logged-ness doesn't match their owning table's.
425+
CREATE TABLE identity_dump_logged (a INT GENERATED ALWAYS AS IDENTITY);
426+
ALTER SEQUENCE identity_dump_logged_a_seq SET UNLOGGED;
427+
CREATE UNLOGGED TABLE identity_dump_unlogged (a INT GENERATED ALWAYS AS IDENTITY);
428+
ALTER SEQUENCE identity_dump_unlogged_a_seq SET LOGGED;
429+
SELECT relname, relpersistence FROM pg_class
430+
WHERE relname ~ '^identity_dump_' ORDER BY 1;

0 commit comments

Comments
 (0)