Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p2uCL-000254-6o for pgsql-bugs@arkaria.postgresql.org; Wed, 07 Dec 2022 13:10:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1p2uCK-0001uw-1v for pgsql-bugs@arkaria.postgresql.org; Wed, 07 Dec 2022 13:10:52 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p2cXp-0006vs-2J; Tue, 06 Dec 2022 18:19:53 +0000 Received: from mail-qk1-x72d.google.com ([2607:f8b0:4864:20::72d]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1p2cXm-0008WJ-Tl; Tue, 06 Dec 2022 18:19:52 +0000 Received: by mail-qk1-x72d.google.com with SMTP id k2so8153263qkk.7; Tue, 06 Dec 2022 10:19:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:user-agent:reply-to :references:in-reply-to:message-id:date:cc:subject:to:from:from:to :cc:subject:date:message-id:reply-to; bh=yBlcV5L5sj2y/wx97uxOt+ff0JtgyjILYJ4oQ09K5Vk=; b=g4k3JK427gyvdf9cCxfyzbw+2iudY4V17GXklukb7QtHoCqjRq2TiF0VN3x0LJTfhD HcYBR+EHwhBj73wHZ1/f/gzFkRlWlpagyKPGHq3VczyPSROA6PpDsMM1+uqRaCcl7hnO GMQlI3mlydhrFkLLMSwW48AfXn+KA5z+H6sjhWyRoN9NKmlguvXYwbQk2X8uL/pcdydc e6IH5Oh3zLrQhX2ThvACa2nD/nFO9Qu8LRZ2f5hX6Gl9aryPpeH/7oBSkoxaugstcAhK AOYOvxSLbSMhNkXMAUPj/FBzlTC6Pt+dcgiQLQZ/2eAm0hwNC3T7X0sMk7qnDFv6Gr5n P2EQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:user-agent:reply-to :references:in-reply-to:message-id:date:cc:subject:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=yBlcV5L5sj2y/wx97uxOt+ff0JtgyjILYJ4oQ09K5Vk=; b=O3LSifZGzfXxow/LShAZ7mmERRNlS8wKSpvVbGSfGiAwRmt3F1MSulllh2YFljp1hu 1YHTLibNzSB+4EY9btBLcFWtSqEMGuLD4xNlCCY3iN9qiGbskgbPZfDAD52s6BxqXP9x irmlRaYvzRFVbTdR8mzZPVS/WxXF5sT1asELoLxGZn+2/AsHZvtO7IWMQIzJ1ddwEagv t6/8xu58Z5Zqh1DV2yMocolnN2QyHviD4MLHmV2zylxAJMk06DE/gdjQ2eT6RvQ9CCRy YWyniJPngEmDtp5l882O2TZKmqhJnaRfgkUhsW91NtMHO/L4B9OjtBatFEdkybtk2239 2AwA== X-Gm-Message-State: ANoB5pns0sXC7cTL8Zg2zAJPUmbSlhgIJGPeUxoZ8WjH14jn5IklVVz7 QlAp+vrlSpuvO7JpEVN8GUs= X-Google-Smtp-Source: AA0mqf780kIf5fdcqMWX3a/v9WfKSWZIXfH9pJR6sviqfoNN7n1DebzpdfOKdz9eu1Dh3x/kei2neA== X-Received: by 2002:ae9:ee08:0:b0:6fb:def9:3fc1 with SMTP id i8-20020ae9ee08000000b006fbdef93fc1mr62335322qkg.335.1670350789985; Tue, 06 Dec 2022 10:19:49 -0800 (PST) Received: from [192.168.128.53] (c-71-234-60-75.hsd1.ct.comcast.net. [71.234.60.75]) by smtp.gmail.com with ESMTPSA id bm8-20020a05620a198800b006fa8299b4d5sm15498421qkb.100.2022.12.06.10.19.49 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Dec 2022 10:19:49 -0800 (PST) From: "Nunya Business" To: "Tom Lane" Subject: Re[2]: PG 14.5 -- Impossible to restore dump due to interaction/order of views, functions, and generated columns Cc: pgsql-general@postgresql.org, pgsql-bugs@postgresql.org Date: Tue, 06 Dec 2022 18:19:36 +0000 Message-Id: In-Reply-To: <3635234.1670340237@sss.pgh.pa.us> References: <3635234.1670340237@sss.pgh.pa.us> Reply-To: "Nunya Business" User-Agent: eM_Client/9.1.2109.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk >"Nunya Business" writes: >>Within my schema there is a table that has a GENERATED ALWAYS column that = calls a plpgsql function. The called function has a "row type" variable d= eclared that references a view. While the schema itself functions properl= y day to day, and pg_dumpall works as expected, the generated SQL fails to= successfully execute. The table in question is restored with no rows, and= an error is generated during the COPY stating that the type does not exist. > >Hmm, do you have actually circular dependencies in that? pg_dump has >some heuristics for dealing with such cases, but maybe it needs more. >Please create a self-contained example and submit it to pgsql-bugs. > > regards, tom lane Thanks Tom. There are indeed circular references in the schema and the=20 whole thing sort of doesn't pass the smell test, but this is my first=20 look at it. The generated column on the table calls a function which=20 selects from a view that references the table. The production schema=20 where I ran into this is pretty large and complex, so the contrived=20 example that follows may not be the minimum working example but it's=20 pretty small and has the same behavior regarding the SQL generated by=20 pg_dumpall. It seems that the schema is probably invalid according to the GENERATED=20 rules and that pg_dumpall is operating as intended, but somehow the=20 check in the ALTER TABLE isn't deep enough to prevent the issue, but=20 maybe I'm mistaken. Once this is created, if you insert a few rows and=20 execute pg_dumpall, the resulting SQL cannot be loaded and will fail=20 during the COPY, complaining that the view referenced by the function=20 doesn't exist. Here is the schema. CCing pgsql-bugs as requested. --------------------------CUT CREATE TABLE tblA ( id serial unique not null, dt timestamp with time zone not null default now(), data text ); CREATE OR REPLACE VIEW viewA as ( SELECT sum(id) FROM tblA ); CREATE OR REPLACE FUNCTION fnA(askid INTEGER) RETURNS integer LANGUAGE 'plpgsql' IMMUTABLE AS $$ declare varA viewA; ret integer; begin SELECT viewA.* INTO varA FROM viewA; ret =3D varA.sum; return ret; end; $$; ALTER TABLE tblA ADD COLUMN idsum INTEGER GENERATED ALWAYS AS (fnA(id))=20 STORED;