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

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

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

От:
Álvaro Herrera <alvherre@kurilemu.de>
Дата:
Вложения:
On 2026-Sep-08, Álvaro Herrera wrote:

> Yes, but I think the question is in which direction should we fix said
> bug.  My preference is to go for Kyotaro's suggestion: have both REPACK
> and REPACK (CONCURRENTLY) raise an error with an invalid index, asking
> the user to drop it.
> 
> Would anybody oppose that?

Concretely, something like this.  (Hmm, I guess this should be noted in
repack.sgml as well.)

I don't want to touch the behavior of REINDEX, CLUSTER or VACUUM FULL in
pg19 at this stage, much less within the context of an "open item"; we
can discuss that for pg20 afterwards.

-- 
Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/
"Uno puede defenderse de los ataques; contra los elogios se esta indefenso"

unexpected behaviour generating queryid

От:
mailinglists <lists@database.one>
Дата:
Something has changed along the line, possibly already in PG17, in the way query’s are generated that makes it possible for queryid’s to clash. 
The crux of the issue seems to be that tables are now identified by their alias, and only their alias, no longer their old.
This may become especially problematic with the upcoming plan_advice extension, since that relies completely on the queryid.

I prepared a small example that shows a clash. 
I am assuming that the compute_query_id is on auto.
select * from pg_settings where name like '%compute%';
— setup test tables 
drop table if exists test_measurements;
create table test_measurements (
id serial primary key,
name text,
measurement_ts timestamptz default now(),
value float
);
drop table if exists test_users;
create table test_users (
id int,
fullname text,
address text,
creation_moment timestamptz default now()
);
insert into test_measurements (name, measurement_ts, value)
select 'voltage' name,
now() - interval '1 minute'*n measurement_ts,
floor(random()*100) + n value
from generate_series(1,1000) n;

insert into test_users (id, fullname, address, creation_moment)
values (0,'backend', 'Office 69', '2026-01-01 00:00:00+00'::timestamptz)
, (1,'root', 'Office 69', '2026-01-02 10:10:10+00'::timestamptz);

-- testing queries
explain verbose select * from test_measurements a where id IN (66, 77, 88, 99);
explain verbose select * from test_users a where id IN (0, 1);
explain verbose select * from test_measurements xx where id IN (66, 77, 88, 99);
The first two queries give me an identical queryid.
Tested on PG19Beta3

Ellert.

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

От:
Robert Treat <rob@xzilla.net>
Дата:
On Sun, Sep 13, 2026 at 10:35 PM shihao zhong  wrote:
>
> Hi,
>
> The check in 0c5d6269614 tests indisready, so it does not catch an index
> that is ready but not valid.  CREATE INDEX CONCURRENTLY leaves such an
> index when it fails during validation, and for a unique index that means
> the table contains duplicates.
>
>
> I reproduced it: 6M rows, CREATE UNIQUE INDEX CONCURRENTLY, and a
> duplicate inserted while the build was running.  The index ends up with
> indisready = true, indisvalid = false.  On master with 0c5d6269614:
>
>   REPACK u                 -> succeeds after 17s, index stays invalid
>   REPACK (CONCURRENTLY) u  -> fails after 15s:
>       ERROR:  could not create unique index "u_c_uniq_repacknew"
>
> So the inconsistency the commit wanted to remove is still there for this
> case, and the concurrent form fails only after copying the table.
>

The patch looks right to me, and I think I am +1 for this generally,
though I would note that we are widening the scope here such that
indexes that could succeed with a rebuild would now cause an error, so
we're a little less functional though a little more behaviorally
consistent.

> The attached patch checks indisvalid instead.  Both forms then fail
> immediately with the new error.  It also adds the ready-but-invalid case
> to the test.  A validation failure needs a concurrent session, so the
> test sets indisvalid = false in pg_index directly.  If that is too hacky,
> I can drop that part.
>

This does feel hacky, since we're being manipulative rather than
testing a real scenario. I think what we want would be to follow the
lead of src/test/modules/injection_points/sql/reindex_conc.sql?

Robert Treat
https://xzilla.net


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

От:
Álvaro Herrera <alvherre@kurilemu.de>
Дата:
On 2026-Sep-14, Robert Treat wrote:

> The patch looks right to me, and I think I am +1 for this generally,
> though I would note that we are widening the scope here such that
> indexes that could succeed with a rebuild would now cause an error, so
> we're a little less functional though a little more behaviorally
> consistent.

Yeah.  I considered the idea of adding a REPACK option like
"rebuild_invalid_indexes=on" that would attempt to rebuild rather than
failing outright, at the user's risk.  Not sure it's worth the trouble.

I have pushed this change now, without the test.  I did throw in a short
doc update.

One thing I didn't want to say in said doc update, is that while you can
do a REINDEX of the non-validated index prior to REPACK, it's rather a
complete waste of time, because REPACK has to rebuild that index again.
It's probably better to DROP the index, then REPACK, then do CREATE
INDEX CONCURRENTLY.  If we didn't cause REPACK to error out in the
presence of a buildable invalid index, then REPACK could rebuild the
index just fine.  I'm happy to listen to your operationally-experienced
opinion on this.

> This does feel hacky, since we're being manipulative rather than
> testing a real scenario.

Yeah.

> I think what we want would be to follow the lead of
> src/test/modules/injection_points/sql/reindex_conc.sql?

Do you mean creating test invalid indexes by way of using injection
points to interrupt creation in the various phases?  I don't see
reindex_conc.sql doing that.  But also, we run these tests in
test_decoding because it's the test suite that is certain to have
wal_level=logical; but if we wanted to also require injection_points,
the changes in meson.build / Makefile get more involved.  I didn't find
a test suite that has conditional tests depending on injection_points.
src/test/modules/authentication does, but for a TAP test, not a plain
regress or isolation test.  It is surely just a SMOP ...

-- 
Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/
“Cuando no hay humildad las personas se degradan” (A. Christie)


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

От:
Robert Treat <rob@xzilla.net>
Дата:
On Tue, Sep 15, 2026 at 11:30 AM Álvaro Herrera  wrote:
> On 2026-Sep-14, Robert Treat wrote:
>
> > The patch looks right to me, and I think I am +1 for this generally,
> > though I would note that we are widening the scope here such that
> > indexes that could succeed with a rebuild would now cause an error, so
> > we're a little less functional though a little more behaviorally
> > consistent.
>
> Yeah.  I considered the idea of adding a REPACK option like
> "rebuild_invalid_indexes=on" that would attempt to rebuild rather than
> failing outright, at the user's risk.  Not sure it's worth the trouble.
>

Yeah, I started thinking if there were some way to handle this better,
but it quickly gets in to late stage design work that is probably
better left to v20 and some user feedback.

> I have pushed this change now, without the test.  I did throw in a short
> doc update.
>
> One thing I didn't want to say in said doc update, is that while you can
> do a REINDEX of the non-validated index prior to REPACK, it's rather a
> complete waste of time, because REPACK has to rebuild that index again.
> It's probably better to DROP the index, then REPACK, then do CREATE
> INDEX CONCURRENTLY.  If we didn't cause REPACK to error out in the
> presence of a buildable invalid index, then REPACK could rebuild the
> index just fine.  I'm happy to listen to your operationally-experienced
> opinion on this.
>

I'd agree with you that in most cases, the double rebuild isn't going
to be worth it. I mean maybe you want to fix a broken unique index so
that it is enforced before (and thus during) a long REPACKCC; but that
feels pretty contrived since we are talking about leftover broken
indexes anyway.

> > This does feel hacky, since we're being manipulative rather than
> > testing a real scenario.
>
> Yeah.
>
> > I think what we want would be to follow the lead of
> > src/test/modules/injection_points/sql/reindex_conc.sql?
>
> Do you mean creating test invalid indexes by way of using injection
> points to interrupt creation in the various phases?  I don't see
> reindex_conc.sql doing that.  But also, we run these tests in
> test_decoding because it's the test suite that is certain to have
> wal_level=logical; but if we wanted to also require injection_points,
> the changes in meson.build / Makefile get more involved.  I didn't find
> a test suite that has conditional tests depending on injection_points.
> src/test/modules/authentication does, but for a TAP test, not a plain
> regress or isolation test.  It is surely just a SMOP ...
>

Ah, yeah, I'm less sure its worth it given the above, but I was
thinking something like this:

CREATE EXTENSION injection_points;
SELECT injection_points_set_local();
SELECT injection_points_attach('define-index-before-set-valid', 'error');

CREATE TABLE x (a int PRIMARY KEY, b int);
INSERT INTO x VALUES (1,1),(2,2);
CREATE INDEX CONCURRENTLY x_b ON x (b);

SELECT injection_points_detach('define-index-before-set-valid');

Robert Treat
https://xzilla.net


FAQ