Original query:

"Hash Semi Join  (cost=118519.37..156494.40 rows=2 width=980)"
"  Hash Cond: (doks.rowid = doks_1.rowid)"
"  ->  Hash Left Join  (cost=118519.26..155685.54 rows=308083 width=716)"
"        Hash Cond: (doks.kursv_id = tab_kurs.rowid)"
"        ->  Hash Left Join  (cost=118404.03..154412.95 rows=308083 width=692)"
"              Hash Cond: ((doks.kto)::text = (pracbv.login)::text)"
"              ->  Hash Left Join  (cost=118360.55..150731.62 rows=308083 width=677)"
"                    Hash Cond: (((doks.kart_plat)::text = (odb.kod_kart)::text) AND (doks.odbiorca = odb.kod))"
"                    ->  Hash Left Join  (cost=117192.70..144215.89 rows=308083 width=585)"
"                          Hash Cond: ((doks.platnik = kh.kod) AND ((doks.kart_plat)::text = (kh.kod_kart)::text))"
"                          ->  Hash Right Join  (cost=116024.85..137156.11 rows=308083 width=446)"
"                                Hash Cond: ((tax.oddz = doks.oddz) AND (tax.dzial = doks.dzial) AND ((tax.mag)::text = (doks.mag)::text) AND ((tax.nr_dok_ref)::text = (doks.nr_dok)::text))"
"                                ->  Index Scan using doks_dok_ref on doks tax  (cost=0.41..3165.12 rows=453 width=30)"
"                                      Index Cond: ((typ_dok_ref)::text = 'PA'::text)"
"                                ->  Hash  (cost=92562.77..92562.77 rows=308083 width=435)"
"                                      ->  Merge Left Join  (cost=1.12..92562.77 rows=308083 width=435)"
"                                            Merge Cond: ((doks.oddz = pa.oddz) AND (doks.dzial = pa.dzial) AND ((doks.mag)::text = (pa.mag)::text) AND ((doks.typ_dok)::text = (pa.typ_dok_ref)::text) AND ((doks.nr_dok)::text = (pa.nr_dok_ref)::text))"
"                                            Join Filter: ((pa.typ_dok)::text = CASE WHEN ((doks.typ_dok)::text = 'FV'::text) THEN 'PA'::text ELSE 'PAEU'::text END)"
"                                            ->  Merge Left Join  (cost=0.70..59480.31 rows=308083 width=417)"
"                                                  Merge Cond: ((doks.oddz = kor.oddz) AND (doks.dzial = kor.dzial) AND ((doks.mag)::text = (kor.mag)::text) AND ((doks.typ_dok)::text = (kor.typ_fak_kor)::text) AND ((doks.nr_dok)::text = (kor.nr_fak_kor)::te (...)"
"                                                  ->  Index Scan using pk_doks on doks  (cost=0.42..34591.73 rows=308083 width=403)"
"                                                  ->  Index Scan using doks_fak_kor on doks kor  (cost=0.28..17185.94 rows=308083 width=36)"
"                                            ->  Index Scan using doks_dok_ref on doks pa  (cost=0.41..25285.85 rows=308083 width=40)"
"                          ->  Hash  (cost=842.74..842.74 rows=21674 width=139)"
"                                ->  Seq Scan on kh  (cost=0.00..842.74 rows=21674 width=139)"
"                    ->  Hash  (cost=842.74..842.74 rows=21674 width=92)"
"                          ->  Seq Scan on kh odb  (cost=0.00..842.74 rows=21674 width=92)"
"              ->  Hash  (cost=41.56..41.56 rows=154 width=20)"
"                    ->  Subquery Scan on pracbv  (cost=37.45..41.56 rows=154 width=20)"
"                          ->  Unique  (cost=37.45..40.02 rows=154 width=246)"
"                                ->  Sort  (cost=37.45..38.74 rows=513 width=246)"
"                                      Sort Key: pracownicy.kod_pracow, pracownicy.tz DESC"
"                                      ->  Seq Scan on pracownicy  (cost=0.00..14.36 rows=513 width=246)"
"                                            Filter: (kod_pracow < 100)"
"        ->  Hash  (cost=66.77..66.77 rows=3877 width=28)"
"              ->  Seq Scan on tab_kurs  (cost=0.00..66.77 rows=3877 width=28)"
"  ->  Hash  (cost=0.08..0.08 rows=2 width=4)"
"        ->  Limit  (cost=0.00..0.06 rows=2 width=4)"
"              ->  Seq Scan on doks doks_1  (cost=0.00..9596.83 rows=308083 width=4)"