Re: REPACK (CONCURRENTLY) doesn't handle invalid indexes

Поиск
Список
Период
Сортировка
Искать

Re: REPACK (CONCURRENTLY) doesn't handle invalid indexes

От:
Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Дата:
Hello,

At Thu, 13 Aug 2026 22:37:25 +0100, Zsolt Parragi  wrote in 
> > I'll create a patch for that approach, but it is a bit more complex than v1.
> 
> It turned out not to be that complex, attached v2.
> 
> One thing to point out is that this is a slight behavior change for
> REINDEX TABLE. We could avoid that, but the current behavior doesn't
> seem to be documented anywhere, and this version seems more consistent
> to me.

I think Alvaro's point about whether invalid indexes should be rebuilt
in the first place is worth considering further. In fact, I wonder
whether REPACK should accept a relation containing an invalid index at
all.

I understand that an invalid index with indisready set cannot simply
be ignored by normal DML. As I understand it, indisready represents an
intermediate state in a concurrent index build, allowing INSERT/UPDATE
maintenance to start before the index becomes available for
queries. Therefore, if a concurrent index build fails at that stage,
an index with indisready = true and indisvalid = false can be left
behind.

However, I think there is a distinction between DML having to continue
maintaining such an index according to indisready and a later,
unrelated DDL command rebuilding it as if it were a normal index.

An invalid index left behind by a failed concurrent index build cannot
be used for queries, and is normally either dropped or explicitly
rebuilt with REINDEX. The fact that indisready is true does not mean
that the index is valid for normal use; it can simply mean that
maintenance had already been enabled as part of the concurrent
operation before it failed.

For that reason, I am somewhat uncomfortable with REPACK implicitly
rebuilding such an index. REPACK is not a command for repairing
indexes, so wouldn't it be more natural to reject the operation if an
invalid index exists and require the user to DROP or REINDEX it first,
rather than trying to reproduce or repair that state as part of
REPACK? 

I understand that non-concurrent REPACK and VACUUM FULL currently
rebuild invalid indexes. However, that seems to be a consequence of
rebuilding all indexes as part of the heap rewrite, and I am not sure
that this behavior should necessarily define the semantics for REPACK
CONCURRENTLY. In fact, Alvaro's point also makes me wonder about the
existing behavior itself. I wonder whether non-concurrent REPACK or
VACUUM FULL should implicitly rebuild such an index as a side effect
of an operation with a different purpose.

So rather than deciding which invalid indexes to rebuild based on
indisready, as in v2, perhaps we should first decide whether a
relation containing an invalid index should be considered a valid
input for REPACK at all.

Regards,

-- 
Kyotaro Horiguchi
NTT Open Source Software Center


Re: REPACK (CONCURRENTLY) doesn't handle invalid indexes

От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
> I think Alvaro's point about whether invalid indexes should be rebuilt
> in the first place is worth considering further. In fact, I wonder
> whether REPACK should accept a relation containing an invalid index at
> all.

Not rebuilding it / emptying it isn't really an option, as I showed an
example in my earlier emails, not rebuilding it results in bogus
checks and statements failing with file read errors, if we empty it it
results in additional constraint violations.

Not allowing these commands (consistently) to work on tables with
invalid indexes is an option, but then that should be consistent
across all similar commands, and it will be a behavior change for
normal vacuum too.

Actually after I looked at this again after Nathan's email yesterday,
I realized that even v2 causes a regression (or lets call it a
behavior change at least), most likely v1 is a better solution.

Consider the following scenario:

CREATE TABLE orders (id int PRIMARY KEY, price int);
INSERT INTO orders VALUES (1, 10), (2, 0), (3, 20);
-- currently fails with division by zero
CREATE INDEX CONCURRENTLY orders_margin ON orders ((100/price));
-- removing bad data
DELETE FROM orders WHERE price = 0;
-- repairs the index
VACUUM FULL orders;

or another less visible example is REFRESH MATERIALIZED VIEW:

CREATE MATERIALIZED VIEW mv AS SELECT * FROM src;
REFRESH MATERIALIZED VIEW mv; -- let's say this is a daily/hourly cron
job or something like that
CREATE INDEX CONCURRENTLY mv_margin ON mv ((100/price));  -- fails
DELETE FROM src WHERE price = 0;
REFRESH MATERIALIZED VIEW mv; -- index now works on master/v1, remains
invalid in v2

And if I follow your suggestion consistently across all commands about
treating it as an invalid input the last command should fail in both
scenarios.

v1 seems to be a better/less risky version to me, especially for 19.


FAQ