Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tmZ1Q-007fxf-Kk for pgsql-performance@arkaria.postgresql.org; Mon, 24 Feb 2025 14:01:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tmZ1P-00342r-48 for pgsql-performance@arkaria.postgresql.org; Mon, 24 Feb 2025 14:01:23 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tmZ1O-00341n-Oa for pgsql-performance@lists.postgresql.org; Mon, 24 Feb 2025 14:01:22 +0000 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tmZ1L-000U58-2T for pgsql-performance@lists.postgresql.org; Mon, 24 Feb 2025 14:01:21 +0000 Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-4398738217aso38917955e9.3 for ; Mon, 24 Feb 2025 06:01:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740405678; x=1741010478; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=HjG2EXQ23PLFQWoE6OxL4uNfxLSDcvGJR5Xw7x81Mzs=; b=FQNlmhD8j/Q1ViDtyb5n3E7EImCcErmVfdt4NMeaGoPaUwMJi+msNqUWY9MZJepabi pP3UlNhYtwSURPamS5ZNwTU2xdNlCwIYheYNhefypqT5FYeMJX7QekwUZr2LZ5Sxpm/B TUZGuzq44bKqZsFhnTtj6hfQoOxJKnXZ2ryP/qRhbntwWUwa+q1LzvGtH7dvqvXI98p5 fBLQZkKw0sd5EQj4ALY1p7hRRadhboweaSWAr1Q3Xpgrr4THDpKp5llCdvLmGxo1xFvq 3eAaSH+FHpfltjBAJdhHsiv9ba92vwxAuf5jtuBbaANLSZ5ICw0lJ1ZdM35p9K80wqsg hptw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740405678; x=1741010478; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=HjG2EXQ23PLFQWoE6OxL4uNfxLSDcvGJR5Xw7x81Mzs=; b=L+zVGwXpC/T6mkkSvmaocPoe6y+OqecTS2OfKSpsLew1uIYvbHEPXB+lZA7hS6L8Ey O5EFm2arglq7+dzxzpth+nrEFRikYWX939ufKvOdioOERBf7n8fvY5W19mFougAXwtqm 4nZUyJi484DANmJIKDxZUYnxQkN6p+gEb16S4jwNyr+5ozi8pKxJZnKDf1t96oSjf5fO vSGj260eAQVX3lff2lvKwEyJwOJXJmby8EGJcN6CeCIvCpJ36sGqxanTG2AdjZl1EU7m DKfDrGIbImWxhMnjI2M8VGNTgQtehm3rCgYW3XyPdA+fkPMb+AtFZvXHmVGoGqQafIGo 8K0g== X-Gm-Message-State: AOJu0YynYNKIqKV5TIdu6hXWB7pi5kI7uW31z3DKO0e11V8SGeNTXEjX KqYvIevqfUwykQJILeAgtEIYofkki1LjrxKc1eAGUW9KVfQkh5R+ X-Gm-Gg: ASbGncuN8l9JR1lBMc5sIGBcq0L5NI0nNd4Kko/TVnVQ5C8KWt9qUD+TAr+XbKi8LN8 9dY0Xdt0qJIHH2JJ0OAUqKaUEmTXTObDuyPmyOOLlsNHLlDr3FnI33DTOEWHTpLxR8LZBv8IoL6 JEUzolL8I7KOUa60P0WcysCRtrriVPwNBOWFbGRa/g1fTTwvuWN9n9jAgku4gJShvT7vCNiVIAc BFTyzX5pFSg+Yq1UtjCwXlJn42iicxNHfsRxZdoniM9onoO3feKnk6RjGFRW72se0R/0qSlUkye 5VOkgna6u4reToKniLYB+i6en3VzJbzodD4tN+IuKp6/eXLyuF5dsyRY120mDYEoaFJoCFIwp9o +r0EmPtqg+61T3pc= X-Google-Smtp-Source: AGHT+IFjwrbQ0CXCr1njqJhzY9ehCHJ0bJuOh6YedRIsfhAnu5uDk/9+eLZmmIY0E197w8wCP8qFYA== X-Received: by 2002:a05:600c:1c0b:b0:439:86fb:7328 with SMTP id 5b1f17b1804b1-439ae2188e5mr84410615e9.24.1740405677877; Mon, 24 Feb 2025 06:01:17 -0800 (PST) Received: from ?IPV6:2a0c:5a81:ba04:f100:fd53:ff46:7d02:7f2a? ([2a0c:5a81:ba04:f100:fd53:ff46:7d02:7f2a]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-439b02e6cf4sm106049715e9.19.2025.02.24.06.01.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 06:01:17 -0800 (PST) Message-ID: <937da8a1-7603-4b43-af4e-55de6af733b4@gmail.com> Date: Mon, 24 Feb 2025 15:01:16 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Unfortunate Nested Loop + Missing Autovacuum To: Lincoln Swaine-Moore Cc: pgsql-performance@lists.postgresql.org References: Content-Language: en-US From: Andrei Lepikhov In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 23/2/2025 23:49, Lincoln Swaine-Moore wrote: > Thanks for the reply! I tried the analysis on our much shorter staging > table and it did change the plan. I haven’t tried it on the production > ones because my understanding is that the autovacuum process is gentler > with resource consumption and I didn’t want to gum things up in the > meantime. But that may be false or avoidable. > > Unfortunately the insertions are not batched—they arrive in a fairly > steady stream. Given that, and the fact that we’re talking about the > behavior of the daemon, I’m not sure how to provide a reproduction > (though would be game if you had pointers). I could in theory make a job > analyze these tables on a regular cadence, though I guess that feels > like I’d be stepping on autovacuum’s toes. > > Would be happy to read over similar reports if you could steer me toward > them. I know about at least two problematic use cases: 1. Batch insertions of tuples with steadily growing value of a column (timestamp as an example). Selecting "just arrived" tuples, we can't estimate cardinalities correctly because these values are out of histogram and MCV statistics until the next analyze. 2. The table is overcrowded by queries, and autovacuum does not have a chance to pass the table. For case No.1, I attempted to extend the 'probe indexes' technique [1]. However, it is applicable only in specific conditions (analytic load, for example) and should be designed as an extension. Unfortunately, we still do not have selectivity hooks to let it go. To resolve case No.2 (and not only), I have invented the 'plan freezing' extension [2,3]. Using it, you may find the optimal plan once and freeze it in the instance's plan cache, picking constants that have to be treated as parameters. Any changes of the table content, statistics, or even GUCs will not impact the query plan until "unfreezing". Also, you may use the pg_hint_plan extension [4] to provide Postgres optimiser with hints that can help it generate the plan you need. [1] https://danolivo.substack.com/p/probing-indexes-to-survive-data-skew [2] https://danolivo.substack.com/p/designing-a-prototype-postgres-plan [3] https://postgrespro.com/docs/enterprise/16/sr-plan [4] https://github.com/ossc-db/pg_hint_plan -- regards, Andrei Lepikhov