Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d2Zei-0006xu-86 for pgsql-hackers@arkaria.postgresql.org; Mon, 24 Apr 2017 08:47:36 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1d2Zeg-0002qT-Up for pgsql-hackers@arkaria.postgresql.org; Mon, 24 Apr 2017 08:47:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1d2ZdB-0000C0-58 for pgsql-hackers@postgresql.org; Mon, 24 Apr 2017 08:46:01 +0000 Received: from mail.postgrespro.ru ([93.174.131.138]) by magus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d2Zd7-0005zy-RM for pgsql-hackers@postgresql.org; Mon, 24 Apr 2017 08:46:00 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 957E921C2819 for ; Mon, 24 Apr 2017 11:45:56 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru To: pgsql-hackers@postgresql.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1493023556; bh=If0jsi4J8McqU0R0P9JiGH0R/Cw3wbsMF8JaiGvwSrk=; h=From:Subject:Date; b=docmsE/O0OnJsCzCqf1kxIIYjnALNQwQ4czpzuRy0B868gtJ6eAAM3aGJEeA5dP9H nsNkpxDrOt3rc3rhWqdprxRTYQLPiQcOC0m/aaj03zi3VwTXA0tSu9gxlQMBUB5TEe qsGnV/8v/MpQVSQhj1wb8XnVWFmp8s8xM8F/qBL4= From: Konstantin Knizhnik Subject: Cached plans and statement generalization Message-ID: <8e76d8fc-8b8c-14bd-d4d1-e9cf193a74f5@postgrespro.ru> Date: Mon, 24 Apr 2017 11:46:02 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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 Hi hackers, 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. So what I am thinking now is implicit query caching. If the same query with different literal values is repeated many times, then we can try to generalize this query and replace it with prepared query with parameters. I am not considering now shared query cache: is seems to be much harder to implement. But local caching of generalized queries seems to be not so difficult to implement and requires not so much changes in Postgres code. And it can be useful not only for sharding, but for many other cases where prepared statements can not be used. I wonder if such option was already considered and if it was for some reasons rejected: can you point me at this reasons? -- Konstantin Knizhnik Postgres Professional: http://www.postgrespro.com The Russian Postgres Company -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers