pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: André Hänsel <andre@webkr.de>
To: pgsql-general@lists.postgresql.org
Subject: Why is DEFAULT much faster than UPDATE?
Date: Thu, 11 Aug 2022 01:02:13 +0200
Message-ID: <0d4101d8ad0d$3af0cf70$b0d26e50$@webkr.de> (raw)

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?

Fiddle:
https://dbfiddle.uk/?rdbms=postgres_14&fiddle=56595e8ee397a5bc48b84277da3133
a9






view thread (2+ messages)  latest in thread

Message-ID: <0d4101d8ad0d$3af0cf70$b0d26e50$@webkr.de>
Permalink:  ../0d4101d8ad0d$3af0cf70$b0d26e50$@webkr.de/
Also on:    postgresql.org/message-id/0d4101d8ad0d$3af0cf70$b0d26e50$@webkr.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: andre@webkr.de
  Subject: Re: Why is DEFAULT much faster than UPDATE?
  In-Reply-To: <0d4101d8ad0d$3af0cf70$b0d26e50$@webkr.de>
  References: <0d4101d8ad0d$3af0cf70$b0d26e50$@webkr.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox