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 1tmWyv-007Oz5-JI for pgsql-general@arkaria.postgresql.org; Mon, 24 Feb 2025 11:50:42 +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 1tmWyu-001o9l-Jz for pgsql-general@arkaria.postgresql.org; Mon, 24 Feb 2025 11:50:40 +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.94.2) (envelope-from ) id 1tmWyu-001o8V-7f for pgsql-general@lists.postgresql.org; Mon, 24 Feb 2025 11:50:40 +0000 Received: from mail-wr1-x42d.google.com ([2a00:1450:4864:20::42d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tmWyr-000TAJ-0T for pgsql-general@lists.postgresql.org; Mon, 24 Feb 2025 11:50:39 +0000 Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-38f3913569fso3019463f8f.1 for ; Mon, 24 Feb 2025 03:50:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1740397837; x=1741002637; darn=lists.postgresql.org; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=+m0mj1fkOk0FAYf0XkIb6bdJv8KLOaNE/IjvibdFTmM=; b=X3m9gM0c0fm/5y1RMPCAtEJ3NTmX1YfXONwKC9EVw0cLMPOEuZfHPXo3pNEhFO/7rP bXD6VFBMjO5p3kGWt/ivaNxB+nEQDzlROMCH0+8QsNc7Mdaf/9je8wS5mP+55icJhrhB YF9Zz1dhlF1w1RH+YgwVsh697q5U++rcu4iFDZkZ0KJvVFn45FT+fLrfTRN4itl1JXpd m0VfK9uhVsC+Z3YZzfN1kHS/szgRsCR0jeeeK524NzyBFLqSKKjJLDu2v18sZ/3SjzAX 1hacedfxyL6GzHDYLA4d95uSQe/PD/7WGgPJv4VwFjjr/eOd8vuUmu1tn74jnTHLHCg/ F1GQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740397837; x=1741002637; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=+m0mj1fkOk0FAYf0XkIb6bdJv8KLOaNE/IjvibdFTmM=; b=qmU7m78li/RfYsOnggoLpogP6Mm6g8X+flGU2FHr58F8ARUjtx1Rjq3DYfQ3RCEy+Y Ly/JS2KQ+eacX5Lm0+iirYIXHOZdeP6eKW+6AnCKaO9YyKZpu0R+INRoR4NHHwF7YKIa DrgcRwGbzsny6nEvr1P+PaxQsAGViBWmRR/YLSvDy5WXRr/E6JQTRbkiA4HqakOkROUy O014hNSZZ3xuwGpKAD1zl5JB8RxNAxSnOZFXeWbMhEI5hQ6SljL78DgXLRMeJaG6CX7r ifSNPg93Rj7Ur4Ht9M01MYaICk+40C6hC0AFuwwU/QxPHoG61IripBV0BOCFnAxsbpdY Mdyg== X-Forwarded-Encrypted: i=1; AJvYcCUlr3C5ixMP79X49MP7mHryu6oo2Jeyq0LwJOk7G8Uf8TgCiNjk7iWhw6SaYOlpWrrId2C9Tx9+WAdZqGju@lists.postgresql.org X-Gm-Message-State: AOJu0Yx7H4XzOQ4WAoFbPfV9Rfrgc51pYSCJBpi3b0Z3g7JiNlhO1bHm NpUX6/UOLX4O5PQuMjbT9+oEMVc6DWnm6YIxbUPlSl1fGSLHOzd25J/AsF8h1S8febUl2Obd1iL WeW2sN3q1DvWRCHzX6XRgVa2B3SVkM6aFgKIKybVsRJ/OtfPtBeH/pgJ8PN9iBdbU X-Gm-Gg: ASbGncu7ZdRr1YznYkUmTA0KrslUe42O2stuSXWjsEjGlSUq4lePka/sRr3kn06+zkR PZhwfo1oMdF7ZA73IL0JqpkYuiLKhtXKmIcd6yYUBTkCDglQXMC/Q4SAlTA0sAVkmZ8ad9UFyyJ VymhkHzaXQXqeDmP9oVh6x60pvqAMEjmROmvlXacjQZfMlz8eGeO6qN6A8THJ/gVoUldfH3wKXD m6G1tfDnk9svY3GD0StLdueom99R6gWUgMG85EOG35vVTYp/87+WvnOJCMS8jB+aSAy9glwBeJ4 IKK1XpL4kjiyH6FOWPe1QV22KeXjog+fliU0OdeQWgW5kA== X-Google-Smtp-Source: AGHT+IHKJSLKFCoEJXUlrt1lnogjNf+ki+hBRQf2N/M+ESkaKr5OVnCiSPqKSs4UZr1YGUM78OAvVQ== X-Received: by 2002:a5d:6da8:0:b0:38d:e3da:8b4f with SMTP id ffacd0b85a97d-38f6f378d86mr9350309f8f.0.1740397836863; Mon, 24 Feb 2025 03:50:36 -0800 (PST) Received: from localhost.localdomain ([2001:871:5e:381d:19e6:2832:a3b1:b1ce]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38f259d8f1csm31847840f8f.69.2025.02.24.03.50.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Feb 2025 03:50:36 -0800 (PST) Message-ID: Subject: Re: Default Value Retention After Dropping Default From: Laurenz Albe To: Marcelo Fernandes , pgsql-general@lists.postgresql.org Date: Mon, 24 Feb 2025 12:50:35 +0100 In-Reply-To: References: User-Agent: Evolution 3.54.3 (3.54.3-1.fc41) MIME-Version: 1.0 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 Mon, 2025-02-24 at 20:56 +1300, Marcelo Fernandes wrote: > I am experiencing an interesting behavior in PostgreSQL and would like to= seek > some clarification. >=20 > In the following snippet, I first add a column with a default value, then= drop > that default. However, when I query the table, the column still retains t= he > dropped default for existing rows: >=20 > =C2=A0 SET client_min_messages=3Ddebug1; >=20 > =C2=A0 DROP TABLE IF EXISTS foo CASCADE; > =C2=A0 CREATE TABLE foo (id SERIAL PRIMARY KEY); >=20 > =C2=A0 INSERT INTO foo (id) SELECT generate_series(1, 10000); >=20 > =C2=A0 ALTER TABLE foo ADD COLUMN bar varchar(255) NOT NULL DEFAULT 'defa= ult'; > =C2=A0 ALTER TABLE foo ALTER COLUMN bar DROP DEFAULT; >=20 > =C2=A0 SELECT * from foo order by id desc limit 5; > =C2=A0 --=C2=A0=C2=A0 id=C2=A0=C2=A0 |=C2=A0=C2=A0 bar > =C2=A0 -- -------+--------- > =C2=A0 --=C2=A0 10000 | default > =C2=A0 --=C2=A0=C2=A0 9999 | default > =C2=A0 --=C2=A0=C2=A0 9998 | default > =C2=A0 --=C2=A0=C2=A0 9997 | default > =C2=A0 --=C2=A0=C2=A0 9996 | default >=20 > In this example, even after dropping the default value from the bar colum= n, the > rows that were previously inserted (prior to dropping the default) still = show > 'default' as their value in the bar column. >=20 > It does not see that the table has been rewritten or rescanned, otherwise= the > debug1 messages would be triggered. >=20 > Can anyone explain how PostgreSQL "knows about" the default value that ha= s just > been dropped and what is happened under the scenes? I am keen on a deep > understanding on how Postgres achieves this. The "missing value" is stored in pg_attribute.admissingval: SELECT attmissingval FROM pg_attribute WHERE attrelid =3D 'foo'::regclass AND attname =3D 'bar'; attmissingval=20 =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90 {default} (1 row) That value is used for all rows that don't yet physically have the column. Yours, Laurenz Albe --=20 *E-Mail Disclaimer* Der Inhalt dieser E-Mail ist ausschliesslich fuer den=20 bezeichneten Adressaten bestimmt. Wenn Sie nicht der vorgesehene Adressat= =20 dieser E-Mail oder dessen Vertreter sein sollten, so beachten Sie bitte,=20 dass jede Form der Kenntnisnahme, Veroeffentlichung, Vervielfaeltigung oder= =20 Weitergabe des Inhalts dieser E-Mail unzulaessig ist. Wir bitten Sie, sich= =20 in diesem Fall mit dem Absender der E-Mail in Verbindung zu setzen. *CONFIDENTIALITY NOTICE & DISCLAIMER *This message and any attachment are=20 confidential and may be privileged or otherwise protected from disclosure= =20 and solely for the use of the person(s) or entity to whom it is intended.= =20 If you have received this message in error and are not the intended=20 recipient, please notify the sender immediately and delete this message and= =20 any attachment from your system. If you are not the intended recipient, be= =20 advised that any use of this message is prohibited and may be unlawful, and= =20 you must not copy this message or attachment or disclose the contents to=20 any other person.