SQL profiler

A live statement trace of the whole cluster, read from the server log: every completed statement with duration, PID, role, database, bind parameters and errors.

The SQL profiler

What it is for

The SQL as the server received it (also what an ORM generates), with its duration, its bind parameters (extended query protocol) and its error. Unlike the dashboard, which samples pg_stat_activity, it shows every statement.

How it works — and what that means for you

  1. ALTER SYSTEM SET log_min_duration_statement = 0, a machine-readable format added to log_destination, then pg_reload_conf().
  2. Once a second: pg_current_logfile(), pg_stat_file() and pg_read_binary_file() for the new bytes — over the tab's own session, so no file-system access to the host is needed.
  3. On stop the settings are restored.
Requirement
logging_collector = onIf it is off, Start only reports it; pgtc never restarts a server.
PrivilegesSuperuser, or pg_read_server_files plus the right to run ALTER SYSTEM for the two settings and pg_reload_conf(). Nothing is checked in advance: Start or the first read shows the server's error. A Start whose ALTER SYSTEM statements all fail changed nothing and leaves nothing to restore. Managed services usually grant neither.
ScopeBoth settings are cluster-wide: all databases and roles are logged while it runs. See Filtering.

log_min_duration_statement = 0 adds a log write to every statement and makes the log grow fast.

Opening the profiler

Alt+P (also Ctrl+P) on a connected server or one of its databases; Tools → SQL profiler; the context menu.

Start

F5 reads logging_collector, log_destination and log_min_duration_statement with their sourcefile from pg_settings. If something has to change, pgtc lists it and asks:

The question before Start
SettingSet to
log_min_duration_statement0
log_destinationThe current value plus jsonlog (15+) or csvlog (older). If one of them is already written, nothing is added and that format is read.

Reading the list

Column
TimeLog timestamp: when the statement completed, server clock.
Duration msThe record's duration:; empty for an error.
DetailFor an execute: the bind parameters from the record's DETAIL. For an error: the message.
Kindstatement — simple query protocol; execute — Execute of the extended protocol; error. Parse and Bind are not listed.

The statement pane

The selected statement in full. Bind parameters (-- Parameters: $1 = '75123', …) and the error (-- ERROR: …) are put above it as SQL comments, so a copy is still a valid script.

Key
Alt+Down / Alt+UpInto the pane / back.
Ctrl+RHide / show the pane.
Alt+Shift+Up / Alt+Shift+DownSmaller / larger (or drag its upper line).
Ctrl+EThe statement in a new query tab on the database it ran in.

The whole entry

Execution plan

Ctrl+Q (context menu Execution plan…): EXPLAIN of the selected entry's statement — never ANALYZE, so nothing is executed.

The execution plan of a profiler entry

Filtering

Ctrl+F:

The profiler's filter dialog

Pause and clear

Key
Ctrl+PPause: reading stops, the server keeps logging; resume continues at the same file offset.
Ctrl+LClear the list.

Stop

F5 again. A value that came from postgresql.auto.conf before Start is set back with ALTER SYSTEM SET; any other is removed with ALTER SYSTEM RESET; then pg_reload_conf(). The entries stay in the list. A value the session saw from ALTER ROLE / ALTER DATABASE … SET is not the server's: it is never written back with ALTER SYSTEM, and Start sets log_min_duration_statement = 0 cluster-wide even when the session already has 0.

The same happens when the tab is closed, the server is disconnected, or pgtc quits (it waits: Restoring logging settings…) — also right after Yes, before the server has answered.

If pgtc could not put the settings back

After a kill, a crash or a lost session the changed values stay in postgresql.auto.conf.

Answer
YesRuns the ALTER SYSTEM statements and pg_reload_conf().
Not nowAsks again at the next connect.
ForgetDrops the record; the settings stay.

If somebody changed the settings in the meantime, the record is dropped silently. If restoring fails, the record stays. By hand (if the two settings were not set with ALTER SYSTEM before):

ALTER SYSTEM RESET log_min_duration_statement;
ALTER SYSTEM RESET log_destination;
SELECT pg_reload_conf();

The tab status row

The profiler's own session (local · postgres@postgres) and the state: Stopped, Running or Paused, with the entries shown and read.

Limits

All profiler keys

Key
F5Start / stop.
Ctrl+PPause / resume.
Ctrl+FFilter.
Ctrl+LClear the list.
EnterThe whole entry.
Ctrl+QExecution plan (EXPLAIN) of the statement.
Ctrl+EThe statement in a new query tab.
Ctrl+RStatement pane.
Alt+Down / Alt+UpStatement pane / list.
Ctrl+C, Ctrl+ACopy; select all.