Postgres Observability
pgstats.dev
pgstats.dev
It would be great if Postgres could emit a trace per query, showing in real-time which internal components were hit by this query. A sort of continuous query explain service.
Combine these traces to database clients and other front end services and you’ll be able to point to the front end service version which causes cache misses deep inside postgres.
“PostgreSQL provides facilities to support dynamic tracing of the database server. This allows an external utility to be called at specific points in the code and thereby trace execution.
A number of probes or trace points are already inserted into the source code. These probes are intended to be used by database developers and administrators. By default the probes are not compiled into PostgreSQL; the user needs to explicitly tell the configure script to make the probes available.
Currently, the DTrace utility is supported, which, at the time of this writing, is available on Solaris, macOS, FreeBSD, NetBSD, and Oracle Linux. The SystemTap project for Linux provides a DTrace equivalent and can also be used. Supporting other dynamic tracing utilities is theoretically possible by changing the definitions for the macros in src/include/utils/probes.h.”
Not planning to work on it directly, but I am planning to do some larger executor work as one of the next bigger projects, and having ideas about what kind of information people would like to see could make it easier to implement them later.
Just always collecting that information just in case it may get accessed thus isn't really feasible. It'd be good to make it possible to query cheaper information on-demand though (e.g. asking for the EXPLAIN of a query running in another session, without analyze, should be doable with some effort).
You can already set up things in a way that allows to correlate connections / queries with distributed tracing. But it's a more work than it should be. Postgres' pg_stat_activity shows queries, and it can include information that allows to correlate in the connection's 'application_name'.
[1] https://copyconstruct.medium.com/monitoring-and-observabilit...
Amazing stuff.
I'm not sure if it's the only one but opentelemetry.io does list PostgreSQL.
You need to:
1. Have an Opencensus tracing context
2. Have query logging setup on your PG server. There are a bunch of ways to do this with minimal overhead. You can log slow queries (say queries >5ms), or log queries that fail, or sample queries.
Your query / log ends up getting something like:
2020-11-11 21:00:00 UTC:titusvpcservice@titusvpcservice:
[60294]:HINT: The transaction might succeed if retried.
2020-11-11 21:00:00 UTC:titusvpcservice@titusvpcservice:
[60294]:STATEMENT: /* md: {"spanID":"34c1a9f38fb44cad"} */
INSERT INTO assignments(branch_eni_association, assignment_id) VALUES ($1, $2) RETURNING id
You can then look at a Zipkin, and use the value within MD (spanID) to get the trace. I did this originally, because I wanted to transparently wrap the PG SQL Driver for Zipkin. Postgres can be oblivious to the fact there's "Zipkin Inside", because it's a terminal node. You can get the span ID from the BEGIN TRANSACTION / first query, and then tie that to the pid and timestamp, and then use that to go back and look through things with standard(ish) postgres introspection, since almost all of it has the pg_backend_pid + timestamp in it.[0] https://github.com/influxdata/community-templates/tree/maste... [1] https://github.com/influxdata/telegraf/tree/master/plugins/i...
It's super inspired (read: mostly copy / pasted) from Heroku's "PG Extras" tool, except in this case it all works without Heroku. You just need to be using SQLAlchemy.
I guess I'm asking if you can take postgres and "turn it inside out" by cherry-picking parts of it to build other storage software with it.
Yes.
> I guess I'm asking if you can take postgres and "turn it inside out" by cherry-picking parts of it to build other storage software with it.
Unfortunately many subsystems of postgres are too interdependent to easily be used independently. Including the WAL.
Part of that is historical / unintentional. But there's also plenty places where generalizing subsystems would make the whole slower and/or more complicated.
There definitely are bits that I'd like to have used outside of postgres many times. If you have to write C, something like postgres' memory context API makes it so much more convenient (and often more performant too!).
https://www.postgresql.org/docs/current/spi-memory.html
https://web.archive.org/web/20200224083655/https://blog.pgad...
You can click on them and it will tell you more.
[1] https://github.com/wrouesnel/postgres_exporter