public inbox for [email protected]
help / color / mirror / Atom feedFrom: Keith <[email protected]>
To: Антон Глушаков <[email protected]>
Cc: [email protected]
Subject: Re: order of pg_dump command "create sequence"
Date: Fri, 6 Jun 2025 10:18:40 -0400
Message-ID: <CAHw75vsXZcvFpYhoVy=p-yhhf2u2q7yN+GP6j0guifz7sCbKZw@mail.gmail.com> (raw)
In-Reply-To: <CAHnOmadVj0y982EqT9sEvszjcKikysWtSrX88OvknP0W0poDkQ@mail.gmail.com>
References: <CAHnOmadVj0y982EqT9sEvszjcKikysWtSrX88OvknP0W0poDkQ@mail.gmail.com>
On Fri, Jun 6, 2025 at 5:16 AM Антон Глушаков <[email protected]>
wrote:
> Hi, I found a peculiarity of pg_dump's work with sequences when they are
> not explicitly linked to a table.
> I encountered a situation (clearly abnormal use of sequences, but Postgres
> does not prohibit it) in which restoring from a dump becomes impossible due
> to the violation of the order of commands.
>
> Example:
>
> /* create simple sequence */
> CREATE SEQUENCE public.my_seq;
>
> /* create a function that will move the sequence */
> CREATE FUNCTION public.gen_id() RETURNS character varying
> LANGUAGE sql IMMUTABLE AS
> $$
> SELECT 'PREFIX_'||nextval('public.my_seq'::regclass)::VARCHAR;
> $$;
>
> /* table, the column of which will call the function */
> CREATE TABLE public.exp_table (id character varying(13) GENERATED ALWAYS
> AS (public.gen_id()) STORED NOT NULL);
>
> If you make a pg_dump of the created , the sequence in dump of actions
> will be as follows:
>
> 1) Creating a function "CREATE FUNCTION public.gen_id()"
> 2) Creating a table "CREATE TABLE public.exp_table"
> 3) Creating a sequence "CREATE SEQUENCE public.my_seq"
>
> And here the problems begin.
> If we try to restore the table structure from the dump, we get the
> expected error
> "ERROR: relation "public.my_seq" does not exist
> LINE 2: SELECT 'PREFIX_'||nextval('public.my_seq'::regclass)::VARCHAR;
> ^
> QUERY:
> SELECT 'PREFIX_'||nextval('public.my_seq'::regclass)::VARCHAR;
> CONTEXT: SQL function "gen_id" during startup"
>
>
> It turns out that Postgres does not know that the sequence is associated
> with the table and places the code for creating it after the code for
> creating the table.
> A workaround for this particular case is to change the name of the
> sequence so that it appears higher in the dump (according to alphabetical
> order, for example rename it to "a_my_seq") and then pg_dump will place the
> creation of the sequence before the table, and the restore will be
> successful.
> Whether this is a bug or a feature that you need to know about, I can't
> say, but such problems can cause, for example, an error during an upgrade
> or logical replication, when you need to dump and restore the data schema.
>
If you need a sequence to be associated with a table, make it an IDENTITY
column
https://www.postgresql.org/docs/17/ddl-identity-columns.html
Or make sure that the sequence is owned by the table column that it is
associated with. See the OWNED BY clause
https://www.postgresql.org/docs/17/sql-createsequence.html
The IDENTITY method is far preferred, though, since it allows use of the
GENERATED features and makes all the permissions of the table automatically
associated with the sequence as well. Otherwise you have to manage them
independently.
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected], [email protected]
Subject: Re: order of pg_dump command "create sequence"
In-Reply-To: <CAHw75vsXZcvFpYhoVy=p-yhhf2u2q7yN+GP6j0guifz7sCbKZw@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox