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 1uETWB-008f5w-5K for pgsql-performance@arkaria.postgresql.org; Mon, 12 May 2025 13:48:31 +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 1uETW9-00GEAW-Cj for pgsql-performance@arkaria.postgresql.org; Mon, 12 May 2025 13:48:29 +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 1uETW9-00GEAL-2j for pgsql-performance@lists.postgresql.org; Mon, 12 May 2025 13:48:29 +0000 Received: from mail-wr1-x42d.google.com ([2a00:1450:4864:20::42d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uETW6-001Qzk-1v for pgsql-performance@lists.postgresql.org; Mon, 12 May 2025 13:48:28 +0000 Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-3a1f8c85562so1993609f8f.1 for ; Mon, 12 May 2025 06:48:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747057705; x=1747662505; 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=ivV2UYH4qFMdTJnyK8JstE7qhhf7izqJfIrflBIl8SM=; b=bNDq19/7RmPiRIwSINnox7lWdg1Reuyg2gA54s1p9w3kMZCgI8gfRwxKljiaajXJEc whKJrH/sWOxUOE0qN2kWqybTNkTSF16W1DO5Xdo0R9GFTKGOG1Bty/IDd5fQrn8cWFjj FIqJZSJ8TloDCF9/L72YJ2OCEjVwqyGAG3cQJdTixDFYoJzjikQCixZWNXjsuoKdpEiU 2/bA6coHOf1ChK1IWDNEbAvuiIXyO7HrmAukE1Dxx8ekcR87Ytx0bIabIEoWa09sspaR iEYlsvGrFYR+hg6EjIU2vz3+WWMulokKmS36mrJD6WmalNGO2szSi1ZtEMtoWPR0uKES cklw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747057705; x=1747662505; 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=ivV2UYH4qFMdTJnyK8JstE7qhhf7izqJfIrflBIl8SM=; b=sYfj9d3UO0sbmm6Ccm9+w025mFzJOAV5Nx0ZhGQ3eYF4l0amb7UjZZnvJO626uinGC AxUyxdBYL93uLtsdFDCeXNUb60seo+hXolNwIQ2fkXlSa5oM8anv4J9fe5M27IyoISbO LEFFlcZ9vX91EyAF3WS/6j/GN9s4x7PSdRJha8fcx2+JtrwFYdHiAqOexGKCrLAG9MSK MvRrNcJK0M5b1tSziXTnLockQPqVzLrE5Cx7J/5D5d8uQ3luvkXw4l4RQi908v1k7304 nbf+BELteuxEtdp/SF9J+kKo4PHPL0P3XB1fTtkO7idpCrkHZ7WbNulPeB9HgY81DD48 vidw== X-Gm-Message-State: AOJu0YzNE8PyofjC7gjUkt7lxEFDSD51M2gVv9t40R0nFwHsy4jBx0po n+QVmjp3WdT4DsEWkW4OXbunTf95obHvl3ipfBbgnyPaOYFi77IS X-Gm-Gg: ASbGncv3hBAEPykrnNKFPyY/wMc9PFD8nSTpBrThKAzCeY0vlFGBTEKGRrlY1lObU+J a65c58Qh8AGJn0lN9CPPf5HdblZVAKEXhwfvVOaLxTiTKmLaJHqWMrndw9aJJIwLDjEPHOkkKv3 gwAu+/zo/JI5iXxhxo6FBKzxCP2pdtb5xg6mhZdoTjjrUJ2cCjbKPXKVbqboCOvjuJoo000kiGK T5/S6gsu7FFvbydWRK7zVHlp+DBqi7mzAEJa/0mD8JNfs8J9dmSq6HzlSyzS7cQVeOIlXkU0Oo/ Q6G0NUcFtm1OT9Ga9KyOEZ4XRWy3d3gcwG/e3VHMbv584fjhLBf2dnsvb5dKYHOriRlS0Ocoe2V FZpo/qNiZLQ== X-Google-Smtp-Source: AGHT+IFBoDQMscIKYS5uaExYzTnQP6cavoUSqBbUdRW0GrxwAMOtmlpOl/d8muH/cIY7Y3YoiXzsEA== X-Received: by 2002:a5d:6489:0:b0:391:253b:4046 with SMTP id ffacd0b85a97d-3a1f6431452mr9233359f8f.16.1747057705381; Mon, 12 May 2025 06:48:25 -0700 (PDT) Received: from [192.168.0.89] (84.123.234.224.dyn.user.ono.com. [84.123.234.224]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a1f5a2d2f4sm12522050f8f.78.2025.05.12.06.48.24 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 12 May 2025 06:48:25 -0700 (PDT) Message-ID: <8480be46-6e2a-4b12-9f80-78ba3db79dfb@gmail.com> Date: Mon, 12 May 2025 15:48:24 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: inefficient/wrong plan cache mode selection for queries with partitioned tables (postgresql 17) To: Maxim Boguk Cc: pgsql-performance@lists.postgresql.org References: Content-Language: en-AU From: Andrei Lepikhov In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 5/12/25 15:08, Maxim Boguk wrote: > PS: problem not with difference between custom and generic planning time > but with prepared statements > generic plan plans only once, but custom plan plan every call (and plan > time cost 95% on total query runtime). Ah, now I got it. I'm aware of this problem from at least two sources of regular complaints. What can you do here? Let's imagine a palliative solution: Having pg_stat_statements data and the list of prepared statements (see pg_prepared_statement) and queryId enabled, there is a way to force a custom or generic plan in specific cases only: look up into min/max query execution time. If no big difference exists and planning time is sufficient, setting force_generic_plan for this plan makes sense. In another case, if the planning time is too short or the generic plan is unstable - switch to force_custom_plan. It is not hard to write such a tiny extension. As I see, the only extra stored "C" procedure is needed to set up force-plan-type flag employing FetchPreparedStatement(). The rest of the code - querying pg_stat_statements and switching between plan types may be written in plpgsql. If I'm not mistaken, it will work with all PG versions that are currently in support. What do you think? -- regards, Andrei Lepikhov