Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jVsct-0003VW-74 for pgsql-performance@arkaria.postgresql.org; Tue, 05 May 2020 08:08:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jVscs-0007va-1S for pgsql-performance@arkaria.postgresql.org; Tue, 05 May 2020 08:08:26 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jVscr-0007vT-QX for pgsql-performance@lists.postgresql.org; Tue, 05 May 2020 08:08:25 +0000 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jVscp-0003p5-IQ for pgsql-performance@postgresql.org; Tue, 05 May 2020 08:08:24 +0000 Received: by mail-wm1-x341.google.com with SMTP id g12so1212076wmh.3 for ; Tue, 05 May 2020 01:08:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:cc:date:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=uSfew4j6b/D0tVtutso0ljwImkp0W4sbLJMLhfz2F3Y=; b=cutioJyNAGyvCMs18QUs0sdtEhk1Z/vBi7wW3nuhSZs5XE4bRax/Ewv3znZYPzw7Ot 7I4KYxtwpuPR4LZAN2JsrGHZEgGeR0vH954BJLdicpSsVKNI+icejgfWfK+KlG4CLRJA LfYI6RdqCPp0AGY+qxGd5RWIiAADMv8qTUpoIkJU3IT6g/rdWKGXvJiIpGXwattjZGyV CE4nD5RFSoFQ6fmp4J6HAB9iUZcdU7VzFIwvaFnNMTUC2rqcTzUIdd9PxEcgz1ZffgmI X2tApizks1wg4lbBl2A59aQim2lEhQtidco37Y3Ny6lPPG+vG1J1gHvjDMv0HRxBo3JS ZzNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=uSfew4j6b/D0tVtutso0ljwImkp0W4sbLJMLhfz2F3Y=; b=B/l1GHSn5w3u7k1/ov6VboG4eVRIj/BsdoygH9wB/ZAXF/YaxSmzX5nDh0Vu514Gcg NhaoA9IVaZA1brQXhgavYakQ/ROCsjnj4bOV8Dc8SxRTtmTBpgJarnCV56uq9yuoS0ON WGP6zmSnRlQFFRoa/a3hrVOftj9GF9/TIHo4NnrHbJdJ/5FZe9ND52cysN9vxc24P1w8 1OLwHxgNo9XEbhd2s9vdezrPENqOWJhUp5jiFolmb5MGkNfnTsQSERTGG3nTJ9saAV1N zGG2QvKA5SZVN2oWJcVs0ihRK6oJZ6AtWFx6pEa/JKC4kDwpEOTDn3mYiwl4f9YuOXL1 fFXw== X-Gm-Message-State: AGi0Pubpjt04c11NxJgK8XLkEzzn0nOd1t+JAdL+t5s3Cxr9IVBGUj9p zdemNwLmfPWPPLS3bnc7ONl9TQ== X-Google-Smtp-Source: APiQypI6VEPzADgrhZ1dnOZjf0rsyW3C7sRZrMAY6hLJIsn+scLlGlRP5xRyweEiZbSrfdF9IUHnBA== X-Received: by 2002:a05:600c:2194:: with SMTP id e20mr1867182wme.22.1588666101054; Tue, 05 May 2020 01:08:21 -0700 (PDT) Received: from dynamic-epxaph215gp4f1osc-pd01.res.v6.highway.a1.net (dynamic-epxaph215gp4f1osc-pd01.res.v6.highway.a1.net. [2001:871:60:e806:264c:4bf6:b041:c2bc]) by smtp.gmail.com with ESMTPSA id y63sm2529728wmg.21.2020.05.05.01.08.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 May 2020 01:08:20 -0700 (PDT) Message-ID: <1949097f3ae6189d111d4e0dba63505db958ffef.camel@cybertec.at> Subject: Re: Please help! Query jumps from 1s -> 4m From: Laurenz Albe To: James Thompson , Justin Pryzby Cc: pgsql-performance@postgresql.org Date: Tue, 05 May 2020 10:08:20 +0200 In-Reply-To: References: <20200503153825.GK28974@telsasoft.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.32.5 (3.32.5-1.fc30) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, 2020-05-04 at 20:12 +0100, James Thompson wrote: > The change is abrupt, on the 10th execution (but I hadn't spotted it was always after the > same number of executions until your suggestion - thanks for pointing me in that direction). > > I don't see any custom configuration on our end that changes the threshold for this from 5->10. > Debugging the query call I also see that PgConnection has the prepareThreshold set to 5. > > Additionally, the execution plans for the 10th + following queries look fine, they have the > same structure as if I run the query manually. It's not that the query plan switches, > it seems as though the same query plan is just > 200X slower than usual. > > As for the heap fetches -> as far as I can tell, on both occasions the fetches are relatively > low and shouldn't account for minutes of execution (even if one is lower than the other). > Looking through one days logs I do find cases with lower heap fetches too, for example as > below which has 1977 fetches instead of the previous 6940 but took approx the same time: > -> Index Only Scan using table1_typea_include_uniqueid_col16_idx on table1 table1alias1 (cost=0.56..17.25 rows=1 width=60) (actual time=56.858..120893.874 rows=67000 loops=1) > Index Cond: (col20 = $2005) > Filter: (((col3 = $2004) OR (col3 IS NULL)) AND ((col8)::text = ANY ((ARRAY[$1004, ..., $2003])::text[]))) > Rows Removed by Filter: 2662793 > Heap Fetches: 1977 > Buffers: shared hit=84574 read=3522 > > Would you agree the statement threshold / heap fetches seems unlikely to be causing this? Any other thoughts? It does sound suspiciously like custom plans vs. generic plan. If you are using JDBC, then the cut-off of 10 would make sense: the JDBC driver uses (server) prepared statements only after the fifth execution, and the prepared statement will use a generic plan only after the fifth execution. It would be good to see the execution plan from the third, seventh and thirteenth execution. You could use "auto_explain" for that. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com