Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d2bnU-0006mR-SV for pgsql-hackers@arkaria.postgresql.org; Mon, 24 Apr 2017 11:04:49 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1d2bnT-00077V-4t for pgsql-hackers@arkaria.postgresql.org; Mon, 24 Apr 2017 11:04:47 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1d2bnP-00072z-P2 for pgsql-hackers@postgresql.org; Mon, 24 Apr 2017 11:04:43 +0000 Received: from mail.postgrespro.ru ([93.174.131.138]) by makus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d2bnL-0003Zm-U2 for pgsql-hackers@postgresql.org; Mon, 24 Apr 2017 11:04:42 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 8063A21C2A7D for ; Mon, 24 Apr 2017 14:04:36 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru Subject: Re: Cached plans and statement generalization DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1493031876; bh=7VYmhTQbRE9k8xqLHLxLG/GQq+5uuhj4Vgcx4ygfAQM=; h=To:References:From:Date:In-Reply-To; b=pid38vvGe5RfZau2PZ73yfRvj8Ht4UZ1OeACVqykgHP2SD7kauNgGeEh9YdxhITxq K3QfACELXRlGH7yGQGOXPASwTY+oj3eH4HdSCu3yabIsFQSx6Wt37Ij8KPhrMXCFsS zl7+VoBX2P7reMAUNMWxXdetzyx8g5mNv0M3eRSY= To: pgsql-hackers@postgresql.org References: <8e76d8fc-8b8c-14bd-d4d1-e9cf193a74f5@postgrespro.ru> From: Konstantin Knizhnik Message-ID: <7d90eab7-bccf-119e-3e61-c779a9b51197@postgrespro.ru> Date: Mon, 24 Apr 2017 14:04:42 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------34564A416FBD016715220F7C" X-Pg-Spam-Score: -2.0 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org This is a multi-part message in MIME format. --------------34564A416FBD016715220F7C Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 24.04.2017 13:24, Alexander Korotkov wrote: > Hi, Konstantin! > > On Mon, Apr 24, 2017 at 11:46 AM, Konstantin Knizhnik > > wrote: > > There were a lot of discussions about query plan caching in > hackers mailing list, but I failed to find some clear answer for > my question and the current consensus on this question in Postgres > community. As far as I understand current state is the following: > 1. We have per-connection prepared statements. > 2. Queries executed inside plpgsql code are implicitly prepared. > > It is not always possible or convenient to use prepared statements. > For example, if pgbouncer is used to perform connection pooling. > Another use case (which is actually the problem I am trying to > solve now) is partitioning. > Efficient execution of query to partitioned table requires > hardcoded value for partitioning key. > Only in this case optimizer will be able to construct efficient > query plan which access only affected tables (partitions). > > My small benchmark for distributed partitioned table based on > pg_pathman + postgres_fdw shows 3 times degrade of performance in > case of using prepared statements. > But without prepared statements substantial amount of time is > spent in query compilation and planning. I was be able to speed up > benchmark more than two time by > sending prepared queries directly to the remote nodes. > > > I don't think it's correct to ask PostgreSQL hackers about problem > which arises with pg_pathman while pg_pathman is an extension > supported by Postgres Pro. > Since we have declarative partitioning committed to 10, I think that > community should address this issue in the context of declarative > partitioning. > However, it's unlikely we can spot this issue with declarative > partitioning because it still uses very inefficient constraint > exclusion mechanism. Thus, issues you are writing about would become > visible on declarative partitioning only when constraint exclusion > would be replaced with something more efficient. > > Long story short, could you reproduce this issue without pg_pathman? > Sorry, I have mentioned pg_pathman just as example. The same problems takes place with partitioning based on standard Postgres inheritance mechanism (when I manually create derived tables and specify constraints for them). I didn't test yet declarative partitioning committed to 10, but I expect the that it will also suffer from this problem (because is based on inheritance). But as I wrote, I think that the problem with plan caching is wider and is not bounded just to partitioning. > ------ > Alexander Korotkov > Postgres Professional: http://www.postgrespro.com > > The Russian Postgres Company > > -- Konstantin Knizhnik Postgres Professional: http://www.postgrespro.com The Russian Postgres Company --------------34564A416FBD016715220F7C Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

On 24.04.2017 13:24, Alexander Korotkov wrote:
Hi, Konstantin!

On Mon, Apr 24, 2017 at 11:46 AM, Konstantin Knizhnik <k.knizhnik@postgrespro.ru> wrote:
There were a lot of discussions about query plan caching in hackers mailing list, but I failed to find some clear answer for my question and the current consensus on this question in Postgres community. As far as I understand current state is the following:
1. We have per-connection prepared statements.
2. Queries executed inside plpgsql code are implicitly prepared.

It is not always possible or convenient to use prepared statements.
For example, if pgbouncer is used to perform connection pooling.
Another use case (which is actually the problem I am trying to solve now) is partitioning.
Efficient execution of query to partitioned table requires hardcoded value for partitioning key.
Only in this case optimizer will be able to construct efficient query plan which access only affected tables (partitions).

My small benchmark for distributed partitioned table based on pg_pathman + postgres_fdw shows 3 times degrade of performance in case of using prepared statements.
But without prepared statements substantial amount of time is spent in query compilation and planning. I was be able to speed up benchmark more than two time by
sending prepared queries directly to the remote nodes.

I don't think it's correct to ask PostgreSQL hackers about problem which arises with pg_pathman while pg_pathman is an extension supported by Postgres Pro.
Since we have declarative partitioning committed to 10, I think that community should address this issue in the context of declarative partitioning.
However, it's unlikely we can spot this issue with declarative partitioning because it still uses very inefficient constraint exclusion mechanism.  Thus, issues you are writing about would become visible on declarative partitioning only when constraint exclusion would be replaced with something more efficient.

Long story short, could you reproduce this issue without pg_pathman?

 
Sorry, I have mentioned pg_pathman just as example. 
The same problems takes place with partitioning based on standard Postgres inheritance mechanism (when I manually create derived tables and specify constraints for them).
I didn't test yet declarative partitioning committed to 10, but I expect the that it will also suffer from this problem (because is based on inheritance).
But as I wrote, I think that the problem with plan caching is wider and is not bounded just to partitioning.
 


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



-- 
Konstantin Knizhnik
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company 
--------------34564A416FBD016715220F7C--