Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uNXuk-004D4N-IV for pgsql-admin@arkaria.postgresql.org; Fri, 06 Jun 2025 14:19:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uNXui-00BC3r-FM for pgsql-admin@arkaria.postgresql.org; Fri, 06 Jun 2025 14:19:21 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uNXuh-00BC3j-Se for pgsql-admin@lists.postgresql.org; Fri, 06 Jun 2025 14:19:20 +0000 Received: from mail-oo1-xc2b.google.com ([2607:f8b0:4864:20::c2b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uNXuf-000adW-2L for pgsql-admin@lists.postgresql.org; Fri, 06 Jun 2025 14:19:19 +0000 Received: by mail-oo1-xc2b.google.com with SMTP id 006d021491bc7-6021b5d1c2fso173400eaf.3 for ; Fri, 06 Jun 2025 07:19:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foresightweb-net.20230601.gappssmtp.com; s=20230601; t=1749219557; x=1749824357; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=LO6kRSkTAUyf8U+fO+OATlwdZnOy4G0fJ9muH12wzKo=; b=BUISbSrkn+ZZrHJSQWfVGFxonDvbdv+E5vxOpnXGQNmWF+CBnhodDxRmE81lKwEKtF y6xvZHA3vH74U4G+Tcbx1/zhFkiHjVphzmo7l+OEJ48C+lf9oGuV3bUrRQc2/eOcLN1v Ct4vlAecy1Kmlq+nwcTUtpR2y/6FQT2/s3NZhCC5m4iVzIQFabY6KEEEZdmz2HZZBN/T HxWK4i9Cq5iUThedsWsph9JmEJHvf6YeSuKom2linw8e2ON2YNiDGHik6+gqBeq9akfn tlbNX5dCbiP6xTdPtdCgxIPjdfqbx2RqtHgOfGnW4NjXhsofzP76GEatmqLMxKyuPbYI tyYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749219557; x=1749824357; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=LO6kRSkTAUyf8U+fO+OATlwdZnOy4G0fJ9muH12wzKo=; b=Klwr1N/6b1gXLrc60lJKimNe18C4OSGtgftnOuEyz/AdqJSm5IoFUI7wZfmzWSxoj5 D4+vDqJjWepvpBMoKpiENZM6xspodo1DJqBFENOpboh7pWWuJNctm+6/72iq+os7RZVe wLPmIg8yo3K/ZF3b6GEKoB2Vb6JYQ9tjj+/w3t03PMrh8dYUICs7NtzFP39/pp3IICNI ANseM8fLXVWc3PBpkLpt5WqjS2haYCTQ0li28bWSDMRoDEjAGJ1FlJq3hXS4Of3B5ach S0c/LeLDzOouDvSleQVyuOAcyLW9DemJvwJgzD4UKkhrgL7UO/FxT/XA83cx+T/xx64M zfnw== X-Gm-Message-State: AOJu0YzWsYUJyyxF50H1Oz0v/Z3XwZ3hbRc63wmlbJ3Oobm+4860MwCq FTgI1j4tr+Qk27tWVRJiV2XL9jrNl32VXFI18x455wZEUuRpMs5EQrht/qY3qsjy5fByyVhaU9J teyGRGHzpatMm8kHFo2aGvT+HcSehZtiPpfPdzg9R1A== X-Gm-Gg: ASbGnctxmMmZgAJN+2n5SJEsQ5g0gjnY6jdM6pYpRLXhEz2k/TVW4tRubldKWGtf40i brh+tDsdIBz8O/zNBgXMoyKNDgvARsZ2AvdWZFUwEi0+PoxHYmSZLGmfKp9bMOYlA2lL64kG5T2 GuGfQukYPVgAURP9j7E113lbZ9xp4SihHCm2uQBTn+Mttc X-Google-Smtp-Source: AGHT+IEUIG7SA6+p7DrR2fXahHoOR3mITvi1Oes1thpiiZqL7kDJzbf+pcxjax0pr3NrbVc+VrJpogWYVTwCHft8txA= X-Received: by 2002:a05:6870:a79a:b0:2ea:ad1:c771 with SMTP id 586e51a60fabf-2ea0ad1d2d7mr361246fac.6.1749219556679; Fri, 06 Jun 2025 07:19:16 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Keith Date: Fri, 6 Jun 2025 10:18:40 -0400 X-Gm-Features: AX0GCFszwupnNExSP1IcoQDUG-hzLVv42hfeHU7Mm-Ut98XU2ydQQ5Mb_m-sbeY Message-ID: Subject: Re: order of pg_dump command "create sequence" To: =?UTF-8?B?0JDQvdGC0L7QvSDQk9C70YPRiNCw0LrQvtCy?= Cc: pgsql-admin@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000004f7aa40636e7eab4" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000004f7aa40636e7eab4 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Jun 6, 2025 at 5:16=E2=80=AFAM =D0=90=D0=BD=D1=82=D0=BE=D0=BD =D0= =93=D0=BB=D1=83=D1=88=D0=B0=D0=BA=D0=BE=D0=B2 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 Postgre= s > does not prohibit it) in which restoring from a dump becomes impossible d= ue > 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 t= he > 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. --0000000000004f7aa40636e7eab4 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Fri, Jun 6, = 2025 at 5:16=E2=80=AFAM =D0=90=D0=BD=D1=82=D0=BE=D0=BD =D0=93=D0=BB=D1=83= =D1=88=D0=B0=D0=BA=D0=BE=D0=B2 <a.glushakov86@gmail.com> wrote:
Hi, I found a peculiarity of pg_= dump's work with sequences when they are not explicitly linked to a tab= le.
I encountered a situation (clearly abnormal use of sequences, but Po= stgres does not prohibit it) in which restoring from a dump becomes impossi= ble due to the violation of the order of commands.

Example:

<= div>/* create simple sequence */
CREATE SEQUENCE public.m= y_seq;
=

/* create a function that will move the sequenc= e */
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 pub= lic.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 sequ= ence in dump of actions will be as follows:

1) Creating a function &= quot;CREATE FUNCTION public.gen_id()"
2) Creating a table "CRE= ATE TABLE public.exp_table"
3) Creating a sequence "CREATE SEQ= UENCE 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: S= ELECT 'PREFIX_'||nextval('public.my_seq'::regclass)::VARCHA= R;
^
QUERY:
SELECT 'PREFIX_'||nextval('public.my_seq&#= 39;::regclass)::VARCHAR;
CONTEXT: SQL function "gen_id" during startup"


It turns = out that Postgres does not know that the sequence is associated with the ta= ble and places the code for creating it after the code for creating the tab= le.
A workaround for this particular case is to change the name of the s= equence so that it appears higher in the dump (according to alphabetical or= der, for example rename it to "a_my_seq") and then pg_dump will p= lace 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 ab= out, I can't say, but such problems can cause, for example, an error du= ring an upgrade or logical replication, when you need to dump and restore t= he data schema.

If you need a sequenc= e to be associated with a table, make it an IDENTITY column

<= /div>
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
The IDENTITY method is far preferred, though, since it a= llows use of the GENERATED features and makes all the permissions of the ta= ble automatically associated with the sequence as well. Otherwise you have = to manage them independently.
--0000000000004f7aa40636e7eab4--