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.96) (envelope-from ) id 1wW2y6-002M7M-1n for pgsql-hackers@arkaria.postgresql.org; Sun, 07 Jun 2026 02:10:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wW2y5-00Gnpl-0I for pgsql-hackers@arkaria.postgresql.org; Sun, 07 Jun 2026 02:10:29 +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.96) (envelope-from ) id 1wW2y4-00Gnpc-2a for pgsql-hackers@lists.postgresql.org; Sun, 07 Jun 2026 02:10:28 +0000 Received: from mail-pf1-x42b.google.com ([2607:f8b0:4864:20::42b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wW2y2-00000001TYN-3pwc for pgsql-hackers@lists.postgresql.org; Sun, 07 Jun 2026 02:10:27 +0000 Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-84275887a3fso2654616b3a.1 for ; Sat, 06 Jun 2026 19:10:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780798226; x=1781403026; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=QHBIdE9/+FvTnGEjwkw1R6e+kDfgu+GtyXIwgq9CGcE=; b=sW+BO07NPHgJFips/9QOH2PpYMgmQDLwLOzltt187R+D1cQ/V93XIVeRsrRCfveHwj iZyS3CrAEEkJNnDs3PqmhnGRFwzRO9Zb45PAczenxhk3pv9LcRGXbKvEDT5KRBUgSh4S /CIlfqvH5xOzuuPF4URKIAnVzc8lpqwuSXS97wuGAeTZKSsbpj7GPqitwNSMV0noP16p jXPYdCDBhU8H7HqqJRex4SiCmoScaticgUZfSBw+VaVtKnBgoxkAXTRENXe9yqa52YIQ 6IkDaZJ75h753RNOfiv6YYXMFyjCdAvdBBn0fkWtQd6DbLo0kxskt5dvg1uFqaW7Trcv XWPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780798226; x=1781403026; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=QHBIdE9/+FvTnGEjwkw1R6e+kDfgu+GtyXIwgq9CGcE=; b=GdpaOQxZEEjYOATlflXhDwNW5xI13jA5wXgmoqnOF03CUIRVDrz1rcnjXHFV640jvH 5Yo+r2/7EKPLCczmRhP5zf1yjRUsxLoDxZP3DiNEqMI/qd3vrsoJjx6VjuP+9fcucBBZ F6hqnh0a4v12Tn6Ulhmt6weCHaCvi2ARulyTkj+6DXd6mLW6YnydLwTjb3ffEuLdJpLN e2jsWGnM4BVmMRCHU3mALpH9YUgtQ5TxETXSnxuX2vEy85t05r2nHnzwq8DYovOfMd7W m/ORsoEHxeR1EPZh2BJ2jJjpVW9i0RcU621aLw7C4uZhuWSrcUJvmIJEmth89LApq0SN Fs4w== X-Forwarded-Encrypted: i=1; AFNElJ/VtazRAWSMUf9GVGWXEdamQWybrlRKFmPyxlV1MYoL73Ic8332Od0cJW7iBDRCb1pRPKCxSMfAE6T0M3hw@lists.postgresql.org X-Gm-Message-State: AOJu0YwU0fmf4S0YhW3hTmw8uYc4j8SildMVBdwXFrv3XqHLG/EJcsP+ ckWP+88l+1DjxSDoXs0t6sOtQPyuimTlN4RKq2ryHqq2w1n0CAmyVQoz X-Gm-Gg: Acq92OF9Uxu3KuQECWubupjG/mzl5y8G3AxkXjp98dXeIi0Fzm5T1FwCRuLoP99Rs21 EqgsWHLP7ITfdtKZ5XmKBf7PFxLCI/bApnoOJ4sJqvOV1dptArbB8QsHDGaxRR8DpdjuR+FUj8h wnOEUplRNDYU1OzOkuC7WpY+9WJ8Eiz5odswG6djSPiFFwjwtpzGiKfUQYIhbXYlFJ6y0BC25Qq TcDTV30BTugCIhkogpg2cBsXiGrbKr0nAwJMzsm66GkZpA9l2YW8+cy4h0QC6gp8iV89hFLr1Uv j4Hairx2KhQzPbtG4FtfS01wI1lvcrYoHE7zhW1jZ3hD/Rb7KRB8mKGPWOeBROvS4oBz/GGGZlh wELlfHEJYDtbVBJaU8CyQfpEJfhZyzXtJMQdrnMsO52gLBQxt6lYJlVYApZeOrA30XDLUZjrP1D +D5iekRAbcwyCjOObZ0Wg6iXFV8zJHQTb/p2CP4rf4WtmuzPDESWuh X-Received: by 2002:a05:6a00:2d9a:b0:842:4982:825 with SMTP id d2e1a72fcca58-842b666ef07mr7310885b3a.1.1780798225536; Sat, 06 Jun 2026 19:10:25 -0700 (PDT) Received: from smtpclient.apple ([45.32.121.103]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-842828821c5sm12619277b3a.32.2026.06.06.19.10.22 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 06 Jun 2026 19:10:25 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.600.51.1.1\)) Subject: Re: Fix domain fast defaults on empty tables From: Chao Li In-Reply-To: <36611.1780777185@sss.pgh.pa.us> Date: Sun, 7 Jun 2026 10:09:44 +0800 Cc: Andrew Dunstan , Heikki Linnakangas , pgsql-hackers@lists.postgresql.org, jian he Content-Transfer-Encoding: quoted-printable Message-Id: <801E695B-F659-4D21-95E8-02F9EE505AF8@gmail.com> References: <7033D663-DDB4-4B35-922C-F33DE53B1502@gmail.com> <219843CF-9B49-404A-838D-88D51902B978@iki.fi> <3590551.1780668509@sss.pgh.pa.us> <203a49c2-52e4-49ab-b644-ef6044266145@dunslane.net> <36611.1780777185@sss.pgh.pa.us> To: Tom Lane X-Mailer: Apple Mail (2.3864.600.51.1.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Jun 7, 2026, at 04:19, Tom Lane wrote: >=20 > Andrew Dunstan writes: >> On 2026-06-05 Fr 10:08 AM, Tom Lane wrote: >>> Concretely, I'm pretty sure it is a hazard for pg_dump, which thinks >>> it can freely transform bits of CREATE operations into ALTERs. >>> I didn't try to make an example case, but I suspect it is now = possible >>> to create a database that will fail dump/restore because of this >>> inconsistency. >=20 >> Seems reasonable. So which of Chao's solutions do you prefer? I think=20= >> both will meet the pg_dump issue, not sure how much we care about the=20= >> case where we have deleted all the rows but not truncated the table. >=20 > [ studies it a bit ...] TBH, I don't like either of these, > nor do I like a0b6ef29a to begin with. The fundamental problem > with this whole mess is that it treats certain kinds of > default-expression evaluation error (i.e., domain CHECK failures) > differently from others. There is no way that that leads to a > consistent user experience. The current complaint is one > manifestation of that, but I'm sure there are others, eg having > to do with domain coercions lower down in a default expression. >=20 > It could work if ExecPrepareExprWithContext were capable of > soft-trapping essentially all execution errors, but that's > not true today and I strongly doubt it ever will be true. >=20 > I think Chao's v1-s2-0001 points the way towards what could be a > workable solution: if we see that the table is known empty (after > we already have exclusive lock on it!), we could skip both the table > rewrite and the insertion of an attmissingval, and thereby not need > to evaluate the default at all during ALTER TABLE. As Chao says, > simplistic versions of "known empty" would expose some user-visible > behavioral inconsistencies, but I'm not sure how much that matters. > For pg_dump and similar applications we would get the behavior we > needed even with just an is-physically-empty check, since all their > CREATE/ALTER DDL happens before we ever insert any data. You could > imagine going further and scanning the table for live tuples, but > I don't know that that's going to be worth the cycles. >=20 > However, I doubt that a bare "RelationGetNumberOfBlocks() =3D=3D 0" = check > is acceptable: we probably need to let the table access method have > control of this. heapam could do "RelationGetNumberOfBlocks() =3D=3D = 0", > but other TAMs might need to do something else. So that means that > this path requires a new TableAmRoutine method, and that probably > puts it in the too-late-for-v19 category. >=20 > My recommendation: we ought to revert a0b6ef29a for now and > redesign the optimization for v20. >=20 > If you don't like that answer, I'd be firmly against v1-s1-0001 > in any case. It repeats the classic mistake of supposing that > we can PG_CATCH random errors and not invoke transaction cleanup. >=20 > BTW, I do not like the fact that a0b6ef29a removed the comment > stanza explaining why we need this fake default expression at all. > That's still largely applicable AFAICS. >=20 > regards, tom lane Thanks, Tom, for the detailed explanation. I'll hold off here and wait for Andrew's decision on how to proceed. = Sounds like reverting a0b6ef29a may be the preferred option for now. For v1-s2, I=E2=80=99m not ware of a better way to decide whether a = table is known empty. I ever considered a count(*)-style scan or a LIMIT = 1 scan, but those seem worse, they would add a new table scan inside = ALTER TABLE just to decide whether to evaluate the default. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/