Uninterruptible long planning of a query with too many WHERE clauses

From: Alexander Kuzmenkov <a(dot)kuzmenkov(at)postgrespro(dot)ru>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Uninterruptible long planning of a query with too many WHERE clauses
Date: 2018-11-09 15:00:41
Message-ID: 90c5bdfa-d633-dabe-9889-3cf3e1acd443@postgrespro.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi hackers,

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). The reason for the slowness is that the
run time of choose_bitmap_and() is quadratic in the number of WHERE
clauses. It assigns unique ids to the clauses by putting them in a list
and then doing a linear search with equal() to determine the position of
each new clause.

Our first attempt to fix this was putting these clauses into an rbtree
or dynahash. This improves the performance, but is not entirely correct.
We don't have a comparison or hash function for nodes, so we have to
hash or compare their string representation. But the equality of
nodeToString() is not equivalent to equal(), because the string has some
fields that are ignored by equal(), such as token location. So we can't
really compare the string value instead of using equal().

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'd like to hear your thoughts on this. This is a valid query that
freezes a backend with 100% CPU usage and no way to interrupt it, and I
think we should fail more gracefully.

Alexander Kuzmenkov
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company

Attachment Content-Type Size
schema_dump.sql application/sql 3.1 KB
select.sql.bz2 application/x-bzip 3.1 MB
choose-bitmap-and.patch text/x-patch 3.6 KB


Browse pgsql-hackers by date

  From Date Subject
Next Message Adam Berlin 2018-11-09 15:14:00 Re: Add extension options to control TAP and isolation tests
Previous Message Robert Haas 2018-11-09 14:50:19 Re: ATTACH/DETACH PARTITION CONCURRENTLY