Re: Uninterruptible long planning of a query with too many WHERE clauses

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alexander Kuzmenkov <a(dot)kuzmenkov(at)postgrespro(dot)ru>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Uninterruptible long planning of a query with too many WHERE clauses
Date: 2018-11-11 04:38:13
Message-ID: 21010.1541911093@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Alexander Kuzmenkov <a(dot)kuzmenkov(at)postgrespro(dot)ru> writes:
> Recently one of our customers encountered a situation when the planning
> of a particular query takes too long (several minutes) and can't be
> interrupted by pg_terminate_backend(). The query and schema are attached
> (this is generated by Zabbix).

Ugh. I hope they aren't expecting actually *good* performance on this
sort of query. Still, O(N^2) behavior isn't nice.

When I first saw your post, I thought maybe the problem was an
unreasonable number of paths, but actually there are only two
indexpaths being considered in choose_bitmap_and(). The problem
is that one of them has 80000 quals attached to it, and the code
that's sorting through the quals is O(N^2).

> Our first attempt to fix this was putting these clauses into an rbtree
> or dynahash. This improves the performance, but is not entirely correct.

... depends on how you do it ...

> I settled on a simpler solution: limiting the number of clauses we try
> to uniquely identify. If there are too many, skip the smarter logic that
> requires comparing paths by clauses, and just return the cheapest input
> path from choose_bitmap_and(). The patch is attached.

I think you have the right basic idea, but we don't have to completely
lobotomize the bitmap-and search logic in order to cope with this.
This code is only trying to figure out which paths are potentially
redundant, so for a path with too many quals, we can just deem it
not-redundant, as attached.

A different line of thought is that using equal() to compare quals
here is likely overkill: plain old pointer equality ought to be enough,
since what we are looking for is different indexpaths derived from the
same members of the relation's baserestrictinfo list. In itself, such
a change would not fix the O(N^2) problem, it'd just cut a constant
factor off the big-O multiplier. (A big constant factor, perhaps, but
still just a constant factor.) However, once we go to pointer equality
as the definition, we could treat the pointer values as scalars and then
use hashing or whatever on them. But this would take a good deal of work,
and I think it might be a net loss for typical not-very-large numbers
of quals. Also, I tried just quickly changing the equal() call to a
pointer comparison, and it didn't seem to make much difference given
that I'd already done the attached. So my feeling is that possibly
that'd be worth doing sometime in the future, but this particular
example isn't offering a compelling reason to do it.

Another thought is that maybe we need a CHECK_FOR_INTERRUPTS call
somewhere in here; but I'm not sure where would be a good place.
I'm not excited about sticking one into classify_index_clause_usage,
but adding one up at the per-path loops would not help for this case.

regards, tom lane

Attachment Content-Type Size
choose-bitmap-and-2.patch text/x-diff 3.1 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Daniel Verite 2018-11-11 14:51:27 Re: csv format for psql
Previous Message Peter Geoghegan 2018-11-11 01:42:16 Re: Connections hang indefinitely while taking a gin index's LWLock buffer_content lock