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 1lJC1i-0002Ci-BB for pgsql-performance@arkaria.postgresql.org; Mon, 08 Mar 2021 09:18:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lJC1h-0008Jq-9g for pgsql-performance@arkaria.postgresql.org; Mon, 08 Mar 2021 09:18:09 +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 1lJC1g-0008Jd-Tv for pgsql-performance@lists.postgresql.org; Mon, 08 Mar 2021 09:18:09 +0000 Received: from mail-wm1-x331.google.com ([2a00:1450:4864:20::331]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lJC1d-0003r7-QQ for pgsql-performance@postgresql.org; Mon, 08 Mar 2021 09:18:07 +0000 Received: by mail-wm1-x331.google.com with SMTP id y124-20020a1c32820000b029010c93864955so3334746wmy.5 for ; Mon, 08 Mar 2021 01:18:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=Qh/Ex81Wuo4TV+svXlYu72HlFOGVStrZJ/6REA2M7mc=; b=chhH4sb+dSaHeeGk1z42LN1POE3iTebCVNgjJVrVqovucBbJWzC9QgfllRVoQwzQRM xf8dxEbMyftP3785BdcwdC6CSQRvY1UWmHYJ7U07SbE8/E/l/3/Gip4tQQnAqYN18Q4r LKqU3dyc6Pf3G6TXYf5eqi7aAG8A96AM0BxpPYO8sTppluifLpirSAKtOU3sOsK+i3b5 d2CpBNNVuW1YnaJdrSYz+mKcwSwsNXFFf+3+YwvUsjDzI0iGaqvoPyp5wZ6TzQ7blirE sfsX/eGuQTn24D2vcsQQUKFgxr2qoYSiiCGQQ8UPt68noyNrJWITf/xOQeiEsks0S/S0 sFoA== 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:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=Qh/Ex81Wuo4TV+svXlYu72HlFOGVStrZJ/6REA2M7mc=; b=UMUQfrkXp7priJSzRyiFknNhWARZuMJblwVSq457ttZwaV36i99BsiZsJ5NchnBnfe D1Ae6VDTRWHQ9EdcgsGJUGSbm6vSDT79jq2ScakCYZ5GO7br3yi91gEi+XrN/ugsg2kX CpFVkLpr9KhjYiC7Huy9H8aS4EiD1ix2tfu53XA0PrSM3ZnnPA7xsbK4jtuBRSvQjcaH QV5c6H0fNMG+3seOk5c7X6X7wipU1J9JaYdyts1XytXRrorvevVphc0KnuPgMWxXB7BO YQZ8uKIXgsx8FjZfeN6ND0DduKmlQmAkpFT3zMkrYH3wYtBxRQhGbLsDOoFH++xOIcbu 1Nfg== X-Gm-Message-State: AOAM531E9ov/jSu89e5K1zHHpKwB14jO00uYKWsdySm5sBNeuBinZqGR DZqlNJZ2mMoThDXBwhqDm+aBPQ== X-Google-Smtp-Source: ABdhPJxbqF37q79nRbkvtf6Ib+I07m5udKDulrWOGRPw6czNB8QMbh42rr9rxRJNqvTFbXBD9f3xxQ== X-Received: by 2002:a05:600c:2312:: with SMTP id 18mr21164739wmo.8.1615195084029; Mon, 08 Mar 2021 01:18:04 -0800 (PST) Received: from localhost.localdomain (217-149-170-140.nat.highway.telekom.at. [217.149.170.140]) by smtp.gmail.com with ESMTPSA id b65sm17803690wmh.4.2021.03.08.01.18.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Mar 2021 01:18:03 -0800 (PST) Message-ID: Subject: Re: Slow query performance inside a transaction on a clean database From: Laurenz Albe To: val.janeiko@gmail.com, pgsql-performance@postgresql.org Date: Mon, 08 Mar 2021 10:17:59 +0100 In-Reply-To: <002101d711e8$bfd8a490$3f89edb0$@gmail.com> References: <002101d711e8$bfd8a490$3f89edb0$@gmail.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 2021-03-05 at 17:55 +0000, val.janeiko@gmail.com wrote: > I have a SELECT query that uses a long chain of CTEs (6) and is executed > repeatedly as part of the transaction (with different parameters). It is > executed quickly most of the time, but sometimes becomes very slow. I > managed to consistently reproduce the issue by executing a transaction > containing this query on an empty database. The query is fast for the first > 150-170 inserted resources, but ~50% of the executions afterwards take 5.6s > instead of 1.4ms. Additionally it only becomes slow if resources are > inserted in a random order, if I insert resources sorted by > `start_date_time` column the query is always fast. > > Here are the results of EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT > JSON): > - Slow: https://explain.depesz.com/s/e4Fo > - Fast: https://explain.depesz.com/s/7HFJ If your transaction modifies the data significantly (which it does if the table is empty before you start), you should throw in an ANALYZE on the affected tables occasionally. Normally, autovacuum takes care of that, but it cannot see your data until the transaction is committed. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com