{"id":32428,"date":"2024-04-11T09:49:58","date_gmt":"2024-04-11T07:49:58","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/?p=32428"},"modified":"2024-04-11T09:50:01","modified_gmt":"2024-04-11T07:50:01","slug":"postgresql-17-pg_buffercache_evict","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/postgresql-17-pg_buffercache_evict\/","title":{"rendered":"PostgreSQL 17: pg_buffercache_evict()"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In PostgreSQL up to version 16, there is no way to evict the buffer cache except by restarting the instance. In Oracle you can do that since ages with &#8220;<a href=\"https:\/\/docs.oracle.com\/en\/database\/oracle\/oracle-database\/19\/sqlrf\/ALTER-SYSTEM.html\" target=\"_blank\" rel=\"noreferrer noopener\">alter system flush buffer cache<\/a>&#8220;, but not in PostgreSQL. This will <a href=\"https:\/\/git.postgresql.org\/gitweb\/?p=postgresql.git;a=commitdiff;h=13453eedd3f692f8dcf8e334396eee84f00fdde2\" target=\"_blank\" rel=\"noreferrer noopener\">change<\/a> when PostgreSQL 17 will be released later this year. Of course, flushing the buffer cache is nothing you&#8217;d usually like to do in production, but this can be very handy for educational or debugging purposes. This is also the reason why this is intended to be a developer feature.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For getting access to the pg_buffercache_evict function you need to install the <a href=\"https:\/\/www.postgresql.org\/docs\/devel\/pgbuffercache.html\" target=\"_blank\" rel=\"noreferrer noopener\">pg_buffercache<\/a> extension as the function is designed to work over the pg_buffercache view:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; highlight: [1,7,9,16]; title: ; notranslate\" title=\"\">\npostgres=# select version();\n                              version                              \n-------------------------------------------------------------------\n PostgreSQL 17devel on x86_64-linux, compiled by gcc-7.5.0, 64-bit\n(1 row)\n\npostgres=# create extension pg_buffercache;\nCREATE EXTENSION\npostgres=# \\dx\n                      List of installed extensions\n      Name      | Version |   Schema   |           Description           \n----------------+---------+------------+---------------------------------\n pg_buffercache | 1.5     | public     | examine the shared buffer cache\n plpgsql        | 1.0     | pg_catalog | PL\/pgSQL procedural language\n(2 rows)\npostgres=# \\d pg_buffercache \n                 View &quot;public.pg_buffercache&quot;\n      Column      |   Type   | Collation | Nullable | Default \n------------------+----------+-----------+----------+---------\n bufferid         | integer  |           |          | \n relfilenode      | oid      |           |          | \n reltablespace    | oid      |           |          | \n reldatabase      | oid      |           |          | \n relforknumber    | smallint |           |          | \n relblocknumber   | bigint   |           |          | \n isdirty          | boolean  |           |          | \n usagecount       | smallint |           |          | \n pinning_backends | integer  |           |          | \n\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Once the extension is in place, the function is there as well:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; highlight: [1]; title: ; notranslate\" title=\"\">\npostgres=# \\dfS *evict*\n                               List of functions\n Schema |         Name         | Result data type | Argument data types | Type \n--------+----------------------+------------------+---------------------+------\n public | pg_buffercache_evict | boolean          | integer             | func\n(1 row)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">To load something into the buffer cache we&#8217;ll make use of the <a href=\"https:\/\/www.postgresql.org\/docs\/current\/pgprewarm.html\" target=\"_blank\" rel=\"noreferrer noopener\">pre_warm<\/a> extension and completely load the table we&#8217;ll create afterwards:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; highlight: [1,3,5,7,12,17]; title: ; notranslate\" title=\"\">\npostgres=# create extension pg_prewarm;\nCREATE EXTENSION\npostgres=# create table t ( a int, b text );\nCREATE TABLE\npostgres=# insert into t select i, i::text from generate_series(1,10000) i;\nINSERT 0 10000\npostgres=# select pg_prewarm ( &#039;t&#039;, &#039;buffer&#039;, &#039;main&#039;, null, null );\n pg_prewarm \n------------\n         54\n(1 row)\npostgres=# select pg_relation_filepath(&#039;t&#039;);\n pg_relation_filepath \n----------------------\n base\/5\/16401\n(1 row)\npostgres=# select count(*) from pg_buffercache where relfilenode = 16401;\n count \n-------\n    58\n(1 row)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">If you wonder why there are 58 blocks cached in the buffer cache but we only loaded 54, this is because of the <a href=\"https:\/\/www.postgresql.org\/docs\/current\/storage-vm.html\" target=\"_blank\" rel=\"noreferrer noopener\">visibility<\/a> and <a href=\"https:\/\/www.postgresql.org\/docs\/current\/storage-fsm.html\">free space map<\/a>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npostgres=# select relforknumber from pg_buffercache where relfilenode = 16401 and relforknumber != 0;\n relforknumber \n---------------\n             1\n             1\n             1\n             2\n(4 rows)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Using the new pg_buffercache_evict() function we are now able to completely evict the buffers of that table from the cache, which results in exactly 58 blocks to be evicted:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; highlight: [1]; title: ; notranslate\" title=\"\">\npostgres=# select pg_buffercache_evict(bufferid) from pg_buffercache where relfilenode = 16401;\n pg_buffercache_evict \n----------------------\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n t\n(58 rows)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Cross-checking this confirms, that all the blocks are gone:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; highlight: [1]; title: ; notranslate\" title=\"\">\npostgres=# select count(*) from pg_buffercache where relfilenode = 16401;\n count \n-------\n     0\n(1 row)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Nice, thanks to all involved.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In PostgreSQL up to version 16, there is no way to evict the buffer cache except by restarting the instance. In Oracle you can do that since ages with &#8220;alter system flush buffer cache&#8220;, but not in PostgreSQL. This will change when PostgreSQL 17 will be released later this year. Of course, flushing the buffer [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[229,198,83],"tags":[77],"type_dbi":[2749],"class_list":["post-32428","post","type-post","status-publish","format-standard","hentry","category-database-administration-monitoring","category-database-management","category-postgresql","tag-postgresql","type-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 17: pg_buffercache_evict() - 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-17-pg_buffercache_evict\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL 17: pg_buffercache_evict()\" \/>\n<meta property=\"og:description\" content=\"In PostgreSQL up to version 16, there is no way to evict the buffer cache except by restarting the instance. In Oracle you can do that since ages with &#8220;alter system flush buffer cache&#8220;, but not in PostgreSQL. This will change when PostgreSQL 17 will be released later this year. Of course, flushing the buffer [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/postgresql-17-pg_buffercache_evict\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2024-04-11T07:49:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-11T07:50:01+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=\"1 minute\" \/>\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-17-pg_buffercache_evict\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-17-pg_buffercache_evict\\\/\"},\"author\":{\"name\":\"Open source Team\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\"},\"headline\":\"PostgreSQL 17: pg_buffercache_evict()\",\"datePublished\":\"2024-04-11T07:49:58+00:00\",\"dateModified\":\"2024-04-11T07:50:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-17-pg_buffercache_evict\\\/\"},\"wordCount\":229,\"commentCount\":0,\"keywords\":[\"PostgreSQL\"],\"articleSection\":[\"Database Administration &amp; Monitoring\",\"Database management\",\"PostgreSQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-17-pg_buffercache_evict\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-17-pg_buffercache_evict\\\/\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-17-pg_buffercache_evict\\\/\",\"name\":\"PostgreSQL 17: pg_buffercache_evict() - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\"},\"datePublished\":\"2024-04-11T07:49:58+00:00\",\"dateModified\":\"2024-04-11T07:50:01+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-17-pg_buffercache_evict\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-17-pg_buffercache_evict\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-17-pg_buffercache_evict\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL 17: pg_buffercache_evict()\"}]},{\"@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 17: pg_buffercache_evict() - 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-17-pg_buffercache_evict\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL 17: pg_buffercache_evict()","og_description":"In PostgreSQL up to version 16, there is no way to evict the buffer cache except by restarting the instance. In Oracle you can do that since ages with &#8220;alter system flush buffer cache&#8220;, but not in PostgreSQL. This will change when PostgreSQL 17 will be released later this year. Of course, flushing the buffer [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/postgresql-17-pg_buffercache_evict\/","og_site_name":"dbi Blog","article_published_time":"2024-04-11T07:49:58+00:00","article_modified_time":"2024-04-11T07:50:01+00:00","author":"Open source Team","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Open source Team","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-17-pg_buffercache_evict\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-17-pg_buffercache_evict\/"},"author":{"name":"Open source Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202"},"headline":"PostgreSQL 17: pg_buffercache_evict()","datePublished":"2024-04-11T07:49:58+00:00","dateModified":"2024-04-11T07:50:01+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-17-pg_buffercache_evict\/"},"wordCount":229,"commentCount":0,"keywords":["PostgreSQL"],"articleSection":["Database Administration &amp; Monitoring","Database management","PostgreSQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/postgresql-17-pg_buffercache_evict\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-17-pg_buffercache_evict\/","url":"https:\/\/www.dbi-services.com\/blog\/postgresql-17-pg_buffercache_evict\/","name":"PostgreSQL 17: pg_buffercache_evict() - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2024-04-11T07:49:58+00:00","dateModified":"2024-04-11T07:50:01+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-17-pg_buffercache_evict\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/postgresql-17-pg_buffercache_evict\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-17-pg_buffercache_evict\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL 17: pg_buffercache_evict()"}]},{"@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\/32428","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=32428"}],"version-history":[{"count":8,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/32428\/revisions"}],"predecessor-version":[{"id":32436,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/32428\/revisions\/32436"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=32428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=32428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=32428"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=32428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}