Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oLurC-0005U1-Bp for pgsql-general@arkaria.postgresql.org; Wed, 10 Aug 2022 23:11:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oLurB-0005IC-6h for pgsql-general@arkaria.postgresql.org; Wed, 10 Aug 2022 23:11:21 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oLurA-0005Ho-2u for pgsql-general@lists.postgresql.org; Wed, 10 Aug 2022 23:11:20 +0000 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oLur6-00049B-GK for pgsql-general@lists.postgresql.org; Wed, 10 Aug 2022 23:11:19 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 69293320015C; Wed, 10 Aug 2022 19:11:13 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Wed, 10 Aug 2022 19:11:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1660173072; x= 1660259472; bh=FHuq83JEGCt8gMwJxNRO44NEYYAWj588C8fqw75nOdM=; b=p SoM59R9e6Kt/3P5uXvBHeI0v3QcC6IB3AqTxznSb9spCyJXS6OyPrBdgIfKG5bDG C+y9GSBU2SptTqbf/l+UvxWv5/5tYBxCdYJ4FS1/gprl+Nzxv1kdXU9L9WX1sxJv PNmF55XOMYreGQ7ew+mfWvzFtoZDoVrETJUBNv/La2Iu2iJES6JgSxq40IHjuZba YEwZ/FuKlICmN45BSeVW2UrnAU+tW6YD8rNV1pTwevOglTh6zNCETaoYKwuJrsp4 T9h3ZAcdtSWN2WwO1zyeC89fDLE2vYJHRnLLLTB1Bw53ldLQBdH1d7R110wSx9/1 PEyUiUkVgrbRkIchsYqjg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:date:feedback-id:feedback-id:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1660173072; x=1660259472; bh=F Huq83JEGCt8gMwJxNRO44NEYYAWj588C8fqw75nOdM=; b=dhkvBOGwLRh2Xc4BX sigBVVIo0MqiHCl567bcO3dKA21ScIoaLIMkkGby3sGl0/C9j3BwOm94M6rLhkid rwfXK0wg9US+eKuYKLcnZ4OorUELabp4JKh/TgQi0jwl97YnfV6M8JdwodxkKBHR qCD3sz6z1aRE4U+o9mnUZXlXxVnTdUewQqVwWtt68sAxvTCquBJGEiDJT4211yJ3 Vdq3POJQ59kY0jTDLXRqClBTeC+MryfqyfuyPy08tHBfFGNEGPLaxhExqSQAOtjU hjsWAFuVqRf68zNQcVwwQI2ILOaFGZrdrGBCOmjiS3FbJxvtyYTVlNXV5s4oxMOg glxVg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrvdegfedgudelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvfhfhjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpeeghefhvefhleekgeegfeelgeeklefhtdelteevveej leevvddujeevueeigfdujeenucffohhmrghinhepphhoshhtghhrvghsqhhlrdhorhhgpd gusghfihguughlvgdruhhknecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehm rghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 10 Aug 2022 19:11:12 -0400 (EDT) Message-ID: Date: Wed, 10 Aug 2022 16:11:11 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Subject: Re: Why is DEFAULT much faster than UPDATE? Content-Language: en-US To: =?UTF-8?B?QW5kcsOpIEjDpG5zZWw=?= , pgsql-general@lists.postgresql.org References: <0d4101d8ad0d$3af0cf70$b0d26e50$@webkr.de> From: Adrian Klaver In-Reply-To: <0d4101d8ad0d$3af0cf70$b0d26e50$@webkr.de> 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 8/10/22 16:02, André Hänsel wrote: > This question is out of curiosity, just to learn more about the internals of > PostgreSQL. > > The goal was to add a not null bool column filled with "false", but with > "true" as the default for new rows. > > The naïve approach would be: > ALTER TABLE foo ADD COLUMN slow bool NOT NULL DEFAULT true; > UPDATE foo SET slow = false; > > This takes a certain, non-negligible amount of time. > > This on the other hand achieves the same result and is almost instant: > ALTER TABLE foo ADD COLUMN fast bool NOT NULL DEFAULT false; > ALTER TABLE foo ALTER COLUMN fast SET DEFAULT true; > > Where does the difference come from, how are those handled internally? From here: https://www.postgresql.org/docs/current/sql-altertable.html "When a column is added with ADD COLUMN and a non-volatile DEFAULT is specified, the default is evaluated at the time of the statement and the result stored in the table's metadata. That value will be used for the column for all existing rows. If no DEFAULT is specified, NULL is used. In neither case is a rewrite of the table required." > > Fiddle: > https://dbfiddle.uk/?rdbms=postgres_14&fiddle=56595e8ee397a5bc48b84277da3133 > a9 > > > -- Adrian Klaver adrian.klaver@aklaver.com