ALTER_SEQUENCE(7pg13)

πŸ“¦ version 13.23, in opensuse-leap 16.0

πŸ’… ALTER SEQUENCE β€” the ALTER_SEQUENCE glow-up page

Ok so bestie, you know that little number generator in your database? The one that hands out IDs like it's running the door at the club? Yeah β€” this is the manual page for giving it a whole new look. It's ALTER_SEQUENCE, and it changes the definition of a sequence generator. Periodt.

This is page ALTER SEQUENCE(7) from the PostgreSQL 13.23 Documentation, and we're gonna walk through it together, no cap. πŸ¦‹


✨ SYNOPSIS ✨

Like, here's the whole menu of things you can do. Copy it exactly, mwah:

ALTER SEQUENCE [ IF EXISTS ] name
    [ AS data_type ]
    [ INCREMENT [ BY ] increment ]
    [ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]
    [ START [ WITH ] start ]
    [ RESTART [ [ WITH ] restart ] ]
    [ CACHE cache ] [ [ NO ] CYCLE ]
    [ OWNED BY { table_name.column_name | NONE } ]
ALTER SEQUENCE [ IF EXISTS ] name OWNER TO { new_owner | CURRENT_USER | SESSION_USER }
ALTER SEQUENCE [ IF EXISTS ] name RENAME TO new_name
ALTER SEQUENCE [ IF EXISTS ] name SET SCHEMA new_schema

Four separate vibes, four different entry points. It's giving ✨versatility✨.


🌸 DESCRIPTION 🌸

So ALTER SEQUENCE literally just changes the parameters of a sequence generator that already exists. And here's the part that's lowkey iconic: any parameters you don't specifically set in the command retain their prior settings. She keeps her old defaults, she doesn't reset on you. 🫢

Now the boring-but-important part (read it, bestie, it's the vibe check):

You must own the sequence to use ALTER SEQUENCE. To change a sequence's schema, you must also have CREATE privilege on the new schema. To alter the owner, you must also be a direct or indirect member of the new owning role, and that role must have CREATE privilege on the sequence's schema.

And why are these restrictions here? They enforce that altering the owner doesn't do anything you couldn't do by dropping and recreating the sequence. That's the whole reason. However β€” plot twist β€” a superuser can alter ownership of any sequence anyway. Main character energy fr fr. πŸ‘‘

πŸŽ€ PARAMETERS πŸŽ€

Okay this is the big one. Every single option, let's go.

  • name β€” the name of the sequence to be altered. It's optionally schema-qualified, so you can be specific about which one you mean.
  • IF EXISTS β€” pls don't error out if the sequence doesn't exist. If it's missing, a notice is issued instead. Very chill, very forgiving. 🌈
  • data_type β€” the optional clause AS data_type changes the data type of the sequence. Valid types are smallint, integer, and bigint. That's the whole list, bestie, don't try anything else.

And here's the tea on how that works: changing the data type automatically changes the minimum and maximum values of the sequence β€” but 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 NO MINVALUE or NO MAXVALUE, implicitly or explicitly. Otherwise? The minimum and maximum values are preserved, unless new values are given as part of the same command. And if the min/max values don't fit into the new data type β€” πŸ’€ error, sorry. It will be generated.

  • increment β€” the clause INCREMENT BY increment is optional. A positive value makes an ascending sequence, a negative one makes a descending sequence. If you don't specify it, the old increment value is maintained. Simple as that.
  • minvalue, NO MINVALUE β€” the optional clause MINVALUE minvalue determines the minimum value a sequence can generate. If you say NO MINVALUE, the defaults are used: 1 and the minimum value of the data type, for ascending and descending sequences respectively. If neither option is specified, the current minimum value is maintained. 🧚
  • maxvalue, NO MAXVALUE β€” the optional clause MAXVALUE maxvalue determines the maximum value for the sequence. If you say NO MAXVALUE, the defaults are the maximum value of the data type and -1, for ascending and descending sequences respectively. If neither option is specified, the current maximum value is maintained.
  • start β€” the optional clause START WITH start changes the recorded start value of the sequence. And listen β€” this has no effect on the current sequence value. It simply sets the value that future ALTER SEQUENCE RESTART commands will use. She's just bookmarking it for later. ⭐
  • restart β€” the optional clause RESTART [ WITH restart ] changes the current value of the sequence. This is similar to calling the setval function with is_called = false: the specified value will be returned by the next call of nextval. Writing RESTART with no restart value is equivalent to supplying the start value that was recorded by CREATE SEQUENCE or last set by ALTER SEQUENCE START WITH.

And here's the difference that matters, so pay attention: in contrast to a setval call, a RESTART operation on a sequence is transactional and blocks concurrent transactions from obtaining numbers from the same sequence. So if that's not the desired mode of operation, setval should be used. K? K. πŸ’œ

  • cache β€” the clause CACHE cache enables sequence numbers to be preallocated and stored in memory for faster access. The minimum value is 1 β€” which means only one value can be generated at a time, i.e. no cache. If unspecified, the old cache value is maintained.
  • CYCLE β€” the optional CYCLE key word enables the sequence to wrap around when the maxvalue or minvalue has been reached by an ascending or descending sequence respectively. So if the limit is reached, the next number generated will be the minvalue or maxvalue, respectively. Round and round we go, slay. πŸ”₯
  • NO CYCLE β€” if you specify the optional NO CYCLE key word, any calls to nextval after the sequence has reached its maximum value will return an error. If neither CYCLE nor NO CYCLE are specified, the old cycle behavior is maintained.
  • OWNED BY table_name.column_name, OWNED BY NONE β€” the OWNED BY 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 OWNED BY NONE removes any existing association, making the sequence "free-standing". She's independent, she pays her own rent. πŸ¦‹
  • new_owner β€” the user name of the new owner of the sequence.
  • new_name β€” the new name for the sequence. Glow-up name change, iconic.
  • new_schema β€” the new schema for the sequence.

πŸ’€ NOTES πŸ’€

Ok these are the "read before you wreak havoc" bits:

  • ALTER SEQUENCE will not immediately affect nextval 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, though? Affected immediately. πŸ™ˆ
  • ALTER SEQUENCE does not affect the currval status for the sequence. (And fun fact: before PostgreSQL 8.3, it sometimes did. Ancient history, bestie.)
  • ALTER SEQUENCE blocks concurrent nextval, currval, lastval, and setval calls. Yeah. All four. Everyone waits.
  • For historical reasons, ALTER TABLE can be used with sequences too β€” but the only variants of ALTER TABLE that are allowed with sequences are equivalent to the forms shown above. So like… don't get creative. πŸŽ€

⭐ EXAMPLES ⭐

Restarting a sequence called serial, at 105. Literally the whole example, here it is:

ALTER SEQUENCE serial RESTART WITH 105;

Ate. No notes. πŸ‘‘


🌈 COMPATIBILITY 🌈

ALTER SEQUENCE conforms to the SQL standard β€” except for the AS, START WITH, OWNED BY, OWNER TO, RENAME TO, and SET SCHEMA clauses, which are PostgreSQL extensions. So those six are our little extras. Iykyk. πŸ’…


🫢 SEE ALSO 🫢

Go read these next, bestie, they're all connected:


Anyway, that's the whole page πŸ’– β€” this was ALTER SEQUENCE(7) from the PostgreSQL 13.23 Documentation, dated 2025. Go forth and alter responsibly, mwah πŸŽ€βœ¨

πŸ“¦ where this page comes from