Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hsuaU-0003fV-8r for pgsql-hackers@arkaria.postgresql.org; Wed, 31 Jul 2019 19:48:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hsuaS-0003s9-Hc for pgsql-hackers@arkaria.postgresql.org; Wed, 31 Jul 2019 19:48:36 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1hsuaS-0003ok-58 for pgsql-hackers@lists.postgresql.org; Wed, 31 Jul 2019 19:48:36 +0000 Received: from cyclops.postgrespro.ru ([93.174.131.138] helo=mail.postgrespro.ru) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hsuaP-0006yI-Ok for pgsql-hackers@postgresql.org; Wed, 31 Jul 2019 19:48:35 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 3DDE321C3D7B; Wed, 31 Jul 2019 22:48:17 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=x tagged_above=-99 required=4 WHITELISTED tests=[] autolearn=unavailable Received: from [192.168.0.12] (broadband-178-140-241-108.ip.moscow.rt.ru [178.140.241.108]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 0199E21C3D47; Wed, 31 Jul 2019 22:48:16 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1564602497; bh=KfRoGqaAs7WH1ZU1M97OSpb6CRZjl+xhpfh7BwloOKQ=; h=Subject:To:Cc:References:From:Date:In-Reply-To; b=T/fKXq7mF1t3KB+pAl8gWSw2tGQytw3rHSZR+DbQzLEqstXt0q+fZF98O1KmU4P8Y FyJwoN6KsH5olLJche9+snn0qpj5wRvMVAIstrxJLeE9tD6L1Kjq7cjmOgE6v27D/N UHXirltRTUpFSsA7t3McG4Jc7OLFN/ggdBqXbFNg= Subject: Re: [HACKERS] Cached plans and statement generalization To: Heikki Linnakangas Cc: Thomas Munro , PostgreSQL Hackers References: <9A6E5062D5D4DB458C80C2B2920BD71D610148@g01jpexmbkw23> <6ea1db86-b300-b57c-0f9d-a55dd3966175@postgrespro.ru> <9A6E5062D5D4DB458C80C2B2920BD71D6820CB@g01jpexmbkw23> <554d0730-f6fc-d1be-fe78-e63973ec621f@postgrespro.ru> <35be0423-36bc-cadc-bef8-02d475751f7d@postgrespro.ru> <7603a101-6b79-2328-2b4b-3bc3217992e5@postgrespro.ru> <3304c34b-4a9c-909c-4ee1-d8e7f6dcb2de@postgrespro.ru> <66f47595-1b93-ef67-fdd6-c5414e0f94c3@postgrespro.ru> From: Konstantin Knizhnik Message-ID: <5538dbd6-fcbb-33a7-49d8-28c5cd2df435@postgrespro.ru> Date: Wed, 31 Jul 2019 22:48:16 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 31.07.2019 19:56, Heikki Linnakangas wrote: > On 09/07/2019 23:59, Konstantin Knizhnik wrote: >> Fixed patch version of the path is attached. > > Much of the patch and the discussion has been around the raw parsing, > and guessing which literals are actually parameters that have been > inlined into the SQL text. Do we really need to do that, though? The > documentation mentions pgbouncer and other connection poolers, where > you don't have session state, as a use case for this. But such > connection poolers could and should still be using the extended query > protocol, with Parse+Bind messages and query parameters, even if they > don't use named prepared statements. I'd want to encourage > applications and middleware to use out-of-band query parameters, to > avoid SQL injection attacks, regardless of whether they use prepared > statements or cache query plans. So how about dropping all the raw > parse tree stuff, and doing the automatic caching of plans based on > the SQL string, somewhere in the exec_parse_message? Check the > autoprepare cache in exec_parse_message(), if it was an "unnamed" > prepared statement, i.e. if the prepared statement name is an empty > string. > > (I'm actually not sure if exec_parse/bind_message is the right place > for this, but I saw that your current patch did it in > exec_simple_query, and exec_parse/bind_message are the equivalent of > that for the extended query protocol). It will significantly simplify this patch and eliminate all complexity and troubles  caused by replacing string literals with parameters if assumption that all client applications are using extended query protocol is true. But I am not sure that we can expect it. At least I myself see many applications which are constructing queries by embedding literal values. May be it is not so good and safe (SQL injection attack), but many applications are doing it. And the idea was to improve execution speed of existed application without changing them. Also please notice that extended protocol requires passing more message which has negative effect on performance. At my  notebook I get about 21k TPS on "pgbench -S" and 18k TPS on "pgbench -M extended -S". And it is with unix socket connection! I think that in case of remote connections difference will be even larger. So may be committing simple version of this patch which do not need to solve any challenged problems is good idea. But I afraid that it will significantly reduce positive effect of this patch.