sql: Potential regression in v19.2.0: pq: validate fk constraint: requested column created not in index
A user is running 19.2 and is attempting to add some Foreign Key constraints to a few tables, and is receiving the error pq: validate fk constraint: requested column created not in index. Attempting to run the same SQL queries in versions 19.1.4 and 19.1.5 result in successful completion of the ALTER TABLE queries.
To Reproduce
Running the script located here in 19.2 will reproduce the issue, with the following output:
CREATE TABLE
Time: 3.66ms
CREATE TABLE
Time: 6.811ms
CREATE TABLE
Time: 10.362ms
CREATE TABLE
Time: 6.448ms
CREATE TABLE
Time: 6.178ms
CREATE TABLE
Time: 8.711ms
CREATE INDEX
Time: 97.301ms
CREATE INDEX
Time: 113.328ms
CREATE INDEX
Time: 91.296ms
CREATE INDEX
Time: 111.441ms
ALTER TABLE
Time: 89.507ms
CREATE INDEX
Time: 99.321ms
CREATE INDEX
Time: 115.588ms
CREATE INDEX
Time: 92.631ms
pq: validate fk constraint: requested column created not in index
pq: validate fk constraint: requested column created not in index
ALTER TABLE
Time: 145.661ms
pq: validate fk constraint: requested column created not in index
ALTER TABLE
Time: 140.999ms
ALTER TABLE
Time: 88.382ms
Please find additional notes from the user from the ZenDesk ticket who investigated and had the following findings: " the problem seems to be coming from interleaved_reader_joiner.go - line 419: https://github.com/cockroachdb/cockroach/blob/905068e117f2fd8913a784edf685f09c11f54a09/pkg/sql/rowexec/interleaved_reader_joiner.go#L414-L425
Which causes fetcher.go to error on line 390: https://github.com/cockroachdb/cockroach/blob/809e4c2fa0e6d59c1e8fe05f7cca1aedd56fb230/pkg/sql/row/fetcher.go#L390-L396
Since the code in interleaved_reader_joiner indicates that it is a SecondaryIndex but proceeds to specify all the columns in neededCols."
Zendesk ticket #4104 has been linked to this issue.
@otan could you take a look at this, since you're about to embark on foreign key stuff?
To start, I would add instrumentation to the error that's getting returned so you can see the whole stack. Having that will help a lot.
It looks like https://github.com/cockroachdb/cockroach/commit/88d5fd1dd56b92647e5605508da0cc7ada8514de caused the regression. Digging further!
smallest repro:
create table parent ( a string, b uuid, extra string, primary key (a, b));
create table child ( a string, b uuid, c uuid, extra string, primary key (a,b,c) ) interleave in parent "parent" (a, b);
select parent.a, parent.b, child.c from child left outer join parent on (parent.a=child.a and parent.b = child.b); -- this is fine
create index idx_parent_child on child(a,b) interleave in parent parent(a,b);
select parent.a, parent.b, child.c from child left outer join parent on (parent.a=child.a and parent.b = child.b); -- this doesn't work `pq: requested column extra not in index`
select * from child left outer join parent on (parent.a=child.a and parent.b = child.b); -- this works fine
Note the query above FAILS with at 1a6f5fa5c3 (which is 88d5fd1), but the ALTER INDEX succeeds in 1a6f5fa5c3. so it looks like 88d5fd1 was hiding a regression underneath
Before 88d5fd1, the FORCE INDEX made the check use the PRIMARY KEY index instead of the INTERLEAVING index which avoided the above bug, but 88d5fd1 removed the FORCE INDEX henceforth causing this bug underneath to be revealed as the optimiser prefers the INTERLEAVING index.