{"id":10585,"date":"2017-10-31T07:03:32","date_gmt":"2017-10-31T06:03:32","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/are-temporary-tables-auto-vacuumed-in-postgresql\/"},"modified":"2017-10-31T07:03:32","modified_gmt":"2017-10-31T06:03:32","slug":"are-temporary-tables-auto-vacuumed-in-postgresql","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/are-temporary-tables-auto-vacuumed-in-postgresql\/","title":{"rendered":"Are temporary tables auto vacuumed in PostgreSQL?"},"content":{"rendered":"<p>While doing the EDB quiz at their booth last week at <a href=\"https:\/\/2017.pgconf.eu\/\" target=\"_blank\" rel=\"noopener\">pgconfeu<\/a> one of the questions was: Are temporary tables auto vacuumed? What do you think? My first thought was yes, but lets see. The first question we need to answer is: How can we check if a table (no matter if temporary or not for now) was auto vacuumed or not? PostgreSQL comes with many <a href=\"https:\/\/www.postgresql.org\/docs\/10\/static\/monitoring-stats.html#monitoring-stats-views\" target=\"_blank\" rel=\"noopener\">views that expose statistical information<\/a> and one of those is pg_stat_all_tables. Lets have a look &#8230;<\/p>\n<p><!--more--><\/p>\n<p>When you describe that view there is column named &#8220;last_autovacuum&#8221;:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1; highlight: [20]\">\npostgres=# d pg_stat_all_tables \n                      View \"pg_catalog.pg_stat_all_tables\"\n       Column        |           Type           | Collation | Nullable | Default \n---------------------+--------------------------+-----------+----------+---------\n relid               | oid                      |           |          | \n schemaname          | name                     |           |          | \n relname             | name                     |           |          | \n seq_scan            | bigint                   |           |          | \n seq_tup_read        | bigint                   |           |          | \n idx_scan            | bigint                   |           |          | \n idx_tup_fetch       | bigint                   |           |          | \n n_tup_ins           | bigint                   |           |          | \n n_tup_upd           | bigint                   |           |          | \n n_tup_del           | bigint                   |           |          | \n n_tup_hot_upd       | bigint                   |           |          | \n n_live_tup          | bigint                   |           |          | \n n_dead_tup          | bigint                   |           |          | \n n_mod_since_analyze | bigint                   |           |          | \n last_vacuum         | timestamp with time zone |           |          | \n last_autovacuum     | timestamp with time zone |           |          | \n last_analyze        | timestamp with time zone |           |          | \n last_autoanalyze    | timestamp with time zone |           |          | \n vacuum_count        | bigint                   |           |          | \n autovacuum_count    | bigint                   |           |          | \n analyze_count       | bigint                   |           |          | \n autoanalyze_count   | bigint                   |           |          | \n<\/pre>\n<p>That should give us the time of the last autovacuum, right? Before we begin, here are my autovacuum settings which are all at their defaults:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# select name,setting from pg_settings where name like '%autovacuum%' order by 1;\n                name                 |  setting  \n-------------------------------------+-----------\n autovacuum                          | on\n autovacuum_analyze_scale_factor     | 0.1\n autovacuum_analyze_threshold        | 50\n autovacuum_freeze_max_age           | 200000000\n autovacuum_max_workers              | 3\n autovacuum_multixact_freeze_max_age | 400000000\n autovacuum_naptime                  | 60\n autovacuum_vacuum_cost_delay        | 20\n autovacuum_vacuum_cost_limit        | -1\n autovacuum_vacuum_scale_factor      | 0.2\n autovacuum_vacuum_threshold         | 50\n autovacuum_work_mem                 | -1\n log_autovacuum_min_duration         | -1\n(13 rows)\n<\/pre>\n<p>That means autovacuum should kick in as soon as we change 50 rows in a table because autovacuum_vacuum_threshold is set to 50? The table:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# create table t1 (a int, b varchar(50));\nCREATE TABLE\npostgres=# insert into t1 (a,b) select a, md5(a::varchar) from generate_series ( 1, 1000000 ) a;\nINSERT 0 1000000\npostgres=# select count(*) from t1;\n  count  \n---------\n 1000000\n(1 row)\n<\/pre>\n<p>As soon as we change 50 or more rows we should see the last_autovacuum column updated in pg_stat_all_tables, so lets check:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# update t1 set a = a + 1 where a &lt; 1000;\nUPDATE 999\npostgres=# select pg_sleep(10);\n pg_sleep \n----------\n \n(1 row)\npostgres=# select relname,last_autovacuum from pg_stat_all_tables where relname = &#039;t1&#039;;\n relname | last_autovacuum \n---------+-----------------\n t1      | \n(1 row)\n<\/pre>\n<p>Hm, not really what was expected. When you check the <a href=\"https:\/\/www.postgresql.org\/docs\/current\/static\/routine-vacuuming.html#autovacuum\" target=\"_blank\" rel=\"noopener\">documentation<\/a> there is a formula we need to consider for our test, which is<\/p>\n<pre class=\"brush: text; gutter: true; first-line: 1\">\nvacuum threshold = autovacuum_vacuum_threshold +  autovacuum_vacuum_scale_factor * pg_class.reltuples\n<\/pre>\n<p>In our case that is:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# show autovacuum_vacuum_threshold;\n autovacuum_vacuum_threshold \n-----------------------------\n 50\n(1 row)\n\npostgres=# show autovacuum_vacuum_scale_factor;\n autovacuum_vacuum_scale_factor \n--------------------------------\n 0.2\n(1 row)\n\npostgres=# select reltuples::int from pg_class where relname = 't1';\n reltuples \n-----------\n   1000000\n(1 row)\n\npostgres=# select 50 + 0.2 * 1000000;\n ?column? \n----------\n 200050.0\n(1 row)\n<\/pre>\n<p>This means we need to change at least 200050 rows to get autovacuum kicked in?<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# update t1 set a = a + 1;\nUPDATE 1000000\n<\/pre>\n<p>That should be fine as we updated all the rows in the table which is way more than 200050:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# select relname,last_autovacuum from pg_stat_all_tables where relname = 't1';\n relname |        last_autovacuum        \n---------+-------------------------------\n t1      | 2017-10-31 07:40:56.553194+01\n(1 row)\n<\/pre>\n<p>&#8230; and here we go. Now, as we know how to check that on a real table we can do the same test on temporary table:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# create temporary table tt1 as select * from t1;\nSELECT 1000000\npostgres=# update tt1 set a = a + 1;\nUPDATE 1000000\npostgres=# select relname,last_autovacuum from pg_stat_all_tables where relname = 'tt1';\n relname | last_autovacuum \n---------+-----------------\n tt1     | \n(1 row)\n<\/pre>\n<p>There is one point to consider: There is the parameter <a href=\"https:\/\/www.postgresql.org\/docs\/current\/static\/runtime-config-autovacuum.html#guc-autovacuum-naptime\" target=\"_blank\" rel=\"noopener\">autovacuum_naptime<\/a> which defaults to one minute so it might take some time until the autovacuum really did its work. But even when you wait for 10 minutes you&#8217;ll not see the last_autovacuum updated in pg_stat_all_tables for a temporary table. So, the answer is: No. There is no autovacuum on temporary tables but of course you can still do that manually:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# select relname,last_autovacuum, last_vacuum from pg_stat_all_tables where relname = 'tt1';\n relname | last_autovacuum |          last_vacuum          \n---------+-----------------+-------------------------------\n tt1     |                 | 2017-10-31 07:50:58.041813+01\n(1 row)\n<\/pre>\n<p>The same is true for the statistics used by the planner, you might need to analyze your temporary table manually:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# select last_analyze, last_autoanalyze from pg_stat_all_tables where relname = 'tt1';\n last_analyze | last_autoanalyze \n--------------+------------------\n              | \n(1 row)\n\npostgres=# analyze tt1;\nANALYZE\npostgres=# select last_analyze, last_autoanalyze from pg_stat_all_tables where relname = 'tt1';\n         last_analyze          | last_autoanalyze \n-------------------------------+------------------\n 2017-10-31 07:52:27.690117+01 | \n(1 row)\n<\/pre>\n<p>Btw: This is clearly written in the documentation: &#8220;Temporary tables cannot be accessed by autovacuum. Therefore, appropriate vacuum and analyze operations should be performed via session SQL commands.&#8221;<\/p>\n<p>Hope this helps &#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While doing the EDB quiz at their booth last week at pgconfeu one of the questions was: Are temporary tables auto vacuumed? What do you think? My first thought was yes, but lets see. The first question we need to answer is: How can we check if a table (no matter if temporary or not [&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-10585","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>Are temporary tables auto vacuumed in PostgreSQL? - 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\/are-temporary-tables-auto-vacuumed-in-postgresql\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Are temporary tables auto vacuumed in PostgreSQL?\" \/>\n<meta property=\"og:description\" content=\"While doing the EDB quiz at their booth last week at pgconfeu one of the questions was: Are temporary tables auto vacuumed? What do you think? My first thought was yes, but lets see. The first question we need to answer is: How can we check if a table (no matter if temporary or not [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/are-temporary-tables-auto-vacuumed-in-postgresql\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2017-10-31T06:03:32+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=\"4 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\\\/are-temporary-tables-auto-vacuumed-in-postgresql\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/are-temporary-tables-auto-vacuumed-in-postgresql\\\/\"},\"author\":{\"name\":\"Open source Team\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\"},\"headline\":\"Are temporary tables auto vacuumed in PostgreSQL?\",\"datePublished\":\"2017-10-31T06:03:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/are-temporary-tables-auto-vacuumed-in-postgresql\\\/\"},\"wordCount\":387,\"commentCount\":0,\"keywords\":[\"PostgreSQL\"],\"articleSection\":[\"Database Administration &amp; Monitoring\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/are-temporary-tables-auto-vacuumed-in-postgresql\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/are-temporary-tables-auto-vacuumed-in-postgresql\\\/\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/are-temporary-tables-auto-vacuumed-in-postgresql\\\/\",\"name\":\"Are temporary tables auto vacuumed in PostgreSQL? - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\"},\"datePublished\":\"2017-10-31T06:03:32+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/are-temporary-tables-auto-vacuumed-in-postgresql\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/are-temporary-tables-auto-vacuumed-in-postgresql\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/are-temporary-tables-auto-vacuumed-in-postgresql\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Are temporary tables auto vacuumed in PostgreSQL?\"}]},{\"@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":"Are temporary tables auto vacuumed in PostgreSQL? - 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\/are-temporary-tables-auto-vacuumed-in-postgresql\/","og_locale":"en_US","og_type":"article","og_title":"Are temporary tables auto vacuumed in PostgreSQL?","og_description":"While doing the EDB quiz at their booth last week at pgconfeu one of the questions was: Are temporary tables auto vacuumed? What do you think? My first thought was yes, but lets see. The first question we need to answer is: How can we check if a table (no matter if temporary or not [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/are-temporary-tables-auto-vacuumed-in-postgresql\/","og_site_name":"dbi Blog","article_published_time":"2017-10-31T06:03:32+00:00","author":"Open source Team","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Open source Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/are-temporary-tables-auto-vacuumed-in-postgresql\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/are-temporary-tables-auto-vacuumed-in-postgresql\/"},"author":{"name":"Open source Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202"},"headline":"Are temporary tables auto vacuumed in PostgreSQL?","datePublished":"2017-10-31T06:03:32+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/are-temporary-tables-auto-vacuumed-in-postgresql\/"},"wordCount":387,"commentCount":0,"keywords":["PostgreSQL"],"articleSection":["Database Administration &amp; Monitoring"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/are-temporary-tables-auto-vacuumed-in-postgresql\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/are-temporary-tables-auto-vacuumed-in-postgresql\/","url":"https:\/\/www.dbi-services.com\/blog\/are-temporary-tables-auto-vacuumed-in-postgresql\/","name":"Are temporary tables auto vacuumed in PostgreSQL? - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2017-10-31T06:03:32+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/are-temporary-tables-auto-vacuumed-in-postgresql\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/are-temporary-tables-auto-vacuumed-in-postgresql\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/are-temporary-tables-auto-vacuumed-in-postgresql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Are temporary tables auto vacuumed in PostgreSQL?"}]},{"@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\/10585","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=10585"}],"version-history":[{"count":0,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/10585\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=10585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=10585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=10585"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=10585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}