Question about foreign-key-based join removal in analyzejoins.c

Started by Ahmed Amrabout 13 hours ago1 messages
Jump to latest
#1Ahmed Amr
ahmedamr4f@gmail.com

Hi,

I've been studying the join removal logic in
src/backend/optimizer/plan/analyzejoins.c, particularly
remove_useless_joins() and join_is_removable(), to understand how
PostgreSQL determines when a join can be safely removed.

From the code and comments, it appears that join removal currently
focuses on LEFT JOIN cases where the inner relation can be proven
unique for the join condition (typically via unique indexes) and where
no attributes from the inner side are referenced above the join.

While exploring this area, I noticed that foreign key metadata is
available during planning via root->fkey_list. I searched the
pgsql-hackers archives for discussion of FK-based join removal but may
have missed earlier threads.

In cases such as:

SELECT o.*
FROM orders o
JOIN customers c ON o.customer_id = c.id;

where o.customer_id references customers(id) and no columns from the
referenced table are used, it seems that the FK constraint could provide
the necessary guarantee for eliminating the join.

Has the use of foreign key constraints for join removal (particularly
for INNER JOINs) been considered previously? If so, were there specific
reasons it wasn't pursued?

I'm currently exploring this area as part of preparing a GSoC proposal
and wanted to make sure I understood any prior design considerations
before digging deeper.

Thanks,
Ahmed Amr