Original query:

"Hash Semi Join  (cost=118519.37..156494.40 rows=2 width=980) (actual time=5442.173..8101.785 rows=2 loops=1)"
"  Hash Cond: (doks.rowid = doks_1.rowid)"
"  ->  Hash Left Join  (cost=118519.26..155685.54 rows=308083 width=716) (actual time=4220.233..7963.758 rows=308222 loops=1)"
"        Hash Cond: (doks.kursv_id = tab_kurs.rowid)"
"        ->  Hash Left Join  (cost=118404.03..154412.95 rows=308083 width=692) (actual time=4215.563..7424.749 rows=308222 loops=1)"
"              Hash Cond: ((doks.kto)::text = (pracbv.login)::text)"
"              ->  Hash Left Join  (cost=118360.55..150731.62 rows=308083 width=677) (actual time=4213.677..6827.606 rows=308222 loops=1)"
"                    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) (actual time=4134.199..5988.681 rows=308222 loops=1)"
"                          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) (actual time=4053.587..5109.964 rows=308222 loops=1)"
"                                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) (actual time=3.064..15.927 rows=2471 loops=1)"
"                                      Index Cond: ((typ_dok_ref)::text = 'PA'::text)"
"                                ->  Hash  (cost=92562.77..92562.77 rows=308083 width=435) (actual time=4047.750..4047.750 rows=308222 loops=1)"
"                                      Buckets: 16384  Batches: 64  Memory Usage: 854kB"
"                                      ->  Merge Left Join  (cost=1.12..92562.77 rows=308083 width=435) (actual time=0.100..2753.460 rows=308222 loops=1)"
"                                            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)"
"                                            Rows Removed by Join Filter: 3495"
"                                            ->  Merge Left Join  (cost=0.70..59480.31 rows=308083 width=417) (actual time=0.074..1785.569 rows=308130 loops=1)"
"                                                  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) (actual time=0.037..1191.694 rows=308083 loops=1)"
"                                                  ->  Index Scan using doks_fak_kor on doks kor  (cost=0.28..17185.94 rows=308083 width=36) (actual time=0.021..19.488 rows=7247 loops=1)"
"                                            ->  Index Scan using doks_dok_ref on doks pa  (cost=0.41..25285.85 rows=308083 width=40) (actual time=0.018..89.777 rows=58619 loops=1)"
"                          ->  Hash  (cost=842.74..842.74 rows=21674 width=139) (actual time=80.397..80.397 rows=21674 loops=1)"
"                                Buckets: 32768  Batches: 1  Memory Usage: 2649kB"
"                                ->  Seq Scan on kh  (cost=0.00..842.74 rows=21674 width=139) (actual time=0.023..39.399 rows=21674 loops=1)"
"                    ->  Hash  (cost=842.74..842.74 rows=21674 width=92) (actual time=79.268..79.268 rows=21674 loops=1)"
"                          Buckets: 32768  Batches: 1  Memory Usage: 2311kB"
"                          ->  Seq Scan on kh odb  (cost=0.00..842.74 rows=21674 width=92) (actual time=0.022..45.931 rows=21674 loops=1)"
"              ->  Hash  (cost=41.56..41.56 rows=154 width=20) (actual time=1.852..1.852 rows=61 loops=1)"
"                    Buckets: 1024  Batches: 1  Memory Usage: 7kB"
"                    ->  Subquery Scan on pracbv  (cost=37.45..41.56 rows=154 width=20) (actual time=1.334..1.776 rows=96 loops=1)"
"                          ->  Unique  (cost=37.45..40.02 rows=154 width=246) (actual time=1.332..1.713 rows=96 loops=1)"
"                                ->  Sort  (cost=37.45..38.74 rows=513 width=246) (actual time=1.331..1.521 rows=512 loops=1)"
"                                      Sort Key: pracownicy.kod_pracow, pracownicy.tz DESC"
"                                      Sort Method: quicksort  Memory: 53kB"
"                                      ->  Seq Scan on pracownicy  (cost=0.00..14.36 rows=513 width=246) (actual time=0.027..0.508 rows=512 loops=1)"
"                                            Filter: (kod_pracow < 100)"
"                                            Rows Removed by Filter: 77"
"        ->  Hash  (cost=66.77..66.77 rows=3877 width=28) (actual time=4.641..4.641 rows=3877 loops=1)"
"              Buckets: 4096  Batches: 1  Memory Usage: 172kB"
"              ->  Seq Scan on tab_kurs  (cost=0.00..66.77 rows=3877 width=28) (actual time=0.018..2.032 rows=3877 loops=1)"
"  ->  Hash  (cost=0.08..0.08 rows=2 width=4) (actual time=0.058..0.058 rows=2 loops=1)"
"        Buckets: 1024  Batches: 1  Memory Usage: 5kB"
"        ->  Limit  (cost=0.00..0.06 rows=2 width=4) (actual time=0.030..0.032 rows=2 loops=1)"
"              ->  Seq Scan on doks doks_1  (cost=0.00..9596.83 rows=308083 width=4) (actual time=0.027..0.029 rows=2 loops=1)"
"Planning time: 49.679 ms"
"Execution time: 8104.267 ms"