pgdesign v0.26.0 /internal/livestats
On this page

Package livestats fetches live table statistics from PostgreSQL and shapes them into the generate D2 stats contract, keeping generate database-free.

#internal/livestats

#internal/livestats

Package livestats fetches live table statistics from a running PostgreSQL server and shapes them into the generate.D2Options.Stats contract (a map keyed by model.TableKey). It is the single DB-touching adapter that keeps the generate package DB-free: serve and the CLI build path call Fetch and inject the result, so heat-map / live annotations render without generate ever importing pgx.

#Querier

Go go
type Querier interface

Querier is the minimal query surface Fetch needs. Both pgxpool.Pool and pgx.Conn satisfy it, so serve (pool) and the CLI (conn) share one fetcher.

#Fetch

Go go
func Fetch(ctx context.Context, q Querier, schemaNames []string) (map[string]generate.TableStats, error)

Fetch reads pg_stat_user_tables for the given schemas and returns live statistics keyed by model.TableKey(schema, name), ready to assign to generate.D2Options.Stats. RowCount comes from n_live_tup; SeqScanRatio is seq_scan / (seq_scan + idx_scan), or -1 when a table has never been scanned. Tables absent from pg_stat_user_tables (never accessed) are simply omitted; generate renders no annotation for a key it does not find.

Search