When an endpoint slows down, the SQL statement may be the cause, but timing alone is not enough to explain why. Useful database observability connects a request to a normalized query, its plan, and the conditions under which it became expensive.
Capture a safe baseline
Record query duration, operation name, rows returned where available, and a trace or request ID. Normalize parameter values so metrics group similar statements, and do not log secrets or personal data from query parameters. Sample high-volume queries to keep telemetry cost bounded. Track percentiles and error rates rather than only averages.
Use PostgreSQL’s slow-query logging or statistics extensions where appropriate, with thresholds chosen to match the service. Inspect
EXPLAINEXPLAIN ANALYZEMake fixes evidence-based
An index can improve one query and make writes or storage worse. Confirm the filter and ordering pattern, check index usage, and measure before and after with realistic data. Look for N+1 access, unbounded result sets, missing pagination, and queries that fetch unused columns. Do not add indexes based solely on a query’s appearance.
Dashboards should connect slow queries to routes and deployments. Alert on sustained user impact, not a single slow sample. Keep a repeatable diagnostic procedure so the next incident can move from symptom to plan to measured change.
Compare estimated rows with actual work
PostgreSQL prints estimated costs in planner units. EXPLAIN ANALYZE runs the statement and reports actual observed rows and timings. Adding BUFFERS shows where it accessed pages. Be careful with write statements because ANALYZE executes them as well. Start with an isolated database or a read-only query. PostgreSQL EXPLAIN
In a PostgreSQL 16.14 test of 10,000 generated search rows, the planner estimated one result and the query returned 67. The same result count appeared after adding the matching partial GIN index, and PostgreSQL switched from a sequential scan to a bitmap index and heap scan. The row estimate remained one. Plan nodes and row estimates tell different stories. Check data distribution and index selectivity before treating a single timing observation as proof of a production regression.
Worked example
Use the reproducible fixture in the DevShareHub search article to inspect both plans. The query below targets the published rows in that fixture. Establish the actual row count before attributing a time difference to the index.
sqlEXPLAIN (ANALYZE, BUFFERS, TIMING OFF) SELECT id FROM examples WHERE published AND to_tsvector('english', title) @@ plainto_tsquery('english', 'draft recovery');




Community
Discussion
Loading comments…