`generate_subscripts(...) AS alias` inside a correlated `ARRAY(SELECT ...)` subquery: "column could not be found in any table in scope"
Environment: same as above.
Repro:
SELECT ARRAY(SELECT k + 1 FROM generate_subscripts(ARRAY[10,20,30], 1) AS k ORDER BY k);
Actual:
ERROR: column "k" could not be found in any table in scope
Expected: {11,21,31}, as in real PostgreSQL.
Related secondary symptom — the alias also doesn't bind to a scalar
column outside the ARRAY() wrapper, it seems to bind to the whole row
instead:
SELECT k FROM generate_subscripts(ARRAY[1,2,3], 1) AS k;
-- returns composite rows: (1), (2), (3) — instead of scalar 1, 2, 3
Why it matters: this exact pattern is used by ActiveRecord's
PostgreSQL adapter in its #indexes(table_name) introspection query
(used to discover existing indexes), which runs on the schema-cache-
population path triggered by nearly every UPDATE through Rails.
Result: any model.update/update! call fails the first time it
needs index info — confirmed to also break third-party gems (e.g.
devise-api's token revoke flow), not just application code. Full
failing query captured via PG::Connection#trace:
SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid),
pg_catalog.obj_description(i.oid, 'pg_class') AS comment, d.indisvalid,
ARRAY(
SELECT pg_get_indexdef(d.indexrelid, k + 1, true)
FROM generate_subscripts(d.indkey, 1) AS k
ORDER BY k
) AS columns
FROM pg_class t
INNER JOIN pg_index d ON t.oid = d.indrelid
INNER JOIN pg_class i ON d.indexrelid = i.oid
LEFT JOIN pg_namespace n ON n.oid = t.relnamespace
WHERE i.relkind IN ('i', 'I') AND d.indisprimary = 'f'
AND t.relname = 'my_table' AND n.nspname = ANY (current_schemas(false))
ORDER BY i.relname
@tititoof , thank you for using Doltgres and filing an issue. We'd love to hear about your use case. Feel free to email me or swing by our Discord if you'd like to share.