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 1v6PQL-0048nH-DO for pgsql-bugs@arkaria.postgresql.org; Wed, 08 Oct 2025 08:21: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 1v6PQI-008Wfi-Ap for pgsql-bugs@arkaria.postgresql.org; Wed, 08 Oct 2025 08:21: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 1v6PQI-008WfZ-1O for pgsql-bugs@lists.postgresql.org; Wed, 08 Oct 2025 08:21:22 +0000 Received: from mail-ej1-x62b.google.com ([2a00:1450:4864:20::62b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1v6PQF-000dXZ-27 for pgsql-bugs@lists.postgresql.org; Wed, 08 Oct 2025 08:21:22 +0000 Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-b41870fef44so1401729566b.3 for ; Wed, 08 Oct 2025 01:21:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1759911678; x=1760516478; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=yc7pkOr7zcBxFD0arJCdJJTn5fJPJF1N2iIunxwrmyA=; b=S2qMXZJqhKC2PUY4OS8kfDgKu3616szIbWjLzf9ofKSPzTuLFZJFqJeyziFLGzMM3e 9RpyhcVOHGnV9FvKszJlqlanlYygaTuCTsjMG51TS+DcHKSXIjGPL7u9997tT9cahe71 e5Xdm5wNXxkDTXhBxIkaI3qmTcY9tD4PRE0OgVWa2nn4qtunrY5K26uLJy/m3jr/X6ki oEb1xswE5JtCNovWWHvO13yg7/c4nNMkuCd2byfKuHCD/UaYYeu0NfPEXvna0DYDpvk7 gnMerlIa/Mol2py6VDoj1lp93H/YvStuhkQIZnRZKbi8QNtufN6dzWdiOEiEq3MXwQhy dw5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759911678; x=1760516478; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=yc7pkOr7zcBxFD0arJCdJJTn5fJPJF1N2iIunxwrmyA=; b=lneZN+O1kCbn4NSbe392liZptV9QHs49ifZm45xd1f+dlj3ShDl5uznp+A+Xpp+OBr yN1xbUx4kerDGcjHbsTEbd+7N6BFc0z//GKEbVqeDDLGJzKbScTBmfd3ObrlzYKZHsbx pzISNtWENDsIs6bMthPXMCbwHXiVtSGmXeINrZXOPf7ArVOrfzr6+114gcFYE/O88paf pjD08x2THXFMfIaF8hedfDEWZoLjmotgLYLAKmsMGl5PCkIAiE1Z56TnSt97J92iQWNG 0Kna/nUigT09ZtItBN2dTApYv4uQkT6P38Br7a411UHV7d5ZAzRSud/5RYpDH/V40lrX IQmA== X-Forwarded-Encrypted: i=1; AJvYcCVGL8RbYZq5eZ4wrre+zmNM1mh9kBmGqc/M028MfPnpUyB8uVRwGxaYEISxvWXdh0rznJGTVg7C4IF9@lists.postgresql.org X-Gm-Message-State: AOJu0YyzHop+0TCYFIU/0CIW3vzuqMqoElPtdDNt3h9wLFmslDtW1FFX llTHj2QlXfYKykg89eD4sBNXCbRZm2O3vwZ6XjWcGsbzeDgltyJEpcK0ffN10r7W6H0= X-Gm-Gg: ASbGncujJ56S9mgn/rcMNCgNoeKseEsawpkMIMRFr68oeWPUJbflzqOf5ZVVDJXow3m rA3ib6Y480Jsm6Jm8TVsu/w9fle/A2q1BqG3VmXxxb9QLA9+RgWiDK7nahuB8KyPIQmcInmzyO8 F+yCj+3GqpJeeaY4Do8J3MGXQijfGA0PVHxmzEUMOjKLuFVaE2jGUmWxdA00tIOWUhtA8LqwOUV H1XgQXLZhcj2ZlPR0f+2BbTLwxBLXWctvk6t/CoOAKPpD528h84TAFCDTEGHK4GkPsd6tdCX+xe p6PmTe65pAnVBYdawCSP5BlgeolgIxXEN5R08MZ71GwQk3ldHR5O+mr9pelGBNs43d4uXQRkaCi 02ZnM2awnFtXjjxofx1T8jvMvPXm9dzOk8I9DHwPe/2b+1ECuuJXWOugAtvRLV6kidJ/lU6l/W5 Cj X-Google-Smtp-Source: AGHT+IEb9WPSDOZXDBgjlVyF4lNV/jv4bQgxjc065JReQ3NtHjK3CCdzSMtTnJlwdVq4j23NguvYAA== X-Received: by 2002:a17:907:c05:b0:b3b:d657:482b with SMTP id a640c23a62f3a-b50aa48b996mr282508566b.2.1759911677656; Wed, 08 Oct 2025 01:21:17 -0700 (PDT) Received: from laurenz.albe-K4N0CV00F97414D ([88.116.133.170]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b4865a83f5fsm1608340166b.32.2025.10.08.01.21.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Oct 2025 01:21:17 -0700 (PDT) Message-ID: <281ae4783e0174e1cb77a560936b86a9f542bba4.camel@cybertec.at> Subject: Re: BUG #19076: Generic query plan is extremely slow From: Laurenz Albe To: premal.patel@veeva.com, pgsql-bugs@lists.postgresql.org Date: Wed, 08 Oct 2025 10:21:16 +0200 In-Reply-To: <19076-4d897c04a44a9627@postgresql.org> References: <19076-4d897c04a44a9627@postgresql.org> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.56.2 (3.56.2-2.fc42) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 2025-10-07 at 20:32 +0000, PG Bug reporting form wrote: > PostgreSQL version: 17.6 > Operating system: macOS 15.6.1 (observed on other Linux based OS) >=20 > I have an application which uses a database driver that creates prepared > statements. I am noticing that for some queries, the first 5 executions > after the database connection is created are performant. After this, the > prepared statement uses the generic query plan and is incredibly slow. Th= is > is even if I do not change the parameters and simple re-run the same quer= y > several times with the same arguments. I have tried re-running ANALYZE on > the tables and setting STATISTICS to 1000 on the rows in question with no > luck. Here is what my query looks like: >=20 > SELECT accounts_contacts.account_id, count(contacts.id) AS count > FROM accounts_contacts JOIN contacts ON contacts.id =3D > accounts_contacts.contact_id > WHERE accounts_contacts.account_id IN (...) AND contacts.tenant_id = =3D > $1::UUID AND contacts.status =3D $2::VARCHAR > GROUP BY accounts_contacts.account_id >=20 > Here is the EXPLAIN output from the first 5 executions using the custom > query plan: >=20 > [hash join] > > Here is the EXPLAIN output from executions 6+ using the generic query pla= n: > > [nested loop join with over 800000 iterations] I don't think that is a bug. The logic that decides whether to use a generic plan or not is a heuristic, and heuristics are bound to get it wrong occasionally. I'd guess that the first five executions happened to use values that had few "contacts". Either don't use a prepared statement for this statement, or make sure that "plan_cache_mode" is set to "force_custom_plan" for that single prepared statement. Yours, Laurenz Albe