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 1oLuiX-000587-7x for pgsql-general@arkaria.postgresql.org; Wed, 10 Aug 2022 23:02:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oLuiU-0001BP-QB for pgsql-general@arkaria.postgresql.org; Wed, 10 Aug 2022 23:02:22 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oLuiU-0001BA-Aw for pgsql-general@lists.postgresql.org; Wed, 10 Aug 2022 23:02:22 +0000 Received: from mail.webkr.de ([78.47.173.34]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oLuiQ-000663-MG for pgsql-general@lists.postgresql.org; Wed, 10 Aug 2022 23:02:21 +0000 Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 0EE181B154C for ; Thu, 11 Aug 2022 01:02:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=webkr.de; s=dkim; t=1660172535; h=from:subject:date:message-id:to:mime-version:content-type: content-transfer-encoding; bh=cUYSqhpDTMs8CCyDRQU5voOlsDqbvMrsfbgudHb/f2k=; b=WGUBmPr8MUoHbrP145uj2wiOyZrvIJRl+hk5f6YP4464gJ75XzHVLSiS8i2ADQouN7xkDL VV4/dnQ1ZnYpX6+/zpjLETl+Ibkg8InrBVAOeKIibcgQ6Dg+vkHgFA0j60EYiM3s2LmmzL EWa+zpoGmj9MkJ7ASCE6v7gNTa0gdy0= From: =?iso-8859-1?Q?Andr=E9_H=E4nsel?= To: Subject: Why is DEFAULT much faster than UPDATE? Date: Thu, 11 Aug 2022 01:02:13 +0200 Message-ID: <0d4101d8ad0d$3af0cf70$b0d26e50$@webkr.de> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Language: de Thread-Index: AditDH+3v05S9LhIQ56V9HUfqsUSag== X-Last-TLS-Session-Version: TLSv1 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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=EFve approach would be: ALTER TABLE foo ADD COLUMN slow bool NOT NULL DEFAULT true; UPDATE foo SET slow =3D 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? Fiddle: https://dbfiddle.uk/?rdbms=3Dpostgres_14&fiddle=3D56595e8ee397a5bc48b8427= 7da3133 a9