Re: Optimization of partial index creation for a new column

From: Andres Freund <andres(at)anarazel(dot)de>
To: Александр Асафов <asafofalexander(at)gmail(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Optimization of partial index creation for a new column
Date: 2025-12-14 21:35:10
Message-ID: s2iowdadng2jwstlpopzxaax6ngxnihdl3w3jdkcsiosvtoygh@737lx7vby2gp
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On 2025-12-14 23:13:49 +0300, Александр Асафов wrote:
> I have a feature suggestion to optimize the creation of a partial
> index. In some cases, it is possible to skip a full table scan. For
> example:
>
> BEGIN;
> ALTER TABLE test ADD COLUMN (newcol int);
> CREATE INDEX newindex ON test(newcol) WHERE newcol IS NOT NULL;
> COMMIT;
>
> In this case, the values of all columns in the new table will be NULL,
> and there is no point in checking the conditions for each row. The
> same optimization can be done for default values in NOT NULL columns.
>
> How complex would a patch with this optimization be? Will it be
> necessary to add any attributes to the table metadata, or can we check
> that the previous command was CREATE TABLE when creating an index?

I doubt this is feasible, or at least that it's going to be worth the
complexity. You'd have to make sure that the transaction didn't actually
insert any rows, and update some in-memory or catalog state if it did. With
subtransactions etc that could get somewhat complicated.

Greetings,

Andres Freund

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2025-12-14 21:38:59 Re: Proposed mid-cycle update of typedefs.list
Previous Message Andres Freund 2025-12-14 21:22:32 Re: Proposed mid-cycle update of typedefs.list