{"id":10674,"date":"2017-11-28T12:32:21","date_gmt":"2017-11-28T11:32:21","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\/"},"modified":"2017-11-28T12:32:21","modified_gmt":"2017-11-28T11:32:21","slug":"are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\/","title":{"rendered":"Are statistics immediately available after creating a table or an index in PostgreSQL?"},"content":{"rendered":"<p>While giving the last <a href=\"https:\/\/www.dbi-services.com\/trainings\/postgresql-dba-essentials-workshop\/\" target=\"_blank\" rel=\"noopener\">PostgreSQL DBA Essentials workshop<\/a> this question came up: When we create a table or an index: are the statistics available automatically? To be more precise: When we create and load a table in one step, create an index on that table afterwards: Do we have the statistics available by default or do we need to wait for autovacuum to kick in or analyze manually? Lets see &#8230;<\/p>\n<p><!--more--><\/p>\n<p>First of all lets disable <a href=\"https:\/\/www.postgresql.org\/docs\/current\/static\/routine-vacuuming.html\" target=\"_blank\" rel=\"noopener\">autovacuum<\/a> so it does not kick off analyze in the background:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# ! ps -ef | grep autov | grep -v grep\npostgres  1641  1635  0 07:08 ?        00:00:00 postgres: MY_CLUSTER: autovacuum launcher process   \npostgres=# alter system set autovacuum=off;\nALTER SYSTEM\npostgres=# select * from pg_reload_conf();\n pg_reload_conf \n----------------\n t\n(1 row)\n\npostgres=# ! ps -ef | grep autov | grep -v grep\n<\/pre>\n<p>Create and populate the table:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# ! cat a.sql\ndrop table if exists t;\ncreate table t\nas select a.*, md5(a::varchar) from generate_series(1,5000000) a;\npostgres=# i a.sql\npsql:a.sql:1: NOTICE:  table \"t\" does not exist, skipping\nDROP TABLE\nSELECT 5000000\n<\/pre>\n<p>Create an index:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# create index i1 on t(a);\nCREATE INDEX\npostgres=# d+ t\n                                     Table \"public.t\"\n Column |  Type   | Collation | Nullable | Default | Storage  | Stats target | Description \n--------+---------+-----------+----------+---------+----------+--------------+-------------\n a      | integer |           |          |         | plain    |              | \n md5    | text    |           |          |         | extended |              | \nIndexes:\n    \"i1\" btree (a)\n<\/pre>\n<p>Do we have statistics already? Lets check:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# select stanullfrac,stawidth,stadistinct from pg_statistic where starelid = 't'::regclass;\n stanullfrac | stawidth | stadistinct \n-------------+----------+-------------\n(0 rows)\n<\/pre>\n<p>No, at least not for the table. What about the index?<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# select stanullfrac,stawidth,stadistinct from pg_statistic where starelid = 'i1'::regclass;\n stanullfrac | stawidth | stadistinct \n-------------+----------+-------------\n(0 rows)\n<\/pre>\n<p>No. Lets analyze:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# analyze t;\nANALYZE\npostgres=# analyze i1;\nWARNING:  skipping \"i1\" --- cannot analyze non-tables or special system tables\nANALYZE\n<\/pre>\n<p>Apparently we can not analyze an index. What do we see now?<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# select stanullfrac,stawidth,stadistinct from pg_statistic where starelid = 't'::regclass;\n stanullfrac | stawidth | stadistinct \n-------------+----------+-------------\n           0 |        4 |          -1\n           0 |       33 |          -1\n(2 rows)\n\npostgres=# select stanullfrac,stawidth,stadistinct from pg_statistic where starelid = 'i1'::regclass;\n stanullfrac | stawidth | stadistinct \n-------------+----------+-------------\n(0 rows)\n<\/pre>\n<p>We do see statistics for the table but not for the index. The reason is that &#8220;analyze&#8221; works on the tables, but not on the indexes. For regular indexes there will be nothing in pg_statistic because that information would be <a href=\"https:\/\/www.postgresql.org\/docs\/current\/static\/catalog-pg-statistic.html\" target=\"_blank\" rel=\"noopener\">redundant with the underlying table columns<\/a>. But there will be statistics for function based indexes:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# create index i2 on t(lower(a::text));\nCREATE INDEX\npostgres=# select stanullfrac,stawidth,stadistinct from pg_statistic where starelid = 'i2'::regclass;\n stanullfrac | stawidth | stadistinct \n-------------+----------+-------------\n(0 rows)\n\npostgres=# analyze t;\nANALYZE\npostgres=# select stanullfrac,stawidth,stadistinct from pg_statistic where starelid = 'i2'::regclass;\n stanullfrac | stawidth | stadistinct \n-------------+----------+-------------\n           0 |       10 |          -1\n(1 row)\n<\/pre>\n<p>So, when autovacuum is off we do not get statistics when we do not kick off a manual analyze (which is not a surprise). What happens when autovacuum is on?<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# alter system set autovacuum=on;\nALTER SYSTEM\npostgres=# select * from pg_reload_conf();\n pg_reload_conf \n----------------\n t\n(1 row)\npostgres=# i a.sql\nDROP TABLE\nSELECT 5000000\npostgres=# select stanullfrac,stawidth,stadistinct from pg_statistic where starelid = 't'::regclass;\n stanullfrac | stawidth | stadistinct \n-------------+----------+-------------\n(0 rows)\n<\/pre>\n<p>Nope, same picture here. But some seconds later:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# select stanullfrac,stawidth,stadistinct from pg_statistic where starelid = 't'::regclass;\n stanullfrac | stawidth | stadistinct \n-------------+----------+-------------\n           0 |        4 |          -1\n           0 |       33 |          -1\n(2 rows)\n<\/pre>\n<p>&#8230; statistics are there. Conclusion: When you require current statistics directly after loading a table you&#8217;d better kick of a manual analyze right after. Otherwise autovacuum will take care about that, but not immediately.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While giving the last PostgreSQL DBA Essentials workshop this question came up: When we create a table or an index: are the statistics available automatically? To be more precise: When we create and load a table in one step, create an index on that table afterwards: Do we have the statistics available by default or [&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-10674","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 statistics immediately available after creating a table or an index 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-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Are statistics immediately available after creating a table or an index in PostgreSQL?\" \/>\n<meta property=\"og:description\" content=\"While giving the last PostgreSQL DBA Essentials workshop this question came up: When we create a table or an index: are the statistics available automatically? To be more precise: When we create and load a table in one step, create an index on that table afterwards: Do we have the statistics available by default or [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2017-11-28T11:32:21+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\\\/are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\\\/\"},\"author\":{\"name\":\"Open source Team\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\"},\"headline\":\"Are statistics immediately available after creating a table or an index in PostgreSQL?\",\"datePublished\":\"2017-11-28T11:32:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\\\/\"},\"wordCount\":268,\"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-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\\\/\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\\\/\",\"name\":\"Are statistics immediately available after creating a table or an index in PostgreSQL? - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\"},\"datePublished\":\"2017-11-28T11:32:21+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Are statistics immediately available after creating a table or an index 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 statistics immediately available after creating a table or an index 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-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\/","og_locale":"en_US","og_type":"article","og_title":"Are statistics immediately available after creating a table or an index in PostgreSQL?","og_description":"While giving the last PostgreSQL DBA Essentials workshop this question came up: When we create a table or an index: are the statistics available automatically? To be more precise: When we create and load a table in one step, create an index on that table afterwards: Do we have the statistics available by default or [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\/","og_site_name":"dbi Blog","article_published_time":"2017-11-28T11:32:21+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\/are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\/"},"author":{"name":"Open source Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202"},"headline":"Are statistics immediately available after creating a table or an index in PostgreSQL?","datePublished":"2017-11-28T11:32:21+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\/"},"wordCount":268,"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-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\/","url":"https:\/\/www.dbi-services.com\/blog\/are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\/","name":"Are statistics immediately available after creating a table or an index in PostgreSQL? - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2017-11-28T11:32:21+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/are-statistics-immediately-available-after-creating-a-table-or-an-index-in-postgresql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Are statistics immediately available after creating a table or an index 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\/10674","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=10674"}],"version-history":[{"count":0,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/10674\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=10674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=10674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=10674"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=10674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}