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 1wWc5c-002jlO-2e for pgsql-hackers@arkaria.postgresql.org; Mon, 08 Jun 2026 15:40:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wWc5b-0032Wj-1s for pgsql-hackers@arkaria.postgresql.org; Mon, 08 Jun 2026 15:40:35 +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 1wWc5a-0032WY-31 for pgsql-hackers@lists.postgresql.org; Mon, 08 Jun 2026 15:40:35 +0000 Received: from mail-ua1-x929.google.com ([2607:f8b0:4864:20::929]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wWc5Y-00000001hrf-2mbc for pgsql-hackers@lists.postgresql.org; Mon, 08 Jun 2026 15:40:33 +0000 Received: by mail-ua1-x929.google.com with SMTP id a1e0cc1a2514c-963ebce7076so934675241.3 for ; Mon, 08 Jun 2026 08:40:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dunslane-net.20251104.gappssmtp.com; s=20251104; t=1780933231; x=1781538031; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:autocrypt:content-language :from:references:cc:to:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=MRLCwr7vW9hKJeDEsahTbLrry6YvT6xa7UaSZ1JBMQE=; b=iA6wyCNHlVbOxD7IfYuIXSx6juFav+lDaweRyW84VSHC6b9gJLgUFc1Tn1LLJLf9Di JQpXCIQkX3UYFfvKuP72alY3tuUe0nQbw7ztIOld6VqYMgvGVZHBZhGMbogVWGFPXj5m Nu4r7ND7MjApkzl2fCZFeVpmXPmlZhvYowZI1RbTCXZ+Kx5q73Pq1GSlkQ9cETAE7hIQ 5c8EgBYQhOpw/g3crpPmj438ss9TzGBMpSBo3x4raRTP4HCA5Vhem1RjJ1CWGPO6LsKm oHGxMjknyJ9fY/Kyfc+i6yozfM20gSL4KyjmbDvu1EXqvIenBlWrQCmwaIVKWHlr6OlW MKRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780933231; x=1781538031; h=content-transfer-encoding:in-reply-to:autocrypt:content-language :from:references:cc:to:subject:user-agent:mime-version:date :message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=MRLCwr7vW9hKJeDEsahTbLrry6YvT6xa7UaSZ1JBMQE=; b=LU8hXGOwBTiCrlBOC1aWUzogF8llfFGV5HNbqgmk1uKm39VlZwzpKj7QSKcF7dWPop ghIIU8hwuy+6Rg0di4asts8JNVSV6MzCnsnQf8zzKS2Jt4scgUSpQfCfWc0KJbDC+w8X dIXyLR0Gbd3ErcF3HqGlkUPlZq3qqfIZoIo/6PiJb4Y63fAmwlG9fUKlSUF9u9KF2VDa k79Mx0Q9V1QWgnZXu4C/ANeWgrLeFuWSMyyWBMfbfv7CCKHaE1QavqByyoxz5hglvaVN izmUSIR7BSJUiGavO+t55xBn+HDccTtxcnAtxhbGh6+gFLYP6NRsgAVZT91l79HE9w26 qo0g== X-Forwarded-Encrypted: i=1; AFNElJ8g9WE+5ape9HFHjePUv8gBeSUjZABLrSM0QUyoH8rOz6r7i5Syl+sGJjtWZL4Wz45mAX3mV80QDk8GYCT6@lists.postgresql.org X-Gm-Message-State: AOJu0YxC1vXXWZMrJv/00LJPD1O/IhJ0nExCDfAiA3qilCsC5wezC1UY 2qSJgkK0a7qLKLOr6BuM/itG39gEpQrrG6T8K3/AAr6TGfj5HB8POGXX0HsrYB21UnI= X-Gm-Gg: Acq92OFlgWPAhfd9lvc+YkLGs51ybTdMYTjvbJj/AhKkr3rC4g9U1OvZcT9C1FgYoQ4 /qhQkNsITYNlqkXcmDY2g44dLc/vvwRlUOvOLTvsG+paTisPiIgJ/LKHUzb2dXnqXNqRXWxxZ/C 4nK2mx3WGoFwMTB5+vbiGna673qOkkmrOxc5R8r46DtuMB/VKmuMq503VVdZa/n4gCs8To5+kBS ipiZFU/ohBNYXe84yScbQ2tB3q+G/V7AjV+v4rFUS75kv+pgamxO6WpjL47amaOkB3F3k0BRjnl nsFO4T6ahC7WQwiA43sVhr1m2/sldQPuqYPGq5Xjb5xLMRfF/k5KarjX7V110df/0InF5UWEBO5 TeTED0AXlZMbsxiN9P9w7u2QMkP1aaqHdXtU+AlqB3w8crsQ2dWJS1UIMTihDsnnfyJaVRD0u2I dM0OSVjHj64+K6gp2vpM5Zyswqltj+z2Akqj1RTvk7Tx6cRMqgePgnBr0fagt0imEODz/ikO2EW YomWg47mw== X-Received: by 2002:a05:6102:b13:b0:622:702c:fd3f with SMTP id ada2fe7eead31-6fefcf7e01amr8141623137.21.1780933231139; Mon, 08 Jun 2026 08:40:31 -0700 (PDT) Received: from ?IPV6:2605:a601:a6b0:500:ccb8:56eb:bfc6:86b5? ([2605:a601:a6b0:500:ccb8:56eb:bfc6:86b5]) by smtp.googlemail.com with ESMTPSA id ada2fe7eead31-6eb36325d59sm15238789137.0.2026.06.08.08.40.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 08 Jun 2026 08:40:30 -0700 (PDT) Message-ID: <12802538-383a-4464-9a7d-de1d98bd6d08@dunslane.net> Date: Mon, 8 Jun 2026 11:40:27 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Fix domain fast defaults on empty tables To: Chao Li , Tom Lane Cc: Heikki Linnakangas , pgsql-hackers@lists.postgresql.org, jian he 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> <801E695B-F659-4D21-95E8-02F9EE505AF8@gmail.com> From: Andrew Dunstan Content-Language: en-US Autocrypt: addr=andrew@dunslane.net; keydata= xsBNBE7KWFkBCAClridxur2AIc7eW2AR7izbfp3EnNefie2HbLF0izW5Ik5UjX2HBXBx4syI gY6b0ugohXrr274+baoAlvSbq6cAoQuEVrk5IZFzt20b1Xkx65FwGSEj526yiKLocqkJceSq Xr9xcA5SGY+FZv441chh5SU92v4q6z+6LPpoHOh97ptAVXZYNTtU0LevyvD5lja0TzbvJm6C eFXitJfnm1pLEr0DGJCR/iUOl/N62Kh4855zZC7NHIjQHPOvV5Stz/l5ilDhvGVk+xkXFPys SjZoUr1rXhYLpiyi5sR0X9FHXT0KnGuz1F5ERO7ZTLSSQ6fJwPj6gOk9K+vvoKvoeql5ABEB AAHNJEFuZHJldyBEdW5zdGFuIDxhbmRyZXdAZHVuc2xhbmUubmV0PsLAlwQTAQgAQQIbAwIX gAIZAQULCQgHAwUVCgkICwUWAgMBAAIeBRYhBOQ+WEYd/Hy/RGkVpZn6f8tZ/DuBBQJoGNGd BQkdEO8nAAoJEJn6f8tZ/DuBq74H/jkTR4Zi3stbw+xC7v2u3QozssK7MYPL2AsVfh7OealS h182fiWXpfvmmAB7WUHbhk9GC2RAOnHI/2d2jgKaMLAHsGYOT0YopTVIwRY43fCw/mK67yxc wmDcX+zyKfLaivNbf5A7QPLNwda98bEAMSJ8Sn652Uc6cA8t3uKGsVzbRBQOoYzjgvBCfSrE 9ql3PDNg0l4BfAqabd2f70ZUm9VAMEPrgv/v2xI7M2XiL4g5BVmqLCOwxLM8RMCotCuoweUr VO43DeBCIDwLxotMJKvGWDjBzQYlU1NPUAtNcz/gN9ITUe1VUGjyvGj4u1lxBOcQQUw7l1+T 5moZ4iZxXzvOwE0ETspYWQEIANGc4zQULOxhbqO2dyD51YhqCNRmm9oKWaqf+wmW4tpDe/VV cxAnNizd4LWCHfzpb5cHAtGkOPePMfzWVf6nvdF7d3eglbtf59+zG7O7llV0xSSoFiieQBsr GvqDInXYX/4mRRXMtyhM353/tixC9RWLs1oofyYmCPPXXY7h9R7en3B8BoVrRFcdzlIY/NFN hFGW/9dkEiGjgna2Rk6e15kln4ZvFBWUg23p93w/pqXcxY6+k/8TEk+C4R+M6w7o2PLGOjdZ +kPiUcw5H85zf/yZJwQXzisXaNduwWB6Vads9YC9dj6kPR1c4VGRqAaYL++LAEOqrlvm2Tvq QqZRtnEAEQEAAcLAfAQYAQgAJgIbDBYhBOQ+WEYd/Hy/RGkVpZn6f8tZ/DuBBQJoGNI2BQkd EODdAAoJEJn6f8tZ/DuBfw0IAKTsfD40teP/pp+bsLLMSxPXUYrrprTj7WFB5v61p6dkpSr/ qXmMlyahdxQFaPmfVgVirB1Vk/kHiWNnnGjfUV9nB2Zg9LI0Xb9/ts3LsUiRWXzG3tkMY6XL vsVOxW4XFRND9l2q+WW93aZ1DZl+fqWfYgMvsusFRhmGFOKTRfKPta2Pkv+AhA24N4+PrR5p bU4k2MO8PAGiK8eaYKGFG1bHKuAvoDoF7WXJ3FHxuWqLnKEt4dfOLm5pAe3zq1Lt6q8azT9i QWGpSAK5vQUWQHBHpiDjdPeqKZ6HiAXIIKfSmb+jrvXBqoP+D6/K7rUjG2aXiRtTIAXms9sm VRu7cmw= In-Reply-To: <801E695B-F659-4D21-95E8-02F9EE505AF8@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2026-06-06 Sa 10:09 PM, Chao Li wrote: > >> On Jun 7, 2026, at 04:19, Tom Lane wrote: >> >> 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. >>> Seems reasonable. So which of Chao's solutions do you prefer? I think >>> both will meet the pg_dump issue, not sure how much we care about the >>> case where we have deleted all the rows but not truncated the table. >> [ 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. >> >> 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. >> >> 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. >> >> However, I doubt that a bare "RelationGetNumberOfBlocks() == 0" check >> is acceptable: we probably need to let the table access method have >> control of this. heapam could do "RelationGetNumberOfBlocks() == 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. >> >> My recommendation: we ought to revert a0b6ef29a for now and >> redesign the optimization for v20. >> >> 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. >> >> 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. >> >> 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’m 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. > Given Tom's reasonable suggestion that we really need a new Table AM callback to do this sensibly, I agree we should revert it. Will do. cheers andrew -- Andrew Dunstan EDB: https://www.enterprisedb.com