psql: Don't offer partitioned tables after CREATE INDEX CONCURRENTLY

Started by Ishjaap Singh21 days ago4 messageshackers

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1Ishjaap Singh
ishjaap.singh07@gmail.com

Hi hackers,

Tab completion after CREATE INDEX CONCURRENTLY [name] ON offers the
same relations as plain CREATE INDEX, including partitioned tables.
CREATE INDEX CONCURRENTLY isn't supported on partitioned tables, so
picking one always fails:

CREATE INDEX CONCURRENTLY ON parted (a);
ERROR: cannot create index on partitioned table "parted" concurrently

This happens on all supported branches.

The attached patch adds a relation list without partitioned tables and
uses it after CONCURRENTLY. Plain tables, materialized views and leaf
partitions are still offered, and plain CREATE INDEX is unchanged.

One detail in the rule change: in psql 18 and later, when a matching
completion rule finds nothing, psql moves on to the next matching one.
So with only the new list, "CREATE INDEX CONCURRENTLY ON <prefix of a
partitioned table>" still ended up in the plain CREATE INDEX rule,
which read CONCURRENTLY as the index name. The same happened with an
index actually named "index". The plain rule now needs CREATE or
UNIQUE right before INDEX and doesn't take CONCURRENTLY as the index
name, so a concurrent command can't reach it.

The new cases in 010_tab_completion.pl check that partitioned tables
are left out after CONCURRENTLY (with and without an index name, with
UNIQUE, and with an index named "index"), and that plain CREATE INDEX
still offers them. They fail without the fix. All psql TAP tests pass
on master and REL_19_STABLE, on Linux with GNU readline (I haven't
tried libedit), and pgindent is clean.

The patch is against master and also applies to REL_19_STABLE and
REL_18_STABLE. REL_15 to REL_17 only need the file name changed to
tab-complete.c. REL_14 needs two small changes (quote_ident() in the
query and the older two-argument macro). I have that version and have
tested it on REL_14_STABLE, and can post it if this should go back
that far.

This is the same kind of fix as the recent ones for DROP INDEX
CONCURRENTLY (f2edf5dd00d) and REFRESH MATERIALIZED VIEW CONCURRENTLY
(54f36adcbcd). There's also a long-running thread about allowing
CREATE INDEX CONCURRENTLY on partitioned tables [1]/messages/by-id/6472861e485554974ff40375e6d4b81a@postgrespro.ru. If that goes in,
this completion would need to depend on the server version. I could not
find a posted fix for this.

[1]: /messages/by-id/6472861e485554974ff40375e6d4b81a@postgrespro.ru

Thanks,
Ishjaap Singh

Attachments:

t254619_1
v1-0001-psql-Don-t-offer-partitioned-tables-after-CREATE-.patchapplication/octet-stream; name=v1-0001-psql-Don-t-offer-partitioned-tables-after-CREATE-.patchDownload+62-4
#2Sho Ito
i.sho.0628@gmail.com
In reply to: Ishjaap Singh (#1)
Re: psql: Don't offer partitioned tables after CREATE INDEX CONCURRENTLY

Hi Ishjaap,

I reviewed v1 on master with assertions enabled. All psql TAP tests pass.

Additional checks confirm that materialized views and leaf partitions remain
offered, including with schema-qualified names, and that plain `CREATE
INDEX`
still offers partitioned tables.

The new completion list aligns with the backend behaviour: leaf partitions
support `CREATE INDEX CONCURRENTLY`, while `DefineIndex()` rejects it for
partitioned parents [1]https://github.com/postgres/postgres/blob/0607aa61090ac1b574f8bafe854c684195b31e16/src/backend/commands/indexcmds.c#L740-L753.

As you noted, if the backend patch allowing `CREATE INDEX CONCURRENTLY` on
partitioned tables is committed, this completion filter will need
revisiting.

I found no issues with the patch. Looks good to me.

[1]: https://github.com/postgres/postgres/blob/0607aa61090ac1b574f8bafe854c684195b31e16/src/backend/commands/indexcmds.c#L740-L753
https://github.com/postgres/postgres/blob/0607aa61090ac1b574f8bafe854c684195b31e16/src/backend/commands/indexcmds.c#L740-L753

Thanks,
Sho

--
=======================================
*Sho Ito (Sean)*

E-mail: i.sho.0628@gmail.com
LinkedIn: *https://www.linkedin.com/in/sho-ito-0628/
<https://www.linkedin.com/in/sho-ito-0628/>*
GitHub: https://github.com/sean0628/
=======================================

#3Alvaro Herrera
alvherre@kurilemu.de
In reply to: Sho Ito (#2)
Re: psql: Don't offer partitioned tables after CREATE INDEX CONCURRENTLY

Hi

There's more to fix here. I think the rule to complete using
Query_for_list_of_concurrent_indexables after "CONCURRENTLY ON" is
obsolete, and worse, it is bogus. First, it is obsolete because if you
have a REPACK command, it will complete nonsense:

REPACK (CONCURRENTLY ON <tab>
shows a list of tables/schemas. Useless!

Second, bogus; the claim is that you need to use TailMatches() because
the CREATE INDEX command can appear as part of a CREATE SCHEMA. But
that doesn't actually work:

=# create schema foo create table bar (a int) create index concurrently on bar (a);
ERROR: CREATE INDEX CONCURRENTLY cannot be executed from a function or procedure

so trying to provide completions after
TailMatches(CONCURRENTLY MatchAny ON) or TailMatches(CONCURRENTLY ON)
is something that we need to remove, not patch.

Thanks, regards,

--
Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/
"Debido a que la velocidad de la luz es mucho mayor que la del sonido,
algunas personas nos parecen brillantes un minuto antes
de escuchar las pelotudeces que dicen." (Roberto Fontanarrosa)

#4Ishjaap Singh
ishjaap.singh07@gmail.com
In reply to: Alvaro Herrera (#3)
Re: psql: Don't offer partitioned tables after CREATE INDEX CONCURRENTLY

Hi Álvaro,

Here's v2.

I removed the TailMatches(CONCURRENTLY MatchAny ON) and
TailMatches(CONCURRENTLY ON) rules, and the same CONCURRENTLY forms in
the rules after the table name. CREATE [UNIQUE] INDEX CONCURRENTLY
[name] ON now completes only as a whole command, using Matches(), still
without partitioned tables.

The plain CREATE INDEX rule keeps TailMatches(), but now skips
CONCURRENTLY. Otherwise, when nothing in the CONCURRENTLY list matches
what's typed, completion falls through to TailMatches("INDEX",
MatchAny, "ON"), which takes CONCURRENTLY as the index name and offers
the partitioned tables anyway.

If you'd rather not complete after CREATE INDEX CONCURRENTLY ON at all,
I can drop the Matches() rule and the new query.

Regards,
Ishjaap Singh

Attachments:

t254619_4
v2-0001-psql-Don-t-offer-partitioned-tables-after-CREATE-.patchapplication/octet-stream; name=v2-0001-psql-Don-t-offer-partitioned-tables-after-CREATE-.patchDownload+118-8