taking long time to create table with foreign tables
query
select distinct a."scid", a."tt", a."p"
into tableA
from db1_foreigntable1 a
join db1_foreigntable2 b on a."scid" = b."obid"
join db1_foreigntable3 c on a."scid" = c."scid"
join db2_foreigntable1 d on c."tr" = d."tr" and d."st" = 'A'
WHERE b."vid" = 'ck98098089';
explain plan
"Unique (cost=1118498.39..1345123636.54 rows=1630056 width=1074)"
" -> Nested Loop (cost=1118498.39..236056972.07 rows=147875555263 width=1074)"
" -> Foreign Scan (cost=1118397.82..1406741.03 rows=2031252 width=57)"
" Relations: ((db1_foreigntable1 a) INNER JOIN (db1_foreigntable2 b)) INNER JOIN (db1_foreigntable3 c)"
" -> Foreign Scan on db2_foreigntable1 d (cost=100.57..114.13 rows=139 width=10)"
explain analyze
"Unique (cost=1118498.39..1345123636.54 rows=1630056 width=1074) (actual time=257.649..765380.721 rows=409229 loops=1)"
" -> Nested Loop (cost=1118498.39..236056972.07 rows=147875555263 width=1074) (actual time=257.647..764798.024 rows=916789 loops=1)"
" -> Foreign Scan (cost=1118397.82..1406741.03 rows=2031252 width=57) (actual time=254.342..100453.211 rows=3033849 loops=1)"
" Relations: ((db1_foreigntable1 a) INNER JOIN (db1_foreigntable2 b)) INNER JOIN (db1_foreigntable3 c)"
" -> Foreign Scan on db2_foreigntable1 d (cost=100.57..114.13 rows=139 width=10) (actual time=0.177..0.177 rows=0 loops=3033849)"
"Planning Time: 26.183 ms"
"Execution Time: 777131.106 ms"
how to mitigate this query I was created FDW with postgres_fdw extension
I was set below parameters for foreign server
use_remote_estimate - true
extensions - fdw_functions
fetch_size - 50000