BUG #18637: CREATE INDEX won't look up operator classes in search_path if PARTITION BY is specified

The Post Office <noreply@postgresql.org>

From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: usamoi@outlook.com
Date: 2024-09-27T14:31:54Z
Lists: pgsql-bugs
The following bug has been logged on the website:

Bug reference:      18637
Logged by:          CREATE INDEX won&#x27;t look up operator classes in search_path if PARTITION BY
is specified
Email address:      usamoi@outlook.com
PostgreSQL version: 17.0
Operating system:   Linux 6.10
Description:        

This SQL works in PostgreSQL 16.4 but not in PostgreSQL 17.0. Here
`vector_l2_ops` is an operator class in schema `vectors`.
```
SET search_path TO public, vectors;
CREATE TABLE items (val vector(3), category_id int) PARTITION BY
LIST(category_id);
CREATE TABLE id_123 PARTITION OF items FOR VALUES IN (1, 2, 3);
CREATE TABLE id_456 PARTITION OF items FOR VALUES IN (4, 5, 6);
CREATE TABLE id_789 PARTITION OF items FOR VALUES IN (7, 8, 9);
CREATE INDEX ON items USING vectors (val vector_l2_ops);
```
The error message is: `ERROR:  operator class "vector_l2_ops" does not exist
for access method "vectors"`

If I do not use PARTITION BY and CREATE TABLE OF, it works and the message
is `CREATE INDEX`.

After debugging, I find that `DefineIndex` would call `RestrictSearchPath`
in `indexcmds.c:1234` and `indexcmds.c:1334` before entering recursion, so
that the nested `DefineIndex` would be only able to look up operator classes
in `pg_catalog, pg_temp`.

Since the behavior between `CREATE INDEX` and `CREATE INDEX PARTITION BY` is
different, it should be a bug.

Commits

  1. Use generateClonedIndexStmt to propagate CREATE INDEX to partitions.