2 .\" Title: ALTER SEQUENCE
3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "ALTER SEQUENCE" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 ALTER_SEQUENCE \- change the definition of a sequence generator
35 ALTER SEQUENCE [ IF EXISTS ] \fIname\fR
36 [ AS \fIdata_type\fR ]
37 [ INCREMENT [ BY ] \fIincrement\fR ]
38 [ MINVALUE \fIminvalue\fR | NO MINVALUE ] [ MAXVALUE \fImaxvalue\fR | NO MAXVALUE ]
40 [ START [ WITH ] \fIstart\fR ]
41 [ RESTART [ [ WITH ] \fIrestart\fR ] ]
43 [ OWNED BY { \fItable_name\fR\&.\fIcolumn_name\fR | NONE } ]
44 ALTER SEQUENCE [ IF EXISTS ] \fIname\fR SET { LOGGED | UNLOGGED }
45 ALTER SEQUENCE [ IF EXISTS ] \fIname\fR OWNER TO { \fInew_owner\fR | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
46 ALTER SEQUENCE [ IF EXISTS ] \fIname\fR RENAME TO \fInew_name\fR
47 ALTER SEQUENCE [ IF EXISTS ] \fIname\fR SET SCHEMA \fInew_schema\fR
52 changes the parameters of an existing sequence generator\&. Any parameters not specifically set in the
54 command retain their prior settings\&.
56 You must own the sequence to use
57 \fBALTER SEQUENCE\fR\&. To change a sequence\*(Aqs schema, you must also have
59 privilege on the new schema\&. To alter the owner, you must be able to
61 to the new owning role, and that role must have
63 privilege on the sequence\*(Aqs schema\&. (These restrictions enforce that altering the owner doesn\*(Aqt do anything you couldn\*(Aqt do by dropping and recreating the sequence\&. However, a superuser can alter ownership of any sequence anyway\&.)
69 The name (optionally schema\-qualified) of a sequence to be altered\&.
74 Do not throw an error if the sequence does not exist\&. A notice is issued in this case\&.
81 changes the data type of the sequence\&. Valid types are
86 Changing the data type automatically changes the minimum and maximum values of the sequence if and only if the previous minimum and maximum values were the minimum or maximum value of the old data type (in other words, if the sequence had been created using
89 NO MAXVALUE, implicitly or explicitly)\&. Otherwise, the minimum and maximum values are preserved, unless new values are given as part of the same command\&. If the minimum and maximum values do not fit into the new data type, an error will be generated\&.
95 INCREMENT BY \fIincrement\fR
96 is optional\&. A positive value will make an ascending sequence, a negative one a descending sequence\&. If unspecified, the old increment value will be maintained\&.
104 MINVALUE \fIminvalue\fR
105 determines the minimum value a sequence can generate\&. If
107 is specified, the defaults of 1 and the minimum value of the data type for ascending and descending sequences, respectively, will be used\&. If neither option is specified, the current minimum value will be maintained\&.
115 MAXVALUE \fImaxvalue\fR
116 determines the maximum value for the sequence\&. If
118 is specified, the defaults of the maximum value of the data type and \-1 for ascending and descending sequences, respectively, will be used\&. If neither option is specified, the current maximum value will be maintained\&.
125 key word can be used to enable the sequence to wrap around when the
129 has been reached by an ascending or descending sequence respectively\&. If the limit is reached, the next number generated will be the
132 \fImaxvalue\fR, respectively\&.
139 key word is specified, any calls to
141 after the sequence has reached its maximum value will return an error\&. If neither
145 are specified, the old cycle behavior will be maintained\&.
151 START WITH \fIstart\fR
152 changes the recorded start value of the sequence\&. This has no effect on the
154 sequence value; it simply sets the value that future
155 \fBALTER SEQUENCE RESTART\fR
162 RESTART [ WITH \fIrestart\fR ]
163 changes the current value of the sequence\&. This is similar to calling the
168 false: the specified value will be returned by the
171 \fBnextval\fR\&. Writing
175 value is equivalent to supplying the start value that was recorded by
176 \fBCREATE SEQUENCE\fR
178 \fBALTER SEQUENCE START WITH\fR\&.
184 operation on a sequence is transactional and blocks concurrent transactions from obtaining numbers from the same sequence\&. If that\*(Aqs not the desired mode of operation,
193 enables sequence numbers to be preallocated and stored in memory for faster access\&. The minimum value is 1 (only one value can be generated at a time, i\&.e\&., no cache)\&. If unspecified, the old cache value will be maintained\&.
196 SET { LOGGED | UNLOGGED }
198 This form changes the sequence from unlogged to logged or vice\-versa (see
199 CREATE SEQUENCE (\fBCREATE_SEQUENCE\fR(7)))\&. It cannot be applied to a temporary sequence\&.
202 OWNED BY \fItable_name\fR\&.\fIcolumn_name\fR
208 option causes the sequence to be associated with a specific table column, such that if that column (or its whole table) is dropped, the sequence will be automatically dropped as well\&. If specified, this association replaces any previously specified association for the sequence\&. The specified table must have the same owner and be in the same schema as the sequence\&. Specifying
210 removes any existing association, making the sequence
211 \(lqfree\-standing\(rq\&.
216 The user name of the new owner of the sequence\&.
221 The new name for the sequence\&.
226 The new schema for the sequence\&.
231 will not immediately affect
233 results in backends, other than the current one, that have preallocated (cached) sequence values\&. They will use up all cached values prior to noticing the changed sequence generation parameters\&. The current backend will be affected immediately\&.
238 status for the sequence\&. (Before
240 8\&.3, it sometimes did\&.)
250 For historical reasons,
252 can be used with sequences too; but the only variants of
254 that are allowed with sequences are equivalent to the forms shown above\&.
257 Restart a sequence called
264 ALTER SEQUENCE serial RESTART WITH 105;
274 standard, except for the
285 CREATE SEQUENCE (\fBCREATE_SEQUENCE\fR(7)), DROP SEQUENCE (\fBDROP_SEQUENCE\fR(7))