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 1tmbSC-007zf1-6F for pgsql-general@arkaria.postgresql.org; Mon, 24 Feb 2025 16:37:12 +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 1tmbSB-004thH-6P for pgsql-general@arkaria.postgresql.org; Mon, 24 Feb 2025 16:37:11 +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 1tmbSA-004th9-Rr for pgsql-general@lists.postgresql.org; Mon, 24 Feb 2025 16:37:10 +0000 Received: from fhigh-a1-smtp.messagingengine.com ([103.168.172.152]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tmbS8-000VRi-02 for pgsql-general@lists.postgresql.org; Mon, 24 Feb 2025 16:37:09 +0000 Received: from phl-compute-06.internal (phl-compute-06.phl.internal [10.202.2.46]) by mailfhigh.phl.internal (Postfix) with ESMTP id BEE7A1140145; Mon, 24 Feb 2025 11:37:07 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-06.internal (MEProxy); Mon, 24 Feb 2025 11:37:07 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm3; t=1740415027; x=1740501427; bh=DdMjrTTPiRUh1Unw4DCTIaVoQI/KbunNJoCXDY5dB1I=; b= rNV8p2+7LOa6RIVRwFJbRIX3hNlvMYyC3OUzaOjyoISK6QAi2+HZtFXYPfBwa0Gi TJ2Iw7IFV18WfGSeGUCvByBt52fJd6j47nbX5n2LLTdD6iNU03qqgqjW5gjoYTAb RTU8kQSRJ2MKfgxl2ncyb3buV5mcG+s8ijHgJMKwJwWyHO0yjX0n4Wqe9DNCD/WG U2cENJIED7QIeMmBCDvABZQDqPz7ONFcWL+zyXtxavNSzcL/Ur/SMabgwoV3iKiB h+Tea4s45qGhBqD8WntwUozEgSO1BIyWhURjmFdQRlrh8ve2GZt3JXDrAc5fwBP/ UCPKbgBSLp/9EbBfGHQJTA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1740415027; x=1740501427; bh=D dMjrTTPiRUh1Unw4DCTIaVoQI/KbunNJoCXDY5dB1I=; b=brFFWxz3eaTinn4iC +980nG+NODGa9nryrmvher8DbNc1Oj4/fcbQtp+KzODp46FDj2lN2bQmvD07rSHT 1XKfAnaQ5/Ci25mRpI4Gm38X8nkiaLV3WJinQL3HpCuEijUWoxRVhnGWlM1liseq aSLPU7JpOZDvKXj5xL1K5C0QWpDID+KhA/EQqHUkqRmbkoPal8cKP0qRw7e13NNv mvtgVmdqBk1Oc81rAUnXQoCg9/OtLs36IFYULzDetqlRzwCzBVnychdONGrVw5XH 3QbqS4RSlvTXcc+DZHyNvp4rC4ZvnZ5oJ5ZODXFYMovDvFJ7b17jtVgmw6jpT4lo mLJYQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdejledvkecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefkffggfgfuvfhfhfgjtgfgsehtkeertddtvdej necuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrh esrghklhgrvhgvrhdrtghomheqnecuggftrfgrthhtvghrnhepffelgeeifefgveduhedt hfekuedtffejveegffegjeevtdehgfduieetfeehjeehnecuvehluhhsthgvrhfuihiivg eptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghk lhgrvhgvrhdrtghomhdpnhgspghrtghpthhtohepfedpmhhouggvpehsmhhtphhouhhtpd hrtghpthhtoheplhgruhhrvghniidrrghlsggvsegthigsvghrthgvtgdrrghtpdhrtghp thhtohepmhgrrhgtvghfvghrnhejsehgmhgrihhlrdgtohhmpdhrtghpthhtohepphhgsh hqlhdqghgvnhgvrhgrlheslhhishhtshdrphhoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 24 Feb 2025 11:37:07 -0500 (EST) Message-ID: Date: Mon, 24 Feb 2025 08:37:06 -0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Default Value Retention After Dropping Default To: Laurenz Albe , Marcelo Fernandes , pgsql-general@lists.postgresql.org References: Content-Language: en-US From: Adrian Klaver In-Reply-To: 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 2/24/25 03:50, Laurenz Albe wrote: > 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. >> >> Can anyone explain how PostgreSQL "knows about" the default value that has 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 = 'foo'::regclass > AND attname = 'bar'; > > attmissingval > ═══════════════ > {default} > (1 row) > > That value is used for all rows that don't yet physically have the column. That answers this part of the process: ALTER TABLE foo ADD COLUMN bar varchar(255) NOT NULL DEFAULT 'default'; I believe the OP is asking about this: ALTER TABLE foo ALTER COLUMN bar DROP DEFAULT; Because if after dropping the DEFAULT you do this: INSERT INTO foo (id) SELECT generate_series(1001, 1010); You get: ERROR: null value in column "bar" of relation "foo" violates not-null constraint DETAIL: Failing row contains (1001, null). The DEFAULT is no longer in use, but the values still exist in the previously entered rows: SELECT * from foo order by id desc limit 5; id | bar -------+--------- 10000 | default 9999 | default 9998 | default 9997 | default 9996 | default > > Yours, > Laurenz Albe > -- Adrian Klaver adrian.klaver@aklaver.com