fast query
select ...
from table1 t1
join table2 t2 on t2.org_id = t1.org_id
where t1.org_id = 1
slow query
select ...
from table1 t1
join table2 t2 on t2.org_id = t1.org_id
where t1.org_id = (select org_id from table3 where org_name = "abc" limit 1)
The only difference in the two queries is a substitution of a sub query for the literal. I've tried this on PostgreSQL 12.2 and 11.6 on AWS with RDS. table1 and table2 are both partitioned on the org_id column. table3 has a primary key of org_id and a unique index on org_name. "limit 1" was added to the slow query's sub query to try to help the optimizer.
fast query returns in 10 second for most orgs. slow query takes 30 - 100 seconds for most orgs.
I've tried partition sizes of 128, 256, 384, 512, 1024, 2048, and 4096, with 384 being the best.
The fast query's explain analyze plan is 15 lines and correctly uses only 1 partition. The slow query's explain plan is 2,388 lines for 384 partitions and appears to use only 1 partition but it considers all partitions.