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 1mN85N-0001dG-PS for pgsql-general@arkaria.postgresql.org; Mon, 06 Sep 2021 06:26:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mN85L-0002Dh-Ts for pgsql-general@arkaria.postgresql.org; Mon, 06 Sep 2021 06:26:27 +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 1mN85L-0002DY-9R for pgsql-general@lists.postgresql.org; Mon, 06 Sep 2021 06:26:27 +0000 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mN85F-0006xY-F5 for pgsql-general@lists.postgresql.org; Mon, 06 Sep 2021 06:26:25 +0000 Received: by mail-wm1-x32e.google.com with SMTP id v20-20020a1cf714000000b002e71f4d2026so3151822wmh.1 for ; Sun, 05 Sep 2021 23:26:20 -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:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=ETk4wYD0t9s/sZUgKT0/ytYZoGsOIvOIaXmzwrZUjEk=; b=tsLM06bWrV278pYkqQ5hftkjA6/zGVQ2CW02mdCba2Wd9Fz4ac38d3XGCojoGFU5Gc 2MLTKHXnXFihZpbwob0oQKKvVNmnkc6v2LOGfOQkDMweYcVcPjc4xm8J94dAhAN5TjIT pAPQQfmdIWGbuE0MjGhnZvpkzRv7BHUoXE7qzO8d1cEnaqmny5ZElcjtFJeqlxCBcQq2 0oKS084RTZMrq1lkD49/0f1J586fBlLJZT5brY5loueFwGLZ5ShIxidSiYNVx+O3SepY 3jtavE2qG3kt0nEYJdDchbj9og4CCJMkgopaHlCaQv/QnwGif0AcJHPCkBLSdphng1I5 5/EA== 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=ETk4wYD0t9s/sZUgKT0/ytYZoGsOIvOIaXmzwrZUjEk=; b=UjuVtccRxMBm/+FxxUhOGvsBCH8cGBrCyp94fzLFp6+GJmMgIpG4LAojNx6P89yDVf eJMh8JzsZrRziZuMyuhU1OfrKfSQA7klJ7zJRYnVgCKT6ey6JAAWYQSMd3uJaKO9A206 zfvv/oiZiW0keZzi0kNvxQtaVmjuQi8yfPpI0DfA/QNkyPAQjKadE+a21Dll+fEYJhR/ EP0IQ18laggmxAFJCReS/EYsVRY4LhlBOLXMd21nPJqVIgofnxGq4K0T89uHNecXuwpP 3In4E32E/gWCyOfijmzigBk0Zq5QTDUgyx9Mq4Wo6LmaZPGj/SYOHrs2incgzus28/dZ 1uiQ== X-Gm-Message-State: AOAM533vE6Cil5L6Hid9jsad8OmNkGjSbJkI495l/sQ/wldNTDGJlM7e RsBKYlETap5ZrKFta3NNajlDjF/avGj7Jw== X-Google-Smtp-Source: ABdhPJzdYl5zh12gOjZZPkLts2TrgMEz4XZQrfWeVUQvAjRIzLmeqqnhf+9PIv9v3DbxbR+jmYIe3A== X-Received: by 2002:a7b:c959:: with SMTP id i25mr9507960wml.143.1630909578815; Sun, 05 Sep 2021 23:26:18 -0700 (PDT) Received: from localhost.localdomain (dynamic-e2fcj9ch68fod91kp-pd01.res.v6.highway.a1.net. [2001:871:5c:9bf4:adfc:f0e1:43e0:ed39]) by smtp.gmail.com with ESMTPSA id v13sm7131876wrf.55.2021.09.05.23.26.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Sep 2021 23:26:18 -0700 (PDT) Message-ID: <3968ca671180b32b8199b2a1c1fbf9a43fa14d60.camel@cybertec.at> Subject: Re: How to observe plan_cache_mode transition from custom to generic plan? From: Laurenz Albe To: Mladen Gogala , pgsql-general@lists.postgresql.org Date: Mon, 06 Sep 2021 08:26:18 +0200 In-Reply-To: <0f9c9510-f4f3-0388-b382-36063f0d4e36@gmail.com> References: <0f9c9510-f4f3-0388-b382-36063f0d4e36@gmail.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.40.4 (3.40.4-1.fc34) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sun, 2021-09-05 at 14:20 -0400, Mladen Gogala wrote: > On 9/5/21 9:35 AM, Richard Michael wrote: > > TL;DR -- > > > > 1/ My basic prepared statement doesn't seem to start out with a custom > > plan > > (i.e., no parameter symbols in query text). That is notmal for the first five executions. PostgreSQL will consider a generic plan only from the sixth execution on. > > 2/ EXPLAIN EXECUTE of prepared statement doesn't show query text, as the > > documentation seems to imply.  Should it? Yes, and it does for me - see below. > > 3/ How can I observe the effect of plan_cache_mode? > >     e.g., Possible plan change from custom to generic By checking for the presence of $1, $2 and the like in the EXPLAIN output. Here is a sample session that shows all that in action: PREPARE stmt(integer) AS SELECT * FROM jobs WHERE job_id = $1; EXPLAIN EXECUTE stmt(1); QUERY PLAN ═════════════════════════════════════════════════════ Seq Scan on jobs (cost=0.00..1.04 rows=1 width=10) Filter: (job_id = 1) (2 rows) EXPLAIN EXECUTE stmt(2); QUERY PLAN ═════════════════════════════════════════════════════ Seq Scan on jobs (cost=0.00..1.04 rows=1 width=10) Filter: (job_id = 2) (2 rows) EXPLAIN EXECUTE stmt(3); QUERY PLAN ═════════════════════════════════════════════════════ Seq Scan on jobs (cost=0.00..1.04 rows=1 width=10) Filter: (job_id = 3) (2 rows) EXPLAIN EXECUTE stmt(4); QUERY PLAN ═════════════════════════════════════════════════════ Seq Scan on jobs (cost=0.00..1.04 rows=1 width=10) Filter: (job_id = 4) (2 rows) EXPLAIN EXECUTE stmt(5); QUERY PLAN ═════════════════════════════════════════════════════ Seq Scan on jobs (cost=0.00..1.04 rows=1 width=10) Filter: (job_id = 5) (2 rows) EXPLAIN EXECUTE stmt(6); QUERY PLAN ═════════════════════════════════════════════════════ Seq Scan on jobs (cost=0.00..1.04 rows=1 width=10) Filter: (job_id = $1) (2 rows) As you see, the sixth execution uses a generic plan. > > 2/ The query text was logged by `auto_explain`.  However, it contains a > > parameter symbol; why?  Also, why is the logged query a `PREPARE` > > statement > > (as if a new prepared statement is being created), instead of only the > > `SELECT ..` which was executed? You saw the log entry for the PREPARE statement. Since your statement contains $1 in its query text, you'll get that logged. Look for the log entries for the EXECUTEs. By the way, here is one of the shortcomings of using prepared statements in my opinion: if EXECUTE is slow, you will get the EXECUTE logged, but not the statement text of the prepared statements. > Well, some other databases that shall remain unnamed have a thing called > "bind variable peeking". [more off-topic things skipped] Most of your explanations about PostgreSQL are correct, but: > Postgres has a different method: it executes the same statement with 5 > different sets of bind variables and if it finds a plan that is cheaper > than the generic plan, it caches it and uses it in the future. That is wrong: If the estimate for the generic plan is more expensive than the plans for the first five executions, it will keep generating a custom plan for subsequent executions of the prepared statement. > People switching from Oracle, me being one of those, frequently make > mistake of using bind variables in Postgres. "Bind variables" just being an Oraclism for parameters, it is *not* a mistake to use them in PostgreSQL. Yours, Laurenz Albe