1ALTER SEQUENCE(7)        PostgreSQL 15.4 Documentation       ALTER SEQUENCE(7)
2
3
4

NAME

6       ALTER_SEQUENCE - change the definition of a sequence generator
7

SYNOPSIS

9       ALTER SEQUENCE [ IF EXISTS ] name
10           [ AS data_type ]
11           [ INCREMENT [ BY ] increment ]
12           [ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]
13           [ START [ WITH ] start ]
14           [ RESTART [ [ WITH ] restart ] ]
15           [ CACHE cache ] [ [ NO ] CYCLE ]
16           [ OWNED BY { table_name.column_name | NONE } ]
17       ALTER SEQUENCE [ IF EXISTS ] name SET { LOGGED | UNLOGGED }
18       ALTER SEQUENCE [ IF EXISTS ] name OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
19       ALTER SEQUENCE [ IF EXISTS ] name RENAME TO new_name
20       ALTER SEQUENCE [ IF EXISTS ] name SET SCHEMA new_schema
21

DESCRIPTION

23       ALTER SEQUENCE changes the parameters of an existing sequence
24       generator. Any parameters not specifically set in the ALTER SEQUENCE
25       command retain their prior settings.
26
27       You must own the sequence to use ALTER SEQUENCE. To change a sequence's
28       schema, you must also have CREATE privilege on the new schema. To alter
29       the owner, you must also be a direct or indirect member of the new
30       owning role, and that role must have CREATE privilege on the sequence's
31       schema. (These restrictions enforce that altering the owner doesn't do
32       anything you couldn't do by dropping and recreating the sequence.
33       However, a superuser can alter ownership of any sequence anyway.)
34

PARAMETERS

36       name
37           The name (optionally schema-qualified) of a sequence to be altered.
38
39       IF EXISTS
40           Do not throw an error if the sequence does not exist. A notice is
41           issued in this case.
42
43       data_type
44           The optional clause AS data_type changes the data type of the
45           sequence. Valid types are smallint, integer, and bigint.
46
47           Changing the data type automatically changes the minimum and
48           maximum values of the sequence if and only if the previous minimum
49           and maximum values were the minimum or maximum value of the old
50           data type (in other words, if the sequence had been created using
51           NO MINVALUE or NO MAXVALUE, implicitly or explicitly). Otherwise,
52           the minimum and maximum values are preserved, unless new values are
53           given as part of the same command. If the minimum and maximum
54           values do not fit into the new data type, an error will be
55           generated.
56
57       increment
58           The clause INCREMENT BY increment is optional. A positive value
59           will make an ascending sequence, a negative one a descending
60           sequence. If unspecified, the old increment value will be
61           maintained.
62
63       minvalue
64       NO MINVALUE
65           The optional clause MINVALUE minvalue determines the minimum value
66           a sequence can generate. If NO MINVALUE is specified, the defaults
67           of 1 and the minimum value of the data type for ascending and
68           descending sequences, respectively, will be used. If neither option
69           is specified, the current minimum value will be maintained.
70
71       maxvalue
72       NO MAXVALUE
73           The optional clause MAXVALUE maxvalue determines the maximum value
74           for the sequence. If NO MAXVALUE is specified, the defaults of the
75           maximum value of the data type and -1 for ascending and descending
76           sequences, respectively, will be used. If neither option is
77           specified, the current maximum value will be maintained.
78
79       start
80           The optional clause START WITH start changes the recorded start
81           value of the sequence. This has no effect on the current sequence
82           value; it simply sets the value that future ALTER SEQUENCE RESTART
83           commands will use.
84
85       restart
86           The optional clause RESTART [ WITH restart ] changes the current
87           value of the sequence. This is similar to calling the setval
88           function with is_called = false: the specified value will be
89           returned by the next call of nextval. Writing RESTART with no
90           restart value is equivalent to supplying the start value that was
91           recorded by CREATE SEQUENCE or last set by ALTER SEQUENCE START
92           WITH.
93
94           In contrast to a setval call, a RESTART operation on a sequence is
95           transactional and blocks concurrent transactions from obtaining
96           numbers from the same sequence. If that's not the desired mode of
97           operation, setval should be used.
98
99       cache
100           The clause CACHE cache enables sequence numbers to be preallocated
101           and stored in memory for faster access. The minimum value is 1
102           (only one value can be generated at a time, i.e., no cache). If
103           unspecified, the old cache value will be maintained.
104
105       CYCLE
106           The optional CYCLE key word can be used to enable the sequence to
107           wrap around when the maxvalue or minvalue has been reached by an
108           ascending or descending sequence respectively. If the limit is
109           reached, the next number generated will be the minvalue or
110           maxvalue, respectively.
111
112       NO CYCLE
113           If the optional NO CYCLE key word is specified, any calls to
114           nextval after the sequence has reached its maximum value will
115           return an error. If neither CYCLE or NO CYCLE are specified, the
116           old cycle behavior will be maintained.
117
118       SET { LOGGED | UNLOGGED }
119           This form changes the sequence from unlogged to logged or
120           vice-versa (see CREATE SEQUENCE (CREATE_SEQUENCE(7))). It cannot be
121           applied to a temporary sequence.
122
123       OWNED BY table_name.column_name
124       OWNED BY NONE
125           The OWNED BY option causes the sequence to be associated with a
126           specific table column, such that if that column (or its whole
127           table) is dropped, the sequence will be automatically dropped as
128           well. If specified, this association replaces any previously
129           specified association for the sequence. The specified table must
130           have the same owner and be in the same schema as the sequence.
131           Specifying OWNED BY NONE removes any existing association, making
132           the sequence “free-standing”.
133
134       new_owner
135           The user name of the new owner of the sequence.
136
137       new_name
138           The new name for the sequence.
139
140       new_schema
141           The new schema for the sequence.
142

NOTES

144       ALTER SEQUENCE will not immediately affect nextval results in backends,
145       other than the current one, that have preallocated (cached) sequence
146       values. They will use up all cached values prior to noticing the
147       changed sequence generation parameters. The current backend will be
148       affected immediately.
149
150       ALTER SEQUENCE does not affect the currval status for the sequence.
151       (Before PostgreSQL 8.3, it sometimes did.)
152
153       ALTER SEQUENCE blocks concurrent nextval, currval, lastval, and setval
154       calls.
155
156       For historical reasons, ALTER TABLE can be used with sequences too; but
157       the only variants of ALTER TABLE that are allowed with sequences are
158       equivalent to the forms shown above.
159

EXAMPLES

161       Restart a sequence called serial, at 105:
162
163           ALTER SEQUENCE serial RESTART WITH 105;
164

COMPATIBILITY

166       ALTER SEQUENCE conforms to the SQL standard, except for the AS, START
167       WITH, OWNED BY, OWNER TO, RENAME TO, and SET SCHEMA clauses, which are
168       PostgreSQL extensions.
169

SEE ALSO

171       CREATE SEQUENCE (CREATE_SEQUENCE(7)), DROP SEQUENCE (DROP_SEQUENCE(7))
172
173
174
175PostgreSQL 15.4                      2023                    ALTER SEQUENCE(7)
Impressum