{"id":12192,"date":"2019-01-11T06:29:44","date_gmt":"2019-01-11T05:29:44","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\/"},"modified":"2019-01-11T06:29:44","modified_gmt":"2019-01-11T05:29:44","slug":"postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\/","title":{"rendered":"PostgreSQL 12, pg_stat_statements_reset for userid, queryid and dbid"},"content":{"rendered":"<p>PostgreSQL 12 will give you more control on resetting statistics gathered by <a href=\"https:\/\/www.postgresql.org\/docs\/current\/pgstatstatements.html\" target=\"_blank\" rel=\"noopener noreferrer\">pg_stat_statements<\/a>. When you check the documentation for PostgreSQL 11 (as linked in the previous sentence) you will see that the function has the following signature:<\/p>\n<p><!--more--><\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">\npg_stat_statements_reset() returns void\n<\/pre>\n<p>This means your only choice is to reset all the statistics. Today <a href=\"https:\/\/git.postgresql.org\/pg\/commitdiff\/43cbedab8ff1eef4088807ffc1a64a107de67af6\" target=\"_blank\" rel=\"noopener noreferrer\">this commit<\/a> landed and this will give you more control on which statistics to reset. The signature of the function now looks like this:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">\npg_stat_statements_reset(userid Oid, dbid Oid, queryid bigint) returns void\n<\/pre>\n<p>There are three new parameters for controlling what to reset: The user id, the database id and the id of a specific query. By default all of them are 0 meaning the the function will behave as in previous versions: Discarding all the statistics. Lets create two users, two databases and a table in each so we will have something in pg_stat_statements we can work with:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">\npostgres@pgbox:\/u02\/pgdata\/DEV\/ [PGDEV] psql -c \"create user u1 with login password 'u1'\" postgres\nCREATE ROLE\npostgres@pgbox:\/u02\/pgdata\/DEV\/ [PGDEV] psql -c \"create user u2 with login password 'u2'\" postgres\nCREATE ROLE\npostgres@pgbox:\/u02\/pgdata\/DEV\/ [PGDEV] psql -c \"create database db1 with owner = u1\" postgres\nCREATE DATABASE\npostgres@pgbox:\/u02\/pgdata\/DEV\/ [PGDEV] psql -c \"create database db2 with owner = u2\" postgres\nCREATE DATABASE\npostgres@pgbox:\/u02\/pgdata\/DEV\/ [PGDEV] psql -c \"create table t1 (a int)\" -U u1 db1\nCREATE TABLE\npostgres@pgbox:\/u02\/pgdata\/DEV\/ [PGDEV] psql -c \"create table t1 (a int)\" -U u2 db2\nCREATE TABLE\npostgres@pgbox:\/u02\/pgdata\/DEV\/ [PGDEV] psql -c \"insert into t1 select * from generate_series(1,100)\" -U u1 db1\nINSERT 0 100\npostgres@pgbox:\/u02\/pgdata\/DEV\/ [PGDEV] psql -c \"insert into t1 select * from generate_series(1,100)\" -U u2 db2\nINSERT 0 100\npostgres@pgbox:\/u02\/pgdata\/DEV\/ [PGDEV] psql -c \"select count(*) from t1\" -U u1 db1\n count \n-------\n   100\n(1 row)\npostgres@pgbox:\/u02\/pgdata\/DEV\/ [PGDEV] psql -c \"select count(*) from t1\" -U u2 db2\n count \n-------\n   100\n(1 row)\n<\/pre>\n<p>We should be able to see the statements in pg_stat_statements but before doing that lets check the dbids:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">\npostgres@pgbox:\/home\/postgres\/ [PGDEV] oid2name \nAll databases:\n    Oid  Database Name  Tablespace\n----------------------------------\n  16394            db1  pg_default\n  16395            db2  pg_default\n  13569       postgres  pg_default\n  13568      template0  pg_default\n      1      template1  pg_default\n<\/pre>\n<p>What do we see for our two databases?<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# select userid,dbid,queryid,calls,query from pg_stat_statements where dbid in (16394,16395);\n userid | dbid  |       queryid        | calls |                        query                        \n--------+-------+----------------------+-------+-----------------------------------------------------\n  16392 | 16394 |  7490503619681577402 |     3 | set client_encoding to 'unicode'\n  16393 | 16395 |   843119317166481275 |     1 | insert into t1 select * from generate_series($1,$2)\n  16392 | 16394 | -3672942776844552312 |     1 | insert into t1 select * from generate_series($1,$2)\n  16393 | 16395 |  7490503619681577402 |     3 | set client_encoding to 'unicode'\n  16392 | 16394 |  5583984467630386743 |     1 | select count(*) from t1\n  16393 | 16395 |  4983979802666994390 |     1 | select count(*) from t1\n  16393 | 16395 |  6842879890091936614 |     1 | create table t1 (a int)\n  16392 | 16394 |  6842879890091936614 |     1 | create table t1 (a int)\n<\/pre>\n<p>We should be able to reset the statistics for a specific query:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# select userid,dbid,queryid,calls,query from pg_stat_statements where dbid in (16394,16395) and queryid = 6842879890091936614;\n userid | dbid  |       queryid       | calls |          query          \n--------+-------+---------------------+-------+-------------------------\n  16393 | 16395 | 6842879890091936614 |     1 | create table t1 (a int)\n  16392 | 16394 | 6842879890091936614 |     1 | create table t1 (a int)\n(2 rows)\npostgres=# select pg_stat_statements_reset(0, 0, 6842879890091936614);\n pg_stat_statements_reset \n--------------------------\n \n(1 row)\n\npostgres=# select userid,dbid,queryid,calls,query from pg_stat_statements where dbid in (16394,16395) and queryid = 6842879890091936614;\n userid | dbid | queryid | calls | query \n--------+------+---------+-------+-------\n(0 rows)\n<\/pre>\n<p>Notice that this of course resets the statistics for both statements as they have the same queryid. You could specify the userid and\/or dbid as well to reset just one of them. Nice new feature.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PostgreSQL 12 will give you more control on resetting statistics gathered by pg_stat_statements. When you check the documentation for PostgreSQL 11 (as linked in the previous sentence) you will see that the function has the following signature:<\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[229],"tags":[77],"type_dbi":[],"class_list":["post-12192","post","type-post","status-publish","format-standard","hentry","category-database-administration-monitoring","tag-postgresql"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v28.8 (Yoast SEO v28.8) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>PostgreSQL 12, pg_stat_statements_reset for userid, queryid and dbid - dbi Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL 12, pg_stat_statements_reset for userid, queryid and dbid\" \/>\n<meta property=\"og:description\" content=\"PostgreSQL 12 will give you more control on resetting statistics gathered by pg_stat_statements. When you check the documentation for PostgreSQL 11 (as linked in the previous sentence) you will see that the function has the following signature:\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-11T05:29:44+00:00\" \/>\n<meta name=\"author\" content=\"Open source Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Open source Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\\\/\"},\"author\":{\"name\":\"Open source Team\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\"},\"headline\":\"PostgreSQL 12, pg_stat_statements_reset for userid, queryid and dbid\",\"datePublished\":\"2019-01-11T05:29:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\\\/\"},\"wordCount\":226,\"commentCount\":0,\"keywords\":[\"PostgreSQL\"],\"articleSection\":[\"Database Administration &amp; Monitoring\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\\\/\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\\\/\",\"name\":\"PostgreSQL 12, pg_stat_statements_reset for userid, queryid and dbid - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\"},\"datePublished\":\"2019-01-11T05:29:44+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL 12, pg_stat_statements_reset for userid, queryid and dbid\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\",\"name\":\"Open source Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"caption\":\"Open source Team\"},\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/author\\\/open-source-team\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"PostgreSQL 12, pg_stat_statements_reset for userid, queryid and dbid - dbi Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL 12, pg_stat_statements_reset for userid, queryid and dbid","og_description":"PostgreSQL 12 will give you more control on resetting statistics gathered by pg_stat_statements. When you check the documentation for PostgreSQL 11 (as linked in the previous sentence) you will see that the function has the following signature:","og_url":"https:\/\/www.dbi-services.com\/blog\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\/","og_site_name":"dbi Blog","article_published_time":"2019-01-11T05:29:44+00:00","author":"Open source Team","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Open source Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\/"},"author":{"name":"Open source Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202"},"headline":"PostgreSQL 12, pg_stat_statements_reset for userid, queryid and dbid","datePublished":"2019-01-11T05:29:44+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\/"},"wordCount":226,"commentCount":0,"keywords":["PostgreSQL"],"articleSection":["Database Administration &amp; Monitoring"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\/","url":"https:\/\/www.dbi-services.com\/blog\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\/","name":"PostgreSQL 12, pg_stat_statements_reset for userid, queryid and dbid - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2019-01-11T05:29:44+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-12-pg_stat_statements_reset-for-userid-queryid-and-dbid\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL 12, pg_stat_statements_reset for userid, queryid and dbid"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202","name":"Open source Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","caption":"Open source Team"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/open-source-team\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/12192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=12192"}],"version-history":[{"count":0,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/12192\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=12192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=12192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=12192"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=12192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}