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 1uNTBO-002h60-V4 for pgsql-admin@arkaria.postgresql.org; Fri, 06 Jun 2025 09:16:15 +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 1uNTBK-006crS-Rf for pgsql-admin@arkaria.postgresql.org; Fri, 06 Jun 2025 09:16:11 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uNTBK-006coZ-BL for pgsql-admin@lists.postgresql.org; Fri, 06 Jun 2025 09:16:11 +0000 Received: from mail-yw1-x112b.google.com ([2607:f8b0:4864:20::112b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uNTBI-000WJS-0X for pgsql-admin@lists.postgresql.org; Fri, 06 Jun 2025 09:16:10 +0000 Received: by mail-yw1-x112b.google.com with SMTP id 00721157ae682-70e4043c5b7so17102067b3.1 for ; Fri, 06 Jun 2025 02:16:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749201365; x=1749806165; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=LArt7Dh32wjw+3zD6UboJLzqZe99Yzpi0ogBUzjWvRc=; b=CCcFBfR+R05TDJ2H69+veZhzvzKMUAvMV9mKL22SPKdyj1vy4A6hb6joqqOCpqyEIP zmYLGcZRJYoY9oBd4AWm3VdFkS4vgdp2lqPbnqMHOrszPDSemEJXueT+lusN/19GCGDR I5qOCUxwfJQPvxJOZEw/DNtHp/DdrNRlejtcV3Z5AgU+FO+rUW6hBOWB5rgLBF902c7Q N2TP45OysarIo+rRecDBsZRO+xmHEKlPwgZx0YPoV9hjfqQARroqhYfajGNh5pRUPz8E WMLlVoGd0msDKEMUMzLbhOdM0mn121D/wDkwNylo0SKa1TVxoimyYZuLSQpO4DaR3EQ6 rThw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749201365; x=1749806165; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=LArt7Dh32wjw+3zD6UboJLzqZe99Yzpi0ogBUzjWvRc=; b=EDXDfptQMj6OHAy+9p9P3aaFD4n/5IROnIWMDbNWgHkGpICmn1/4Qy2S59SE7XPTeW /xst9TXbkV09S//rwYm6dRYD7cMzoN/ITdk7atIJNGzst28PQ3fMNa0wvpRCV5ULJYtj 1qOTCU7gr+bkNdqMZU6jVK2OXZQqJXlrTda/mlcFzLPuHhGZxAasM5EwBzg1GVv9UKlF 5aFS8hI8n/gjq6zSfX1kUsnUS4W5cZ4jYVsnbsfM6mnqhOaMyqxSMlkqOZFMbZPUquuM QnhoeCmYa6K3Rd1DZYqoNbTcr7IGUCPdzq21dCZsOIArfrq+mlEikMwwsnDyyRT8B4OG /8jA== X-Gm-Message-State: AOJu0Yxq3RI5q4cSiS5zWgCzKLrAn7N9YdJlebigXkwzglAcZwBeZnHC WFmySPa3cya28SxFsKsAVeFBoUBRqmSGPlcyBKfZek160vNaBXcU833kSxpX27Kz72OBUpOK79/ +KvuQSDJ3dJiGEMcB1Sxqn6oe9NkIMCYz/tw++1c= X-Gm-Gg: ASbGnctKxx6L6xqWw0XY8a0LP7OKVdWXs5KwwUns4nZXJKk66yrRqSAv2nMDRazpBf8 KdZ0B5Fv28vdOr/EqF9IvV0f2senXrK5U4D7hZhVk0eaJURUrKVqFG7quW5gx1hxStRM43646x2 CnyrlS28ccSR1YnIgAiJbOCAqxLwVwQnYe X-Google-Smtp-Source: AGHT+IH4Mui/NrbSyhguKjj0uHo8fS1R3vfDlun3BMBsKgdcoI3oSjbXOk6zkw1reIv/j6kjW94RDUNhG4plLqVbJUg= X-Received: by 2002:a05:690c:f0f:b0:708:6a2c:147b with SMTP id 00721157ae682-710f7633ed4mr33787887b3.7.1749201365420; Fri, 06 Jun 2025 02:16:05 -0700 (PDT) MIME-Version: 1.0 From: =?UTF-8?B?0JDQvdGC0L7QvSDQk9C70YPRiNCw0LrQvtCy?= Date: Fri, 6 Jun 2025 12:15:45 +0300 X-Gm-Features: AX0GCFtMeL_yKg0JoWfOtYUkyNUpWjBGSXL-UfCEpkNDFadfTOfZHb6psC_UaCs Message-ID: Subject: order of pg_dump command "create sequence" To: pgsql-admin@lists.postgresql.org Content-Type: multipart/alternative; boundary="00000000000006cf6f0636e3ae0c" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000006cf6f0636e3ae0c Content-Type: text/plain; charset="UTF-8" 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. --00000000000006cf6f0636e3ae0c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi, I found a peculiarity of pg_dump's work with seque= nces when they are not explicitly linked to a table.
I encountered a sit= uation (clearly abnormal use of sequences, but Postgres does not prohibit i= t) in which restoring from a dump becomes impossible due to the violation o= f 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
LA= NGUAGE sql IMMUTABLE AS
$$
SELECT 'PREFIX_'||nextval('pub= lic.my_seq'::regclass)::VARCHAR;
$$;

/* table, the column of = which will call the function */
CREATE TABLE public.exp_table (id charac= ter 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 pub= lic.gen_id()"
2) Creating a table "CREATE TABLE public.exp_tab= le"
3) Creating a sequence "CREATE SEQUENCE public.my_seq"= ;

And here the problems begin.
If we try to restore the table str= ucture from the dump, we get the expected error
"ERROR: relation &q= uot;public.my_seq" does not exist
LINE 2: SELECT 'PREFIX_'|= |nextval('public.my_seq'::regclass)::VARCHAR;
^
QUERY:
SEL= ECT 'PREFIX_'||nextval('public.my_seq'::regclass)::VARCHAR;=
CONTEXT: SQL function= "gen_id" during startup"


It turns out that Postgres does n= ot 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 appea= rs 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, bu= t such problems can cause, for example, an error during an upgrade or logic= al replication, when you need to dump and restore the data schema.
--00000000000006cf6f0636e3ae0c--