Check explain plan, change work mem to 100MBs and then check explain planagain. If it changed, then try explain analyze.
Work mem is limit is used per node in the plan, so especially withpartitioned tables, that limit is way too high.