Re: To-Do item: skip table scan for adding column with provable check constraints

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Jeff Janes <jeff(dot)janes(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: To-Do item: skip table scan for adding column with provable check constraints
Date: 2016-05-24 21:28:27
Message-ID: CA+TgmoabjXmYzJvxYjyLvxFfxFsHkucqBRyNqNJ=5oVt7CNMHA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, May 19, 2016 at 7:33 PM, Jeff Janes <jeff(dot)janes(at)gmail(dot)com> wrote:
> I recently had to run something like:
>
> alter table pgbench_accounts add locked text check (locked != 'unlocked');
>
> And was surprised that it took several minutes to complete as it
> scanned the whole table.
>
> The new column is going to start out as NULL in every row, so there is
> no need to validate the check constraint by reading the table as it
> can be proven from first principles. Correct?

Right. If there were a DEFAULT on the new column that would of course
be different, and you can also do thinks like CHECK (a != b) here.
However, if the CHECK constraint does not reference any column other
than the newly-added one, and if the new column will have the same
value for every row either because there is no default or because the
default is a constant, then we can test the CHECK constraint just once
against the value that all new rows will have instead of testing it
once per row.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2016-05-24 21:40:03 Re: Allow COPY to use parameters
Previous Message Alvaro Herrera 2016-05-24 21:28:05 Re: [PROPOSAL] Move all am-related reloption code into src/backend/access/[am-name] and get rid of relopt_kind