{"id":10638,"date":"2017-11-15T12:28:33","date_gmt":"2017-11-15T11:28:33","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/auto-pre-warming-in-edb-postgres-advanced-server-10\/"},"modified":"2017-11-15T12:28:33","modified_gmt":"2017-11-15T11:28:33","slug":"auto-pre-warming-in-edb-postgres-advanced-server-10","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/auto-pre-warming-in-edb-postgres-advanced-server-10\/","title":{"rendered":"Auto pre-warming in EDB Postgres Advanced Server 10"},"content":{"rendered":"<p>Some days ago <a href=\"https:\/\/www.enterprisedb.com\/resources\/data-sheets\/edb-postgres-advanced-server\" target=\"_blank\" rel=\"noopener\">EDB Postgres Advanced Server 10<\/a> was released and one feature which might be handy is auto pre-warming. What this does is to save all the buffers (or better a description of the buffers) which are currently loaded in to shared_buffers to disk and then re-read the buffers automatically when the instance is restarted. Lets see how it works.<\/p>\n<p><!--more--><\/p>\n<p>Before getting the feature to work we need to look at <a href=\"https:\/\/www.enterprisedb.com\/docs\/en\/10.0\/EPAS_Guide_v10\/EDB_Postgres_Advanced_Server_Guide.1.24.html#pID0E0GRF0HA\" target=\"_blank\" rel=\"noopener\">two parameters<\/a> which control the behavior:<\/p>\n<ul>\n<li>pg_prewarm.autoprewarm: Enabled or disabled the feature<\/li>\n<li>pg_prewarm.autoprewarm_interval: The interval the current state is written to disk or 0 to only write once when the instance shutsdown<\/li>\n<\/ul>\n<p>Another requirement is to load the library when the instance starts:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# alter system set shared_preload_libraries ='$libdir\/dbms_pipe,$libdir\/edb_gen,$libdir\/dbms_aq,$libdir\/pg_prewarm';\nALTER SYSTEM\n<\/pre>\n<p>Once the instance is restarted we can proceed with the configuration:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# alter system set pg_prewarm.autoprewarm=true;\nALTER SYSTEM\npostgres=# alter system set pg_prewarm.autoprewarm_interval='10s';\nALTER SYSTEM\n<\/pre>\n<p>By doing this we told the server to write the current state of the buffers to disk every 10 seconds. You&#8217;ll also notice a new background worker process which is responsible for doing the work:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# ! ps -ef | grep prewarm | egrep -v \"ps|grep\"\npostgres  3682  3675  0 12:05 ?        00:00:00 postgres: bgworker: autoprewarm   \n<\/pre>\n<p>Lets load something into shared_buffers:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1; highlight: [12,14]\">\npostgres=# insert into t1 select a, md5(a::varchar) from generate_series(1,1000) a;\nINSERT 0 1000\npostgres=# select count(*) from t1;\n count \n-------\n  1000\n(1 row)\npostgres=# explain (analyze,buffers) select count(*) from t1;\n                                               QUERY PLAN                                                \n---------------------------------------------------------------------------------------------------------\n Aggregate  (cost=21.50..21.51 rows=1 width=8) (actual time=0.492..0.492 rows=1 loops=1)\n   Buffers: shared hit=9\n   -&gt;  Seq Scan on t1  (cost=0.00..19.00 rows=1000 width=0) (actual time=0.019..0.254 rows=1000 loops=1)\n         Buffers: shared hit=9\n Planning time: 0.070 ms\n Execution time: 0.538 ms\n(6 rows)\n<\/pre>\n<p>The &#8220;shared hit&#8221; confirms that we read the buffers from shared_buffers and not from the os\/file system cache. Then lets restart and do the same check again:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1; highlight: [10,12]\">\npostgres@centos7:\/u02\/pgdata\/PG4\/ [EDB10] pg_ctl -D . restart -m fast\npostgres@centos7:\/u02\/pgdata\/PG4\/ [EDB10] psql -X postgres\npsql.bin (10.1.5)\nType \"help\" for help.\n\npostgres=# explain (analyze,buffers) select count(*) from t1;\n                                               QUERY PLAN                                                \n---------------------------------------------------------------------------------------------------------\n Aggregate  (cost=21.50..21.51 rows=1 width=8) (actual time=0.586..0.586 rows=1 loops=1)\n   Buffers: shared hit=9\n   -&gt;  Seq Scan on t1  (cost=0.00..19.00 rows=1000 width=0) (actual time=0.024..0.295 rows=1000 loops=1)\n         Buffers: shared hit=9\n Planning time: 0.451 ms\n Execution time: 0.766 ms\n(6 rows)\n\npostgres=# \n<\/pre>\n<p>&#8230; here we go. How is this information stored? When you take a look at $PGDATA you&#8217;ll notice a file with the following format:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">\npostgres@centos7:\/u02\/pgdata\/PG4\/ [EDB10] cat $PGDATA\/autoprewarm.blocks | tail\n&lt;&gt;\n0,1664,1262,0,0\n15471,1663,1259,0,0\n15471,1663,1259,0,1\n15471,1663,1259,0,2\n15471,1663,1249,0,0\n15471,1663,1249,0,1\n15471,1663,1249,0,2\n15471,1663,1249,0,3\n15471,1663,1249,0,4\n<\/pre>\n<p>The first field is the OID of the database:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# select oid,datname from pg_database where oid=15471;\n  oid  | datname  \n-------+----------\n 15471 | postgres\n(1 row)\n<\/pre>\n<p>The second one is the tablespace:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# select oid,spcname from pg_tablespace where oid=1663;\n oid  |  spcname   \n------+------------\n 1663 | pg_default\n(1 row)\n<\/pre>\n<p>The third one is the table:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# select oid,relname from pg_class where oid = 16402;\n  oid  | relname \n-------+---------\n 16402 | t1\n(1 row)\n\npostgres=# ! grep 16402 $PGDATA\/autoprewarm.blocks\n15471,1663,16402,0,0\n15471,1663,16402,0,1\n15471,1663,16402,0,2\n15471,1663,16402,0,3\n15471,1663,16402,0,4\n15471,1663,16402,0,5\n15471,1663,16402,0,6\n15471,1663,16402,0,7\n15471,1663,16402,0,8\n15471,1663,16402,1,0\n15471,1663,16402,1,2\n<\/pre>\n<p>The fourth one is the fork\/file (0 is the datafile, 1 is the free space map) and the last one is the actual block to load. This is also described in &#8220;.\/contrib\/pg_prewarm\/autoprewarm.c&#8221; in the PostgreSQL source code:<\/p>\n<pre class=\"brush: c; gutter: true; first-line: 1\">\n\/* Metadata for each block we dump. *\/\ntypedef struct BlockInfoRecord\n{\n        Oid                     database;\n        Oid                     tablespace;\n        Oid                     filenode;\n        ForkNumber      forknum;\n        BlockNumber blocknum;\n} BlockInfoRecord;\n<\/pre>\n<p>For community PostgreSQL there is the contrib module <a href=\"https:\/\/www.postgresql.org\/docs\/10\/static\/pgprewarm.html\" target=\"_blank\" rel=\"noopener\">pg_prewarm<\/a> you can use for that, check <a href=\"https:\/\/www.dbi-services.com\/blog\/pre-warming-the-buffer-cache-in-postgresql\/\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some days ago EDB Postgres Advanced Server 10 was released and one feature which might be handy is auto pre-warming. What this does is to save all the buffers (or better a description of the buffers) which are currently loaded in to shared_buffers to disk and then re-read the buffers automatically when the instance is [&hellip;]<\/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-10638","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>Auto pre-warming in EDB Postgres Advanced Server 10 - 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\/auto-pre-warming-in-edb-postgres-advanced-server-10\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Auto pre-warming in EDB Postgres Advanced Server 10\" \/>\n<meta property=\"og:description\" content=\"Some days ago EDB Postgres Advanced Server 10 was released and one feature which might be handy is auto pre-warming. What this does is to save all the buffers (or better a description of the buffers) which are currently loaded in to shared_buffers to disk and then re-read the buffers automatically when the instance is [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/auto-pre-warming-in-edb-postgres-advanced-server-10\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2017-11-15T11:28:33+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\\\/auto-pre-warming-in-edb-postgres-advanced-server-10\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/auto-pre-warming-in-edb-postgres-advanced-server-10\\\/\"},\"author\":{\"name\":\"Open source Team\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\"},\"headline\":\"Auto pre-warming in EDB Postgres Advanced Server 10\",\"datePublished\":\"2017-11-15T11:28:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/auto-pre-warming-in-edb-postgres-advanced-server-10\\\/\"},\"wordCount\":309,\"commentCount\":0,\"keywords\":[\"PostgreSQL\"],\"articleSection\":[\"Database Administration &amp; Monitoring\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/auto-pre-warming-in-edb-postgres-advanced-server-10\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/auto-pre-warming-in-edb-postgres-advanced-server-10\\\/\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/auto-pre-warming-in-edb-postgres-advanced-server-10\\\/\",\"name\":\"Auto pre-warming in EDB Postgres Advanced Server 10 - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\"},\"datePublished\":\"2017-11-15T11:28:33+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/auto-pre-warming-in-edb-postgres-advanced-server-10\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/auto-pre-warming-in-edb-postgres-advanced-server-10\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/auto-pre-warming-in-edb-postgres-advanced-server-10\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Auto pre-warming in EDB Postgres Advanced Server 10\"}]},{\"@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":"Auto pre-warming in EDB Postgres Advanced Server 10 - 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\/auto-pre-warming-in-edb-postgres-advanced-server-10\/","og_locale":"en_US","og_type":"article","og_title":"Auto pre-warming in EDB Postgres Advanced Server 10","og_description":"Some days ago EDB Postgres Advanced Server 10 was released and one feature which might be handy is auto pre-warming. What this does is to save all the buffers (or better a description of the buffers) which are currently loaded in to shared_buffers to disk and then re-read the buffers automatically when the instance is [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/auto-pre-warming-in-edb-postgres-advanced-server-10\/","og_site_name":"dbi Blog","article_published_time":"2017-11-15T11:28:33+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\/auto-pre-warming-in-edb-postgres-advanced-server-10\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/auto-pre-warming-in-edb-postgres-advanced-server-10\/"},"author":{"name":"Open source Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202"},"headline":"Auto pre-warming in EDB Postgres Advanced Server 10","datePublished":"2017-11-15T11:28:33+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/auto-pre-warming-in-edb-postgres-advanced-server-10\/"},"wordCount":309,"commentCount":0,"keywords":["PostgreSQL"],"articleSection":["Database Administration &amp; Monitoring"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/auto-pre-warming-in-edb-postgres-advanced-server-10\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/auto-pre-warming-in-edb-postgres-advanced-server-10\/","url":"https:\/\/www.dbi-services.com\/blog\/auto-pre-warming-in-edb-postgres-advanced-server-10\/","name":"Auto pre-warming in EDB Postgres Advanced Server 10 - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2017-11-15T11:28:33+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/auto-pre-warming-in-edb-postgres-advanced-server-10\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/auto-pre-warming-in-edb-postgres-advanced-server-10\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/auto-pre-warming-in-edb-postgres-advanced-server-10\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Auto pre-warming in EDB Postgres Advanced Server 10"}]},{"@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\/10638","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=10638"}],"version-history":[{"count":0,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/10638\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=10638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=10638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=10638"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=10638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}