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 1hRzkE-00050w-Ir for pgsql-general@arkaria.postgresql.org; Sat, 18 May 2019 13:51:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hRzkC-0007uT-KV for pgsql-general@arkaria.postgresql.org; Sat, 18 May 2019 13:51:24 +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 1hRzkA-0007tM-R5 for pgsql-general@lists.postgresql.org; Sat, 18 May 2019 13:51:24 +0000 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hRzk1-0000aM-CW for pgsql-general@lists.postgresql.org; Sat, 18 May 2019 13:51:22 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id BA405382; Sat, 18 May 2019 09:51:09 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Sat, 18 May 2019 09:51:09 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=p JzRj6NjwfSdN5YWmj8sCze3CzzjX7be7o8wtRfrZ0Q=; b=IUMLWVpFkO5KNHdwl eT7TmcG2A7nBZiqyle6F9bXnQ7bVkFQutCX33Gk5GcNF/vf8hZwRIHWzlpWm0sg6 odkBCdtkvHLJ1lQLR+7mp2StuKWJRsd1ROw7m5CPZJWtWeyqEPevm+/vOgxEMjnL N1ukOcHO+FxpAMcIB79c+jEZTbUcesQF7Z21ymyQ58zEvDwKH8/g09Mtpia1g7d7 6BVzEXGX9qiF3rsYfA6Cy8Sb9sT6yICgN2bpv3sou4JvG6NSiSm1tNYijcDQRhw/ 1a5+pIhMFnlnqlZOo7Wh+LCfOy/a08NMPTDt30PZl7jUfKUA5xcOdVxoZ9kXYnpA HDmcw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=pJzRj6NjwfSdN5YWmj8sCze3CzzjX7be7o8wtRfrZ 0Q=; b=a6yChxnjB2KwScYRLuj0zS3UJ0W3hMN2zG55M0pnFCPu+SVOpRrwcEA/c XxoKuIA9U+Q2nJgwJ1XtTq1Y4oQAje0dNpn526Cd08bVyAXtL463bbUQHwpamI10 b+l7Op8+PEJ0kDMziuHK9YLwrJwDGcipo4IBMGoI9643ahPNbiw8mPDOG09BIxyx WHxNO92voEKPQS8IYRj7NHRTy/6R27Qz9wxAU+NNwH1zj3gMG9lXLb6FJWf75MZE ZO0RrmjYy+duONyPww1/hmxtcBdAFMFu3w5W6+LYhFYUvLGvctMj2XFoJPPWEP1w D8iMLqOnv/94zl3kvHdxsw9TAG0qw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddruddtgedggeegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefheenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucffohhmrghinhepuggvphgvshiirdgtohhmnecukfhppeejuddrvdduvddrudeh tddrvddtvdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrh esrghklhgrvhgvrhdrtghomhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id 413018005B; Sat, 18 May 2019 09:51:08 -0400 (EDT) Subject: Re: Strange performance degregation in sql function (PG11.1) To: Alastair McKinley , "pgsql-general@lists.postgresql.org" References: From: Adrian Klaver Message-ID: Date: Sat, 18 May 2019 06:51:07 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 5/18/19 4:17 AM, Alastair McKinley wrote: > Hi all, > > I recently experienced a performance degradation in an operational > system that I can't explain.  I had a function wrapper for a aggregate > query that was performing well using the expected indexes with the > approximate structure as shown below. > > create or replace function example_function(param1 int, param2 int) > returns setof custom_type as > $$ >     select * from big_table where col1 = param1 and col2 = param2; > $$ language sql; > > After creating two new indexes on this table to support a different use > case during a migration, this unchanged function reduced in performance Postgres version? Was the migration from one Postgres version to another? Did you run ANALYZE after migration? More below. > by several orders of magnitude.  Running the query inside the function > manually on the console however worked as expected and the query plan > did not appear to have changed.  On a hunch I changed the structure of > the function to the structure below and immediately the query > performance returned to the expected baseline. Can you provide the EXPLAIN ANALYZE for each case. If you are worried about the information revealed maybe use the anonymization available here: https://explain.depesz.com/ Using EXECUTE will override the plan caching in plpgsql. > > create or replace function example_function(param1 int, param2 int) > returns setof custom_type as > $$ > BEGIN >     return query execute format($query$ >         select * from big_table where col1 = %1$L and col2 = %1$ >     $query$,param1,param2); > END; > $$ language plpgsql; > > The source data itself did not change during the time when I noticed > this issue.  Can anyone explain or guess what could have caused this > degradation?  The only other maintenance that I attempted was 'DISCARD > PLANS;' which did not help. > > Best regards, > > Alastair -- Adrian Klaver adrian.klaver@aklaver.com