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 1wWVEV-002f1R-0p for pgsql-hackers@arkaria.postgresql.org; Mon, 08 Jun 2026 08:21:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wWVEU-001L0Y-0M for pgsql-hackers@arkaria.postgresql.org; Mon, 08 Jun 2026 08:21:18 +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 1wWVET-001L0Q-2f for pgsql-hackers@lists.postgresql.org; Mon, 08 Jun 2026 08:21:17 +0000 Received: from mail-vs1-xe2b.google.com ([2607:f8b0:4864:20::e2b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wWVES-00000001emr-06vu for pgsql-hackers@lists.postgresql.org; Mon, 08 Jun 2026 08:21:16 +0000 Received: by mail-vs1-xe2b.google.com with SMTP id ada2fe7eead31-6efdb57db0cso1110626137.2 for ; Mon, 08 Jun 2026 01:21:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1780906875; cv=none; d=google.com; s=arc-20240605; b=lVAff1ZTnDfxnI+ZcfXdsC+p8O+p/jnMI4yJkgh964DVjc1fK/lcRIhLmVHUCzXcAS s3HmdOfZkYJqR2a+Hnd5xIkVggPh+dr8HrBDRPzAPeS95HkiYgyBdNkWP1YEkNDL8TLA ob73sh/m1+jydfrexOfSgePmHmaSanRH2kkxfaKn7wJdzVIlBt/TLc0yyo2NhC5DWzKV EKlyG0mA6b6z2BTMQKUALETiv6fGS7sovUoqN2tRRZSzuhBj5UkCVDR1xjhXTyNhi5ak LYw2isgDGsYp5jpqkRWb4BsC6CrXHRTqjhwI3hNifhp5r4pnPQrJoghxB7V04nmpt1Ep 3YNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=RpzSpTVepScfw9GGIMQMOc/fRdM3rG+AtaU9d0HmvFc=; fh=NcO64pdmAkoS2fMc1u5kzqy9dbYeXSrA4engQ9vLLs0=; b=WQISNM1sIJoT3mKZ9HyOF+BTSNhAJGth3oaWIYcbmhluVaD2ybZxg11HSdofG6Ein+ 6xSFcdUst0BqVJE/eBzeK9QnKp/etcxRQvAVUUDijVZWNwXCaKzyCnU2sy+QcuOUbVmG V4lYqE5+3/o6WMFQH+T/X6viXjafPuieKV3EK6Sg0P44jTBnmN2AiSIuct7miBXnCHNA xQ41v6fctHtE4/HzM+DreF3Njg5COREXJh0TKVhB1/iHmeX8IxcVFQla8stuFkHylOHg 6fsjr3qN7kw9aR+3+9hgKEoX13K2Pfs9M2I1Qqt8OJtRjaNOM2e7qTIzvuVT1DnXMUWX 7VgA==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780906875; x=1781511675; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=RpzSpTVepScfw9GGIMQMOc/fRdM3rG+AtaU9d0HmvFc=; b=tMFIZQU/G1ap3AhWmRTdtwkWKfSBRtvEcAgGJbBqMl9oDPjXLzuxI7/LakNxk0khYJ pWkFSUGo3HSRjeKVHVvXNTNHjIV71N/rWt5o896zICRuCCN5mAnPBhWnLmgxKxvC18sa XdjI73A6+Pxirew4HDmitleGHXBg/bhzYU48ltolBMUdYhhNBarEl3nMfKU/U7PP95iE avwVcSzAb55AV64BVQGG6w1Htx8nCWxW9VM8f5lXj3qvfJmTzB9F/r2re9CK+pMN6Uiu o/ezzsUqexq2AU4IiElqIQUlKzZT3pzHqu+VeOUdaxkLDeUhSXJL3R9g376Bnp+eKhug dulw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780906875; x=1781511675; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=RpzSpTVepScfw9GGIMQMOc/fRdM3rG+AtaU9d0HmvFc=; b=TOPv3U7Ha1mrmZrYnF6+hin0aZaRh4jbYlf2GaDui3qvbWph7KBCtLZjPlcr8j/NFq VCWy5IBGXUfF37kMwA2luqv1E2ECTT/dxvDoEdFFk3myGcG6T0QpYu6zcmS9hKWZ6B45 GlS1Uw8h5lBi8E6FKbcLSI6awman0IuE24XOpAy/xwAk433CA2X2G7enbqQfGsynwFtl U5fBAP8Oi1LFUUUAlPOo+sFQZHIwHJR5ds/FrLYy/Y7mCNNwaihUd4lWI7TcctlJCBhp eV1UQQp5tnNvY08JUNkCHXE+AhE7fDa17oyz3j2tnTXpjbcDE3Q4eIpv3wenvYDDKY+Q QOeQ== X-Forwarded-Encrypted: i=1; AFNElJ+PIQvlvUCb5MjGA+RNg6YLR3fNV9ZHrX+57HSwa1lT4KcVSKVVDoOxiaN1/ST9JQRNI4MZSfFzEMbBNsgY@lists.postgresql.org X-Gm-Message-State: AOJu0YzAc++Cz0FUiKx6f8t3ph8rPB/ID05lLxFVkPOKWNdDj/nsTDE3 cvenj2Wdgcbn4dWn5VD5eE1Eu6OZ5cQK9g8ZYCmgvarCQNMZLJx8TQD++fIW7faHkamECynCPO4 CX5uy9x6IGhU2tPXMm6Zxhvuth25gAks= X-Gm-Gg: Acq92OHxi1j+ISMfC0Fe7iSk+WE+QfjxUy5r4/wowS5DPY6Y7GDvaz16m6P85E271Wn h9oejSKlxLYzghrS5swaSbrT9TiJYT0OanMCqmz+ZX9jh5uZVWW1YNkCVQ8Yj/LjH99JT08Digq Avg+K1lORFdkIGV1LM8db5789XZv2KaujWMWlxpgfgIBNQ4vlDtHZp2KuKxOUnnwHQWiVZoI0Uv g9SmzEv41yDV9YlnDO9QfzRWZNO8VcbAqOaZ4ROE7T6QoYPIyRK+SOQNmgdl9tkYDmXgUwxsRq2 ZzRyAGB6HYksBX5lCVJrs0fv6JSGfJy/WwxhK+x9Q9oAE/mrDQNWLO1yzZncHw7B9S/9DXo2eh4 3JqOIaMf/AeVL+TrdGj2/LQmky3zaZdNNRofJAB0cnkEVMoVJcEB6D1V2KVcJ+TLoU+wBanIOl0 gJLhWVyf4Lt5+G+Ej8Zjz8t03W0IDwdoXZ4VUU1wi45SF7 X-Received: by 2002:a05:6102:dc6:b0:6de:3c29:373b with SMTP id ada2fe7eead31-6fef998979amr5762734137.15.1780906874709; Mon, 08 Jun 2026 01:21:14 -0700 (PDT) MIME-Version: 1.0 References: <7033D663-DDB4-4B35-922C-F33DE53B1502@gmail.com> <219843CF-9B49-404A-838D-88D51902B978@iki.fi> <3590551.1780668509@sss.pgh.pa.us> In-Reply-To: <3590551.1780668509@sss.pgh.pa.us> From: jian he Date: Mon, 8 Jun 2026 16:20:38 +0800 X-Gm-Features: AVVi8CcXSBPbiw5WOk8ZYAOGJvdSaPfTcxJOxCs1Lw87L5YMD_9OG2ungMOLC9o Message-ID: Subject: Re: Fix domain fast defaults on empty tables To: Tom Lane Cc: Heikki Linnakangas , pgsql-hackers@lists.postgresql.org, Chao Li , Andrew Dunstan Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Jun 5, 2026 at 10:08=E2=80=AFPM Tom Lane wrote: > > Heikki Linnakangas writes: > > On 5 June 2026 10:48:00 EEST, Chao Li wrote: > >> evantest=3D# create domain d_div as int check (1 / (value - 1) > 0); > >> CREATE DOMAIN > >> evantest=3D# create table t (a int); > >> CREATE TABLE > >> evantest=3D# alter table t add column b d_div default 1; > >> ERROR: division by zero > > > It seems totally reasonable to get an error in that case. '1' is not a = valid value for the datatype, whether or not there are any rows in the tabl= e. > > I think there's reason for concern here, which is that we do not throw > an error for the apparently equivalent case > > regression=3D# create table t2 (a int, b d_div default 1); > CREATE TABLE > > This will give you an error at INSERT, but not CREATE. So this > is inconsistent, as well as different from the pre-v19 behavior. > > 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. Per the docs [1], we have two relevant forms: ALTER [ COLUMN ] column_name SET DEFAULT expression ADD [ COLUMN ] [ IF NOT EXISTS ] column_name data_type ... [ DEFAULT default_expr ] ATExecColumnDefault -> AddRelationNewConstraints does NOT evaluate the default expression. So ALTER COLUMN SET DEFAULT will never error out on a bad expression because it is never evaluated at DDL time. ALTER TABLE ADD COLUMN with a DEFAULT: pg_dump.c will never emit such a com= mand. You can verify this by searching for the keyword "add" in src/bin/pg_dump/pg_dump.c, and looking at each occurrence one by one. Looking at dumpTableSchema() confirms the same: we do not produce command: ALTER TABLE ADD COLUMN. See dumpTableSchema below comments related FOR LOOP part also /* * Dump additional per-column properties that we can't handle in th= e * main CREATE TABLE command. */ [1] https://www.postgresql.org/docs/current/sql-altertable.html -------------------------------------------- create or replace function dummy() returns numeric AS $$ BEGIN RETURN 1/0; END$$ immutable LANGUAGE plpgsql; create table t1(a numeric default dummy()); alter table t1 add column b numeric default dummy(); ERROR: division by zero CONTEXT: PL/pgSQL expression "1/0" PL/pgSQL function dummy() line 3 at RETURN As you can see, if pg_dump somehow converted the CREATE default expression = into an ALTER TABLE ADD COLUMN DEFAULT, we would already be facing this issue. Am I missing something? -- jian https://www.enterprisedb.com/