We are running the following select statement on postgres 10 and 11 and notice unexpected behaviour while processing the subquery
See example:
Correct query
--> subquery column name exists in subquery
select * from information_schema."tables" t
where t.table_schema in (select schemaname from pg_tables t2);
Error no column schemaname2
--> subquery column name does not exists in both tables
select * from information_schema."tables" t
where t.table_schema in (select schemaname2 from pg_tables t2);
NO Error although the column table_schema does not exist in pg_tables
--> subquery column name does not in exists in subquery but in main query -> all rows from the main query are selected
select * from information_schema."tables" t
where t.table_schema in (select table_schema from pg_tables t2);
Error due to the use of table alias
--> our proposed workaround -> always use aliases then postgres is forced to validate column name
select * from information_schema."tables" t
where t.table_schema in (select t2.table_schema from pg_tables t2);
Special thx to https://stackoverflow.com/users/8579285/oved-s for verification and constant support