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.96) (envelope-from ) id 1x0w87-004tWZ-29 for pgsql-hackers@arkaria.postgresql.org; Mon, 31 Aug 2026 07:08:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x0w86-00Fzgn-20 for pgsql-hackers@arkaria.postgresql.org; Mon, 31 Aug 2026 07:08:30 +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.96) (envelope-from ) id 1x0w86-00Fzge-0I for pgsql-hackers@lists.postgresql.org; Mon, 31 Aug 2026 07:08:30 +0000 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1x0w84-00000003GLG-0Ayp for pgsql-hackers@postgresql.org; Mon, 31 Aug 2026 07:08:29 +0000 Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-39682983a0fso3896297a91.3 for ; Mon, 31 Aug 2026 00:08:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1788160105; x=1788764905; darn=postgresql.org; h=to:cc:date:message-id:subject:mime-version:content-type:from:from :to:cc:subject:date:message-id:reply-to:content-type; bh=WFtKuaMQu4urAgrj2u0Gh/8/I2rsdhznbyHBLaLhbFQ=; b=GYsTGdfqI8P9A+77isNrFxJjUtH8HLCzd9IoJfkTC0qPKjF2Tx9lnehCRLhmeUmCoA y3aMBPftirfdEBf5RPZm5IaAJm1WFyB1Xn2JIQ6XM27XA1MBDnwVEfwtFL6q0xbcnC34 PsPXOz4UyWl4Mn4ixYzdjcSOLkRjVpkuXcsMWMqBB3W/cFKEF3J+Rl/nywELMR0XTsLo czeJGQ0R/C46sQQfaxYy44rJWPaIfeHEiONLhJ2WvXqHxgMo2h+0s4GWerWgeUXlacP8 RRfGre2nKd+rV0ak+8OgNulxjIN+QbFS7tr1cuWN9p49g4vsCVOSRnEPmXHshL9uVjld 4QrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788160105; x=1788764905; h=to:cc:date:message-id:subject:mime-version:content-type:from :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to:content-type; bh=WFtKuaMQu4urAgrj2u0Gh/8/I2rsdhznbyHBLaLhbFQ=; b=RArZ+LOo6UgexWCSXPMxZz7XZ5C0ySjYHtmcShBmPAqMUAxQbCUzNy2+jtNv1iY4k7 kt3w63XnfurFOuuhG0bspPmWk+l5vkayLZTvTIw7wsUd+xZZ4frB6f4h+JAekoCHULmG lTfpGkkFD5kol8h3Bnno5Y09qeMENTMSWoqYtuxDlrq7Sejm9oojN5bwi/WkAgcc7xB+ tAGro2UMt2ZTOP4EQBETNBMy/23tzPl5RqlIhOZIux46FtzP8NtLjm/AbgoBQRSOtN3M 59M2XdcOcPAVIYfZ2XEM4/7Hh+q/rc1j3ClQkFuq0rhAazE3dzo5yqcuRUDfBBObozJT Fy+w== X-Gm-Message-State: AFuF++nw689IFH0rBftV69ACwiCbiLDOwMfX5BQlMYclVy4nUzuHhNHT DJamd9z+kIz63tWgym0s0zDi47a0nxDgqvT7x6AVYO27wlf7iE2qb05u3csnUQ== X-Gm-Gg: AYBFou0/2qhHdsEouSjSuFckCmyWUZ40Us57Irdhxbw6PokRXBQ7XvxF2MXhBQb/AAQ /gC1wc15FJRR8Qb7aJ1/RDfRK1mQs2ySH6qOJFl8m+pMtfcJtXf0R5XmEHFAE+dS/GLg1MMITzg 8uyb04FzFlPrgM2c/5ifnh3bTCjkwDK19sAgzBMbcP4BzvMULPIDi7nzOjjvXCS7A/NlM3Bd2Ax iMS4TS7N8w0SwIeMNnhm5wTVbSJx077R1AHq+5XxDh1oqK+FlyJg1fN+RBSfs4ZNX7cFvmwI8pT HgCu0YLUcHeFOCJpsuugEI5IUObrXTTNAmGRYae2O11jxZZgfouwBXwNu+vdVk3PPOLl8i7HD9a xIb+OrmQ8MT1ADY408J/19H4/wSIcWmkbULwUgDSF3QVpPISKSNiZuV5IBbGhzyqn18tQl+BXFb xFcrm9b6w5VyTMSuknHB3sib9JdcLX8RUfbePzBCAH+96w4raq/w/kf64iK4o9+mXjegoWFw== X-Received: by 2002:a17:90b:2243:b0:398:9bd5:490d with SMTP id 98e67ed59e1d1-3989bd54c26mr16340635a91.20.1788160105134; Mon, 31 Aug 2026 00:08:25 -0700 (PDT) Received: from smtpclient.apple ([185.135.79.161]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-398d8e493b0sm6428793a91.16.2026.08.31.00.08.22 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 31 Aug 2026 00:08:24 -0700 (PDT) From: Chao Li Content-Type: multipart/mixed; boundary="Apple-Mail=_4B874641-EA28-4144-AB47-98B9F845010F" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.700.51.1.1\)) Subject: Fix pg_stat_statements losing normalized query text after reset Message-Id: <29410A4B-9E7B-4557-A289-B8AB6299707F@gmail.com> Date: Mon, 31 Aug 2026 15:07:47 +0800 Cc: chong.peng@enmotech.com, Lukas Fittl To: PostgreSQL-development X-Mailer: Apple Mail (2.3864.700.51.1.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_4B874641-EA28-4144-AB47-98B9F845010F Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Hi, I received this potential bug report from Chong Peng, who is in the CC = list. PG10 introduced a feature: ``` =E2=80=A2 Show ignored constants as $N rather than ? in = pg_stat_statements ``` The behavior is like: ``` evantest=3D# SELECT pg_stat_statements_reset(); pg_stat_statements_reset ------------------------------- 2026-08-31 10:14:17.326521+08 (1 row) evantest=3D# SELECT oid FROM pg_class WHERE relname in ($1,$2,$3) \parse = stmt1 evantest=3D# \bind_named stmt1 'not_exist_tab1' 'not_exist_tab2' = 'pg_class' \g oid ------ 1259 (1 row) evantest=3D# SELECT queryid,query FROM pg_stat_statements; queryid | query = ----------------------+---------------------------------------------------= -------- -6242574738340340947 | SELECT oid FROM pg_class WHERE relname in ($1 = /*, ... */) 7506118397046287281 | SELECT pg_stat_statements_reset() (2 rows) ``` In the pg_stat_statements output, it shows =E2=80=9C$1 /*, =E2=80=A6*/=E2=80= =9C rather than =E2=80=9C$1, $2, $3=E2=80=9D. But the problem is that, after pg_stat_statements_reset(), the display = becomes =E2=80=9C$1, $2, $3=E2=80=9D: ``` evantest=3D# SELECT pg_stat_statements_reset(); pg_stat_statements_reset ------------------------------- 2026-08-31 10:16:43.419045+08 (1 row) evantest=3D# SELECT queryid,query FROM pg_stat_statements; queryid | query ---------------------+----------------------------------- 7506118397046287281 | SELECT pg_stat_statements_reset() (1 row) evantest=3D# \bind_named stmt1 'pg_class' 'pg_attribute' 'pg_index' \g oid ------ 1249 1259 2610 (3 rows) evantest=3D# SELECT queryid,query FROM pg_stat_statements; queryid | query = ----------------------+---------------------------------------------------= --- -6242574738340340947 | SELECT oid FROM pg_class WHERE relname in = ($1,$2,$3) 7506118397046287281 | SELECT pg_stat_statements_reset() -3049491949917800185 | SELECT queryid,query FROM pg_stat_statements (3 rows) ``` =46rom my debugging, I think the problem is that entry_reset() deletes = the entry from pgss_hash via SINGLE_ENTRY_RESET, while the prepared = statement itself remains cached and can be executed again without going = through parse analysis. When bind_named executes the prepared statement again, = pgss_ExecutorEnd() calls pgss_store(). Since the original entry has = already been removed from pgss_hash, pgss_store() has to create a new = entry. At this point there is no JumbleState available to regenerate the = normalized query text, so the entry is recreated using the original = query string, and the squashed representation is lost. The question is whether pg_stat_statements_reset() really needs to = delete the hash entry in this case? It feels to me that, as long as a = prepared statement remains cached and can still be executed without = being parsed again, it might be reasonable for pgss to retain the = existing hash entry, particularly its representative normalized query = text, while resetting its statistics. The currently executing pg_stat_statements_reset() statement itself is = an exception. Its old entry needs to be removed so that the current = invocation can establish a fresh entry after the reset. There is another case to consider: after a reset, the same query ID = might be parsed again with a different representative query text. In = that case, the retained sticky entry should be updated with the newly = normalized query text, rather than continuing to use the text retained = from before the reset. Based on this understanding, I made a fix that resets existing entries = back to the sticky state rather than deleting them. This preserves the = normalized query text for cached prepared statements. If a query with = the same query ID is parsed again later, the sticky entry is updated = with the newly normalized query text. I=E2=80=99m not very familiar with = the history and design considerations around pg_stat_statements_reset(), = so I don=E2=80=99t have full confidence that retaining the entries is = the intended approach, so comments are welcome. See the attached patch for details. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/ --Apple-Mail=_4B874641-EA28-4144-AB47-98B9F845010F Content-Disposition: attachment; filename=v1-0001-pg_stat_statements-preserve-normalized-query-text.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v1-0001-pg_stat_statements-preserve-normalized-query-text.patch" Content-Transfer-Encoding: quoted-printable =46rom=20ba5e3f9638ccafb7a7042bc855eccef756dc7e0c=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20"Chao=20Li=20(Evan)"=20=0A= Date:=20Mon,=2031=20Aug=202026=2014:53:41=20+0800=0ASubject:=20[PATCH=20= v1]=20pg_stat_statements:=20preserve=20normalized=20query=20text=20= across=0A=20reset=0A=0Apg_stat_statements_reset()=20removes=20entries=20= from=20the=20shared=20hash=20table.=0AHowever,=20a=20prepared=20= statement=20can=20remain=20cached=20and=20be=20executed=20again=0A= without=20going=20through=20parse=20analysis.=0A=0AIn=20that=20case,=20= pgss_ExecutorEnd()=20recreates=20the=20missing=20entry=20without=20a=0A= JumbleState,=20so=20the=20representative=20query=20text=20is=20taken=20= from=20the=20original=0Aquery=20string=20instead=20of=20being=20= normalized=20again.=20=20This=20can=20make=20squashed=0Aquery=20text=20= such=20as=0A=0A```=0A$1=20/*,=20...=20*/=0A```=0A=0Areappear=20as=0A=0A= ```=0A$1,=20$2,=20$3=0A```=0A=0Aafter=20a=20reset.=0A=0AInstead=20of=20= removing=20entries=20during=20reset,=20reset=20their=20statistics=20and=0A= leave=20them=20as=20sticky=20entries=20so=20that=20cached=20prepared=20= statements=20can=0Acontinue=20to=20use=20the=20existing=20normalized=20= query=20text.=20=20The=20entry=20for=20the=0Astatement=20performing=20= the=20reset=20is=20still=20removed=20so=20that=20the=20current=0A= invocation=20can=20establish=20a=20fresh=20entry.=0A=0AIf=20a=20query=20= with=20the=20same=20query=20ID=20is=20parsed=20again=20after=20the=20= reset,=20update=0Athe=20sticky=20entry=20with=20the=20newly=20normalized=20= representative=20query=20text.=0A=0AAlso=20track=20whether=20the=20= currently=20executing=20statement=20is=20top-level=20so=0Athat=20nested=20= reset=20statements=20are=20handled=20correctly.=0A=0AAdd=20regression=20= coverage=20for=20cached=20custom=20and=20generic=20plans,=20reparsing=0A= the=20same=20query=20ID=20after=20reset,=20and=20nested=20reset=20= statements.=0A=0AReported-by:=20Chong=20Peng=20=0A= Author:=20Chao=20Li=20=0A---=0A=20= .../pg_stat_statements/expected/squashing.out=20|=20=2096=20++++++++++=0A= =20.../pg_stat_statements/pg_stat_statements.c=20=20=20|=20170=20= ++++++++++++++----=0A=20contrib/pg_stat_statements/sql/squashing.sql=20=20= |=20=2040=20+++++=0A=203=20files=20changed,=20269=20insertions(+),=2037=20= deletions(-)=0A=0Adiff=20--git=20= a/contrib/pg_stat_statements/expected/squashing.out=20= b/contrib/pg_stat_statements/expected/squashing.out=0Aindex=20= 8438235a2ce..4347b069b91=20100644=0A---=20= a/contrib/pg_stat_statements/expected/squashing.out=0A+++=20= b/contrib/pg_stat_statements/expected/squashing.out=0A@@=20-129,6=20= +129,102=20@@=20SELECT=20query,=20calls=20FROM=20pg_stat_statements=20= ORDER=20BY=20query=20COLLATE=20"C";=0A=20=20SELECT=20= pg_stat_statements_reset()=20IS=20NOT=20NULL=20AS=20t=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20|=20=20=20=20=201=0A=20(3=20rows)=0A=20= =0A+--=20Cached=20prepared=20statements=20retain=20normalized=20text=20= across=20a=20reset=0A+SET=20plan_cache_mode=20=3D=20force_custom_plan;=0A= +SELECT=20*=20FROM=20test_squash=20WHERE=20id=20IN=20($1,=20$2,=20$3)=20= \parse=20p1=0A+\bind_named=20p1=201=202=203=20\g=0A+=20id=20|=20data=20=0A= +----+------=0A+(0=20rows)=0A+=0A+SELECT=20pg_stat_statements_reset()=20= IS=20NOT=20NULL=20AS=20t;=0A+=20t=20=0A+---=0A+=20t=0A+(1=20row)=0A+=0A= +\bind_named=20p1=201=202=203=20\g=0A+=20id=20|=20data=20=0A+----+------=0A= +(0=20rows)=0A+=0A+SELECT=20query,=20calls=20FROM=20pg_stat_statements=0A= +=20=20WHERE=20query=20LIKE=20'SELECT=20*=20FROM=20test_squash=20WHERE=20= id=20IN%';=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20query=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20|=20calls=20=0A= +-------------------------------------------------------+-------=0A+=20= SELECT=20*=20FROM=20test_squash=20WHERE=20id=20IN=20($1=20/*,=20...=20= */)=20|=20=20=20=20=201=0A+(1=20row)=0A+=0A+--=20Reparse=20the=20= statement=20with=20the=20same=20query=20ID,=20changing=20"IN"=20to=20= "in"=20to=20show=0A+--=20that=20its=20representative=20query=20text=20is=20= updated=0A+SELECT=20pg_stat_statements_reset()=20IS=20NOT=20NULL=20AS=20= t;=0A+=20t=20=0A+---=0A+=20t=0A+(1=20row)=0A+=0A+select=20*=20from=20= test_squash=20where=20id=20in=20($1,=20$2,=20$3)=20\parse=20p2=0A= +\bind_named=20p2=201=202=203=20\g=0A+=20id=20|=20data=20=0A+----+------=0A= +(0=20rows)=0A+=0A+SELECT=20query,=20calls=20FROM=20pg_stat_statements=0A= +=20=20WHERE=20query=20LIKE=20'select=20*=20from=20test_squash=20where=20= id=20in%';=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20query=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20|=20calls=20=0A= +-------------------------------------------------------+-------=0A+=20= select=20*=20from=20test_squash=20where=20id=20in=20($1=20/*,=20...=20= */)=20|=20=20=20=20=201=0A+(1=20row)=0A+=0A+\close_prepared=20p2=0A+SET=20= plan_cache_mode=20=3D=20force_generic_plan;=0A+\bind_named=20p1=201=202=20= 3=20\g=0A+=20id=20|=20data=20=0A+----+------=0A+(0=20rows)=0A+=0A+SELECT=20= pg_stat_statements_reset()=20IS=20NOT=20NULL=20AS=20t;=0A+=20t=20=0A+---=0A= +=20t=0A+(1=20row)=0A+=0A+\bind_named=20p1=201=202=203=20\g=0A+=20id=20|=20= data=20=0A+----+------=0A+(0=20rows)=0A+=0A+SELECT=20query,=20calls=20= FROM=20pg_stat_statements=0A+=20=20WHERE=20query=20LIKE=20'select=20*=20= from=20test_squash=20where=20id=20in%';=0A+=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20query=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20|=20calls=20=0A= +-------------------------------------------------------+-------=0A+=20= select=20*=20from=20test_squash=20where=20id=20in=20($1=20/*,=20...=20= */)=20|=20=20=20=20=201=0A+(1=20row)=0A+=0A+\close_prepared=20p1=0A= +RESET=20plan_cache_mode;=0A+--=20A=20nested=20statement=20performing=20= a=20reset=20stores=20its=20current=20query=20text=0A+SET=20= pg_stat_statements.track=20=3D=20'all';=0A+CREATE=20FUNCTION=20= pgss_nested_reset()=20RETURNS=20void=20AS=20$$=0A+BEGIN=0A+=20=20PERFORM=20= pg_stat_statements_reset(0,=200,=200);=0A+END;=0A+$$=20LANGUAGE=20= plpgsql;=0A+SELECT=20pgss_nested_reset();=0A+=20pgss_nested_reset=20=0A= +-------------------=0A+=20=0A+(1=20row)=0A+=0A+SELECT=20query,=20= toplevel=20FROM=20pg_stat_statements=0A+=20=20WHERE=20query=20LIKE=20= 'SELECT=20pg_stat_statements_reset%';=0A+=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20query=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20|=20toplevel=20=0A= +------------------------------------------+----------=0A+=20SELECT=20= pg_stat_statements_reset(0,=200,=200)=20|=20f=0A+(1=20row)=0A+=0A+DROP=20= FUNCTION=20pgss_nested_reset();=0A+RESET=20pg_stat_statements.track;=0A=20= --=20prepared=20statements=20will=20also=20be=20squashed=0A=20--=20the=20= IN=20and=20ARRAY=20forms=20of=20this=20statement=20will=20have=20the=20= same=20queryId=0A=20SELECT=20pg_stat_statements_reset()=20IS=20NOT=20= NULL=20AS=20t;=0Adiff=20--git=20= a/contrib/pg_stat_statements/pg_stat_statements.c=20= b/contrib/pg_stat_statements/pg_stat_statements.c=0Aindex=20= 4562222c9fc..bee60ffc262=20100644=0A---=20= a/contrib/pg_stat_statements/pg_stat_statements.c=0A+++=20= b/contrib/pg_stat_statements/pg_stat_statements.c=0A@@=20-273,6=20+273,8=20= @@=20static=20const=20ShmemCallbacks=20pgss_shmem_callbacks=20=3D=20{=0A=20= =0A=20/*=20Current=20nesting=20depth=20of=20= planner/ExecutorRun/ProcessUtility=20calls=20*/=0A=20static=20int=09= nesting_level=20=3D=200;=0A+static=20int64=20active_queryid=20=3D=20= INT64CONST(0);=0A+static=20bool=20active_toplevel=20=3D=20false;=0A=20=0A= =20/*=20Saved=20hook=20values=20*/=0A=20static=20= post_parse_analyze_hook_type=20prev_post_parse_analyze_hook=20=3D=20= NULL;=0A@@=20-379,6=20+381,9=20@@=20static=20char=20*qtext_fetch(Size=20= query_offset,=20int=20query_len,=0A=20static=20bool=20= need_gc_qtexts(void);=0A=20static=20void=20gc_qtexts(void);=0A=20static=20= TimestampTz=20entry_reset(Oid=20userid,=20Oid=20dbid,=20int64=20queryid,=20= bool=20minmax_only);=0A+static=20void=20entry_update_sticky(const=20char=20= *query,=20int64=20queryid,=0A+=09=09=09=09=09=09=09=09int=20= query_location,=20int=20query_len,=0A+=09=09=09=09=09=09=09=09const=20= JumbleState=20*jstate);=0A=20static=20char=20= *generate_normalized_query(const=20JumbleState=20*jstate,=0A=20=09=09=09=09= =09=09=09=09=09=20=20=20const=20char=20*query,=0A=20=09=09=09=09=09=09=09= =09=09=20=20=20int=20query_loc,=20int=20*query_len_p);=0A@@=20-854,6=20= +859,17=20@@=20pgss_post_parse_analyze(ParseState=20*pstate,=20Query=20= *query,=20const=20JumbleState=20*jst=0A=20=09=09}=0A=20=09}=0A=20=0A+=09= /*=0A+=09=20*=20A=20sticky=20entry=20may=20preserve=20old=20query=20text=20= for=20a=20cached=20statement=20after=0A+=09=20*=20a=20reset.=20=20Since=20= this=20query=20has=20been=20parsed,=20let=20it=20update=20such=20an=20= entry=0A+=09=20*=20with=20a=20new=20representative=20text.=0A+=09=20*/=0A= +=09entry_update_sticky(pstate->p_sourcetext,=0A+=09=09=09=09=09=09= query->queryId,=0A+=09=09=09=09=09=09query->stmt_location,=0A+=09=09=09=09= =09=09query->stmt_len,=0A+=09=09=09=09=09=09jstate);=0A+=0A=20=09/*=0A=20= =09=20*=20If=20query=20jumbling=20were=20able=20to=20identify=20any=20= ignorable=20constants,=20we=0A=20=09=20*=20immediately=20create=20a=20= hash=20table=20entry=20for=20the=20query,=20so=20that=20we=20can=0A@@=20= -1015,6=20+1031,11=20@@=20pgss_ExecutorStart(QueryDesc=20*queryDesc,=20= int=20eflags)=0A=20static=20void=0A=20pgss_ExecutorRun(QueryDesc=20= *queryDesc,=20ScanDirection=20direction,=20uint64=20count)=0A=20{=0A+=09= int64=09=09saved_queryid=20=3D=20active_queryid;=0A+=09bool=09=09= saved_toplevel=20=3D=20active_toplevel;=0A+=0A+=09active_queryid=20=3D=20= queryDesc->plannedstmt->queryId;=0A+=09active_toplevel=20=3D=20= (nesting_level=20=3D=3D=200);=0A=20=09nesting_level++;=0A=20=09PG_TRY();=0A= =20=09{=0A@@=20-1026,6=20+1047,8=20@@=20pgss_ExecutorRun(QueryDesc=20= *queryDesc,=20ScanDirection=20direction,=20uint64=20count)=0A=20=09= PG_FINALLY();=0A=20=09{=0A=20=09=09nesting_level--;=0A+=09=09= active_queryid=20=3D=20saved_queryid;=0A+=09=09active_toplevel=20=3D=20= saved_toplevel;=0A=20=09}=0A=20=09PG_END_TRY();=0A=20}=0A@@=20-1036,6=20= +1059,11=20@@=20pgss_ExecutorRun(QueryDesc=20*queryDesc,=20ScanDirection=20= direction,=20uint64=20count)=0A=20static=20void=0A=20= pgss_ExecutorFinish(QueryDesc=20*queryDesc)=0A=20{=0A+=09int64=09=09= saved_queryid=20=3D=20active_queryid;=0A+=09bool=09=09saved_toplevel=20=3D= =20active_toplevel;=0A+=0A+=09active_queryid=20=3D=20= queryDesc->plannedstmt->queryId;=0A+=09active_toplevel=20=3D=20= (nesting_level=20=3D=3D=200);=0A=20=09nesting_level++;=0A=20=09PG_TRY();=0A= =20=09{=0A@@=20-1047,6=20+1075,8=20@@=20pgss_ExecutorFinish(QueryDesc=20= *queryDesc)=0A=20=09PG_FINALLY();=0A=20=09{=0A=20=09=09nesting_level--;=0A= +=09=09active_queryid=20=3D=20saved_queryid;=0A+=09=09active_toplevel=20= =3D=20saved_toplevel;=0A=20=09}=0A=20=09PG_END_TRY();=0A=20}=0A@@=20= -2665,12=20+2695,105=20@@=20if=20(e)=20{=20\=0A=20=09}=20\=0A=20=09else=20= \=0A=20=09{=20\=0A-=09=09/*=20Remove=20the=20key=20otherwise=20=20*/=20\=0A= -=09=09hash_search(pgss_hash,=20&e->key,=20HASH_REMOVE,=20NULL);=20\=0A-=09= =09num_remove++;=20\=0A+=09=09if=20(e->key.userid=20=3D=3D=20GetUserId()=20= &&=20\=0A+=09=09=09e->key.dbid=20=3D=3D=20MyDatabaseId=20&&=20\=0A+=09=09= =09e->key.queryid=20=3D=3D=20current_queryid=20&&=20\=0A+=09=09=09= e->key.toplevel=20=3D=3D=20current_toplevel)=20\=0A+=09=09{=20\=0A+=09=09= =09/*=20Let=20the=20statement=20performing=20the=20reset=20store=20its=20= current=20text.=20*/=20\=0A+=09=09=09hash_search(pgss_hash,=20&e->key,=20= HASH_REMOVE,=20NULL);=20\=0A+=09=09}=20\=0A+=09=09else=20\=0A+=09=09{=20= \=0A+=09=09=09/*=20\=0A+=09=09=09=20*=20Keep=20the=20representative=20= query=20text,=20so=20that=20a=20cached=20prepared=20\=0A+=09=09=09=20*=20= statement=20can=20reuse=20it=20without=20being=20parsed=20again.=20=20= With=20no=20calls,=20\=0A+=09=09=09=20*=20the=20entry=20is=20sticky=20= and=20therefore=20hidden=20from=20the=20view.=20\=0A+=09=09=09=20*/=20\=0A= +=09=09=09memset(&e->counters,=200,=20sizeof(Counters));=20\=0A+=09=09=09= e->counters.usage=20=3D=20pgss->cur_median_usage;=20\=0A+=09=09=09= e->stats_since=20=3D=20stats_reset;=20\=0A+=09=09=09= e->minmax_stats_since=20=3D=20stats_reset;=20\=0A+=09=09}=20\=0A+=09=09= num_reset++;=20\=0A=20=09}=20\=0A=20}=0A=20=0A+/*=0A+=20*=20Update=20the=20= representative=20query=20text=20of=20a=20sticky=20entry=20when=20its=20= query=20is=0A+=20*=20parsed=20again.=20=20Cached=20statements=20do=20not=20= reach=20this=20function,=20so=20after=20a=0A+=20*=20reset=20they=20can=20= continue=20to=20use=20the=20representative=20query=20text=20that=20was=0A= +=20*=20saved=20before=20it.=0A+=20*/=0A+static=20void=0A= +entry_update_sticky(const=20char=20*query,=20int64=20queryid,=0A+=09=09=09= =09=09int=20query_location,=20int=20query_len,=0A+=09=09=09=09=09const=20= JumbleState=20*jstate)=0A+{=0A+=09pgssHashKey=20key;=0A+=09pgssEntry=20=20= *entry;=0A+=09char=09=20=20=20*norm_query=20=3D=20NULL;=0A+=09bool=09=09= is_sticky=20=3D=20false;=0A+=09Size=09=09query_offset;=0A+=09int=09=09=09= encoding=20=3D=20GetDatabaseEncoding();=0A+=09bool=09=09stored;=0A+=0A+=09= if=20(queryid=20=3D=3D=20INT64CONST(0))=0A+=09=09return;=0A+=0A+=09query=20= =3D=20CleanQuerytext(query,=20&query_location,=20&query_len);=0A+=0A+=09= memset(&key,=200,=20sizeof(pgssHashKey));=0A+=09key.userid=20=3D=20= GetUserId();=0A+=09key.dbid=20=3D=20MyDatabaseId;=0A+=09key.queryid=20=3D=20= queryid;=0A+=09key.toplevel=20=3D=20(nesting_level=20=3D=3D=200);=0A+=0A= +=09/*=20Avoid=20taking=20the=20exclusive=20lock=20in=20the=20usual=20= case.=20*/=0A+=09LWLockAcquire(&pgss->lock.lock,=20LW_SHARED);=0A+=09= entry=20=3D=20(pgssEntry=20*)=20hash_search(pgss_hash,=20&key,=20= HASH_FIND,=20NULL);=0A+=09if=20(entry)=0A+=09{=0A+=09=09= SpinLockAcquire(&entry->mutex);=0A+=09=09is_sticky=20=3D=20= IS_STICKY(entry->counters);=0A+=09=09SpinLockRelease(&entry->mutex);=0A+=09= }=0A+=09LWLockRelease(&pgss->lock.lock);=0A+=0A+=09if=20(!is_sticky)=0A+=09= =09return;=0A+=0A+=09if=20(jstate=20&&=20jstate->clocations_count=20>=20= 0)=0A+=09=09norm_query=20=3D=20generate_normalized_query(jstate,=20= query,=0A+=09=09=09=09=09=09=09=09=09=09=09=20=20=20query_location,=20= &query_len);=0A+=0A+=09/*=0A+=09=20*=20Update=20the=20text=20while=20= holding=20the=20exclusive=20lock,=20so=20that=20an=20executor=0A+=09=20*=20= cannot=20observe=20the=20entry=20as=20missing=20between=20parse=20and=20= execution.=0A+=09=20*/=0A+=09LWLockAcquire(&pgss->lock.lock,=20= LW_EXCLUSIVE);=0A+=09entry=20=3D=20(pgssEntry=20*)=20= hash_search(pgss_hash,=20&key,=20HASH_FIND,=20NULL);=0A+=09if=20(!entry=20= ||=20!IS_STICKY(entry->counters))=0A+=09=09goto=20done;=0A+=0A+=09stored=20= =3D=20qtext_store(norm_query=20?=20norm_query=20:=20query,=20query_len,=0A= +=09=09=09=09=09=09=20&query_offset,=20NULL);=0A+=09if=20(!stored)=0A+=09= =09goto=20done;=0A+=0A+=09entry->query_offset=20=3D=20query_offset;=0A+=09= entry->query_len=20=3D=20query_len;=0A+=09entry->encoding=20=3D=20= encoding;=0A+=0A+=09if=20(need_gc_qtexts())=0A+=09=09gc_qtexts();=0A+=0A= +done:=0A+=09LWLockRelease(&pgss->lock.lock);=0A+=09if=20(norm_query)=0A= +=09=09pfree(norm_query);=0A+}=0A+=0A=20/*=0A=20=20*=20Reset=20entries=20= corresponding=20to=20parameters=20passed.=0A=20=20*/=0A@@=20-2679,9=20= +2802,10=20@@=20entry_reset(Oid=20userid,=20Oid=20dbid,=20int64=20= queryid,=20bool=20minmax_only)=0A=20{=0A=20=09HASH_SEQ_STATUS=20= hash_seq;=0A=20=09pgssEntry=20=20*entry;=0A-=09FILE=09=20=20=20*qfile;=0A= =20=09int64=09=09num_entries;=0A-=09int64=09=09num_remove=20=3D=200;=0A+=09= int64=09=09num_reset=20=3D=200;=0A+=09int64=09=09current_queryid=20=3D=20= active_queryid;=0A+=09bool=09=09current_toplevel=20=3D=20= active_toplevel;=0A=20=09pgssHashKey=20key;=0A=20=09TimestampTz=20= stats_reset;=0A=20=0A@@=20-2742,47=20+2866,19=20@@=20entry_reset(Oid=20= userid,=20Oid=20dbid,=20int64=20queryid,=20bool=20minmax_only)=0A=20=09=09= }=0A=20=09}=0A=20=0A-=09/*=20All=20entries=20are=20removed?=20*/=0A-=09= if=20(num_entries=20!=3D=20num_remove)=0A+=09/*=20Have=20all=20entries=20= been=20reset?=20*/=0A+=09if=20(num_entries=20!=3D=20num_reset)=0A=20=09=09= goto=20release_lock;=0A=20=0A=20=09/*=0A-=09=20*=20Reset=20global=20= statistics=20for=20pg_stat_statements=20since=20all=20entries=20are=0A-=09= =20*=20removed.=0A+=09=20*=20Reset=20global=20statistics=20for=20= pg_stat_statements=20since=20all=20entries=20have=0A+=09=20*=20been=20= reset.=0A=20=09=20*/=0A=20=09SpinLockAcquire(&pgss->mutex);=0A=20=09= pgss->stats.dealloc=20=3D=200;=0A=20=09pgss->stats.stats_reset=20=3D=20= stats_reset;=0A=20=09SpinLockRelease(&pgss->mutex);=0A=20=0A-=09/*=0A-=09= =20*=20Write=20new=20empty=20query=20file,=20perhaps=20even=20creating=20= a=20new=20one=20to=20recover=0A-=09=20*=20if=20the=20file=20was=20= missing.=0A-=09=20*/=0A-=09qfile=20=3D=20AllocateFile(PGSS_TEXT_FILE,=20= PG_BINARY_W);=0A-=09if=20(qfile=20=3D=3D=20NULL)=0A-=09{=0A-=09=09= ereport(LOG,=0A-=09=09=09=09(errcode_for_file_access(),=0A-=09=09=09=09=20= errmsg("could=20not=20create=20file=20\"%s\":=20%m",=0A-=09=09=09=09=09=09= PGSS_TEXT_FILE)));=0A-=09=09goto=20done;=0A-=09}=0A-=0A-=09/*=20If=20= ftruncate=20fails,=20log=20it,=20but=20it's=20not=20a=20fatal=20problem=20= */=0A-=09if=20(ftruncate(fileno(qfile),=200)=20!=3D=200)=0A-=09=09= ereport(LOG,=0A-=09=09=09=09(errcode_for_file_access(),=0A-=09=09=09=09=20= errmsg("could=20not=20truncate=20file=20\"%s\":=20%m",=0A-=09=09=09=09=09= =09PGSS_TEXT_FILE)));=0A-=0A-=09FreeFile(qfile);=0A-=0A-done:=0A-=09= pgss->extent=20=3D=200;=0A-=09/*=20This=20counts=20as=20a=20query=20text=20= garbage=20collection=20for=20our=20purposes=20*/=0A-=09= record_gc_qtexts();=0A-=0A=20release_lock:=0A=20=09= LWLockRelease(&pgss->lock.lock);=0A=20=0Adiff=20--git=20= a/contrib/pg_stat_statements/sql/squashing.sql=20= b/contrib/pg_stat_statements/sql/squashing.sql=0Aindex=20= fc9e6573873..22b1e8cff28=20100644=0A---=20= a/contrib/pg_stat_statements/sql/squashing.sql=0A+++=20= b/contrib/pg_stat_statements/sql/squashing.sql=0A@@=20-39,6=20+39,46=20= @@=20SELECT=20*=20FROM=20test_squash=20WHERE=20id::text=20=3D=20= ANY(ARRAY[$1,=20$2,=20$3,=20$4,=20$5])=20\bind=0A=20;=0A=20SELECT=20= query,=20calls=20FROM=20pg_stat_statements=20ORDER=20BY=20query=20= COLLATE=20"C";=0A=20=0A+--=20Cached=20prepared=20statements=20retain=20= normalized=20text=20across=20a=20reset=0A+SET=20plan_cache_mode=20=3D=20= force_custom_plan;=0A+SELECT=20*=20FROM=20test_squash=20WHERE=20id=20IN=20= ($1,=20$2,=20$3)=20\parse=20p1=0A+\bind_named=20p1=201=202=203=20\g=0A= +SELECT=20pg_stat_statements_reset()=20IS=20NOT=20NULL=20AS=20t;=0A= +\bind_named=20p1=201=202=203=20\g=0A+SELECT=20query,=20calls=20FROM=20= pg_stat_statements=0A+=20=20WHERE=20query=20LIKE=20'SELECT=20*=20FROM=20= test_squash=20WHERE=20id=20IN%';=0A+=0A+--=20Reparse=20the=20statement=20= with=20the=20same=20query=20ID,=20changing=20"IN"=20to=20"in"=20to=20= show=0A+--=20that=20its=20representative=20query=20text=20is=20updated=0A= +SELECT=20pg_stat_statements_reset()=20IS=20NOT=20NULL=20AS=20t;=0A= +select=20*=20from=20test_squash=20where=20id=20in=20($1,=20$2,=20$3)=20= \parse=20p2=0A+\bind_named=20p2=201=202=203=20\g=0A+SELECT=20query,=20= calls=20FROM=20pg_stat_statements=0A+=20=20WHERE=20query=20LIKE=20= 'select=20*=20from=20test_squash=20where=20id=20in%';=0A+\close_prepared=20= p2=0A+=0A+SET=20plan_cache_mode=20=3D=20force_generic_plan;=0A= +\bind_named=20p1=201=202=203=20\g=0A+SELECT=20= pg_stat_statements_reset()=20IS=20NOT=20NULL=20AS=20t;=0A+\bind_named=20= p1=201=202=203=20\g=0A+SELECT=20query,=20calls=20FROM=20= pg_stat_statements=0A+=20=20WHERE=20query=20LIKE=20'select=20*=20from=20= test_squash=20where=20id=20in%';=0A+\close_prepared=20p1=0A+RESET=20= plan_cache_mode;=0A+=0A+--=20A=20nested=20statement=20performing=20a=20= reset=20stores=20its=20current=20query=20text=0A+SET=20= pg_stat_statements.track=20=3D=20'all';=0A+CREATE=20FUNCTION=20= pgss_nested_reset()=20RETURNS=20void=20AS=20$$=0A+BEGIN=0A+=20=20PERFORM=20= pg_stat_statements_reset(0,=200,=200);=0A+END;=0A+$$=20LANGUAGE=20= plpgsql;=0A+SELECT=20pgss_nested_reset();=0A+SELECT=20query,=20toplevel=20= FROM=20pg_stat_statements=0A+=20=20WHERE=20query=20LIKE=20'SELECT=20= pg_stat_statements_reset%';=0A+DROP=20FUNCTION=20pgss_nested_reset();=0A= +RESET=20pg_stat_statements.track;=0A+=0A=20--=20prepared=20statements=20= will=20also=20be=20squashed=0A=20--=20the=20IN=20and=20ARRAY=20forms=20= of=20this=20statement=20will=20have=20the=20same=20queryId=0A=20SELECT=20= pg_stat_statements_reset()=20IS=20NOT=20NULL=20AS=20t;=0A--=20=0A2.50.1=20= (Apple=20Git-155)=0A=0A= --Apple-Mail=_4B874641-EA28-4144-AB47-98B9F845010F--