why this query needs more time? Its very slow
Difficult to say for sure - could you provide the output of EXPLAIN ANALYSE rather than just EXPLAIN?
Some other immediate observations:
1. Perhaps don't post to so many mailing lists at once. If you reply to this, maybe reduce it to pgsql-performance?
2. You don't say whether the row estimates are accurate in the EXPLAIN.
3. You seem to be needlessly coalescing personaldetails.masterid since you check for it being null in your WHERE clause
4. Do you really need to cast to numeric and generate a "sorting" column that you then don't ORDER BY?
5. Is ppid an id number? And are you sure it's safe to calculate it like that?
6. What is balance() and how long does it take to calculate its result?
select coalesce(personaldetails.masterid::numeric,personaldetails.id) + (coalesce(personaldetails.id::numeric,0)/1000000) as sorting, floor(coalesce(personaldetails.masterid::numeric,personaldetails.id) + (coalesce(personaldetails.id::numeric,0)/1000000)) as ppid,
balance('MASTER-REGISTRATION',personaldetails.id) as balance,
balance('MASTER-REGISTRATION',pd2.id) as accbalance,
I'm guessing point 6 is actually your problem - try it without the calls to balance() and see what that does to your timings.
---------------------------(end of broadcast)--------------------------- TIP 5: Have you checked our extensive FAQ?