Menu

1 / 3

Reading EXPLAIN as a tree: the slowest node is never the root

DEV Community: postgres·devtoolkits·about 7 hours ago
#bJ2hqcMO
#devtopostgresql#devto#dev#time#loops
Long article (714 words) — no AI summary yet.
Reading 0:00
15s threshold

Which node in an EXPLAIN plan is actually slow? Reading actual time at face value gets it wrong, and I only noticed while parsing plans into a tree for a browser-only EXPLAIN visualizer (nothing you paste leaves the page).

Parsing the text back into a tree is the easy half. Here are the three things that bit me.

Trap 1: actual time includes the children

In EXPLAIN (ANALYZE) , actual time=0.010..40.000 includes the time the node spent waiting on its children. If you turn that into a percentage, the root ( Sort , Limit ) is always near 100% and tells you nothing.

A node's own time is:

own = (time per loop × loops) − Σ(child time per loop × child loops)

Enter fullscreen mode

Exit fullscreen mode

A real PostgreSQL 16 plan (trimmed):

Merge Join (… ) (actual time=3.426..63.571 rows=18000 loops=1) -> Index Scan using idx_orders_user on orders o (… ) (actual time=0.010..44.488 rows=99996 loops=1) -> Sort (… ) (actual time=2.873..3.233 rows=3600 loops=1) -> Seq Scan on users u (… ) (actual time=0.018..2.266…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.