[{"data":1,"prerenderedAt":6},["ShallowReactive",2],{"post-content-pg-stat-statements-hidden-cost":3},{"content":4,"lastModified":5},"\u003Cp>Our reporting endpoint got slower over about a month. No deploy to blame, nothing in the logs, just a page that used to load in 400ms and now took 1.4 seconds. So I did what everyone does. Turned on \u003Ccode>pg_stat_statements\u003C\u002Fcode>, ranked by \u003Ccode>total_exec_time\u003C\u002Fcode>, read the top ten.\u003C\u002Fp>\n\n\u003Cp>The worst entry was 6ms. Second was 4ms. Nothing on that list could account for a 1.4 second page.\u003C\u002Fp>\n\n\u003Cp>It wasn't lying. It was measuring three things wrong, and none of them were the queries.\u003C\u002Fp>\n\n\u003Cimg src=\"https:\u002F\u002Fimages.pexels.com\u002Fphotos\u002F899317\u002Fpexels-photo-899317.jpeg?auto=compress&amp;cs=tinysrgb&amp;w=1600\" alt=\"Manual flip scoreboard plates showing sets won 2 and sets won 1, hanging on a rusty metal bar\" loading=\"lazy\" \u002F>\n\n\u003Ch2>The planner's share is missing by default\u003C\u002Fh2>\n\n\u003Cp>\u003Ccode>pg_stat_statements.track_planning\u003C\u002Fcode> ships as \u003Ccode>off\u003C\u002Fcode>. \u003Ccode>total_plan_time\u003C\u002Fcode>, \u003Ccode>mean_plan_time\u003C\u002Fcode> and \u003Ccode>stddev_plan_time\u003C\u002Fcode> have existed since Postgres 13. They sit at zero until you flip it.\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-sql\">ALTER SYSTEM SET pg_stat_statements.track_planning = on;\nSELECT pg_reload_conf();\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>No restart. \u003Ccode>shared_preload_libraries\u003C\u002Fcode> needs one, this doesn't.\u003C\u002Fp>\n\n\u003Cp>I built an eight way join over eight small tables and ran it 100 times through \u003Ccode>psql\u003C\u002Fcode>, which replans every time:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-text\">-- track_planning = off\n calls | total_exec_time | total_plan_time | mean_exec_time | mean_plan_time\n-------+-----------------+-----------------+----------------+----------------\n   100 |            6.38 |            0.00 |          0.064 |          0.000\n\n-- track_planning = on\n   100 |            6.82 |           95.82 |          0.068 |          0.958\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>Six point eight milliseconds of execution against ninety five of planning, fourteen times more work sitting in a column the standard top-ten query never selects. Reported the default way that join costs 6.8ms. With planning on it costs 102ms. Joins and union-expanding views are mostly planning, and you have been reading the tip.\u003C\u002Fp>\n\n\u003Cp>One caveat decides how much this matters. If your driver caches prepared statements you pay the plan once and the defaults are fine. Plenty don't, \u003Ccode>psql -c\u003C\u002Fcode> included. On one of those, planning is a cost you have been paying all month without seeing.\u003C\u002Fp>\n\n\u003Ch2>Normalization welds two different queries into one row\u003C\u002Fh2>\n\n\u003Cp>Constants become \u003Ccode>$1\u003C\u002Fcode>. Every post about this view says so. They leave out that LIMIT counts as a constant too.\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-text\">-- select id, note from orders order by note limit 10       (10 calls)\n  mean_exec_time: 29.1ms\n\n-- select id, note from orders order by note limit 20000    (10 calls)\n  mean_exec_time: 302.1ms\n\n-- both of them, in the view:\n calls | mean_exec_time | max_exec_time | stddev_exec_time | query\n-------+----------------+---------------+------------------+------------------------------\n    20 |          165.7 |         305.6 |            137.0 | ... order by note limit $1\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>One row for two jobs that share nothing but a shape. That mean of 165.7 is just the average of 29 and 302. On a dashboard I would read it as slow and consistently slow, which is wrong twice: it isn't one query, and the fast variant is fine.\u003C\u002Fp>\n\n\u003Cp>The tell is \u003Ccode>stddev_exec_time\u003C\u002Fcode>, a column almost nobody selects. 137 next to a mean of 165. A shape with one plan spreads tighter than that. When the standard deviation sits close to the mean, treat the row as a blend until you've proven otherwise.\u003C\u002Fp>\n\n\u003Cp>Aliases are worse than LIMIT, because they don't even leave a trace. Output column names aren't part of the hash at all:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-sql\">select count(*) as a from orders;\nselect count(*) as b from orders;\nselect count(*) as c_really_different from orders;\n-- queryid 9175542703500084910 | calls 3\n-- query: select count(*) as a from orders\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>Three statements, one row, and the \u003Ccode>query\u003C\u002Fcode> column only holds the first. That column is a reconstruction, not a log line. It's the first execution of a shape, constants swapped for placeholders, never anything that came later. Copy it into \u003Ccode>EXPLAIN\u003C\u002Fcode> and you may be explaining a statement that ran with different aliases and a different LIMIT, days ago.\u003C\u002Fp>\n\n\u003Cp>That's why I read \u003Ccode>max_exec_time\u003C\u002Fcode> and \u003Ccode>stddev_exec_time\u003C\u002Fcode> before the mean, and why a slow query log with real parameters still earns its keep. When the plan does look insane, \u003Ca href=\"\u002Fposts\u002Fpostgres-not-using-my-index-what-i-check\u002F\" rel=\"noopener noreferrer\">these are the five things I check first\u003C\u002Fa>.\u003C\u002Fp>\n\n\u003Ch2>Anything inside a function runs off the books\u003C\u002Fh2>\n\n\u003Cp>\u003Ccode>pg_stat_statements.track\u003C\u002Fcode> defaults to \u003Ccode>top\u003C\u002Fcode>. Top level only. A PL\u002FpgSQL function is one statement as far as this view is concerned.\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-sql\">create function touch_orders(n int) returns int language plpgsql as $$\ndeclare total int := 0; r int;\nbegin\n  for i in 1..n loop\n    select count(*) into r from orders where customer_id = i;\n    total := total + r;\n  end loop;\n  return total;\nend $$;\n\n-- with track = top (the default)\n toplevel | calls | total_exec_time | query\n----------+-------+-----------------+---------------------\n t        |     1 |         16.89ms | select touch_orders($1)\n-- rows in the view matching the inner select: 0\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>One call, seventeen milliseconds, scroll past it. Flip to \u003Ccode>track = all\u003C\u002Fcode> and the same invocation produces a second row: \u003Ccode>toplevel = false\u003C\u002Fcode>, 5000 calls, 9.7ms. The function was never one cheap statement. It was five thousand index lookups and a loop, and the hot path I was hunting lived inside that loop the whole time.\u003C\u002Fp>\n\n\u003Cp>I run \u003Ccode>track = all\u003C\u002Fcode> for ten minutes when something smells, then put it back. It instruments every nested statement and costs something. Ten minutes is worth it. Forever is not.\u003C\u002Fp>\n\n\u003Ch2>Before you trust the ordering\u003C\u002Fh2>\n\n\u003Cp>Three things change what the top ten means, and none of them show up in the query everyone copies.\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-sql\">SELECT name, setting FROM pg_settings WHERE name LIKE 'pg_stat_statements%';\nSELECT * FROM pg_stat_statements_info;\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>\u003Ccode>pg_stat_statements.max\u003C\u002Fcode> won't go below 100. I set it to 10 to test eviction and startup logged \u003Ccode>WARNING: 10 is outside the valid range for parameter \"pg_stat_statements.max\" (100 .. 1073741823)\u003C\u002Fcode>, then carried on with the default 5000. Had I not read the log I'd have assumed the setting applied.\u003C\u002Fp>\n\n\u003Cp>When the table fills, entries get evicted and \u003Ccode>pg_stat_statements_info.dealloc\u003C\u002Fcode> counts it. On a fresh restart with \u003Ccode>max = 100\u003C\u002Fcode> I ran 130 statements that differed only in the table they touched. Ninety survived. \u003Ccode>dealloc\u003C\u002Fcode> reported 4, so that counter won't tell you how much you lost either. The only honest check is whether the statement you're chasing is in there at all.\u003C\u002Fp>\n\n\u003Cp>Last one, and it catches people on shared instances. The extension is per database. \u003Ccode>shared_preload_libraries\u003C\u002Fcode> is instance wide, the view is not. I created a second database, queried the view, and got \u003Ccode>relation \"pg_stat_statements\" does not exist\u003C\u002Fcode> while the library collected happily in the first one.\u003C\u002Fp>\n\n\u003Cp>None of this makes the extension a bad tool. It's the only thing I have that answers where the database spent its time. It just answers a narrower question than a ranking implies, and the gap between the two is where my afternoon went. The 1.4 seconds came from a per-request function running a few thousand lookups under one cheap-looking row.\u003C\u002Fp>\n\n\u003Cp>So: planning on, rank by \u003Ccode>total_plan_time + total_exec_time\u003C\u002Fcode>, check the standard deviation before the mean, and look at \u003Ccode>dealloc\u003C\u002Fcode> when the list seems too clean. On MySQL the same drill looks nothing like this, I wrote \u003Ca href=\"\u002Fposts\u002Fmysql-slow-query-debugging-workflow\u002F\" rel=\"noopener noreferrer\">that one up here\u003C\u002Fa>. The ranking query and the function test live in \u003Ca href=\"\u002Fsnippetark\u002F\" rel=\"noopener noreferrer\">Snippet Ark\u003C\u002Fa> now, mostly so I stop rebuilding them from memory every time a page gets slow.\u003C\u002Fp>\n","2026-11-20",1795678594080]