log_line_prefix
Read PG 18 manual ↗This is a printf-style string that is output at the beginning of each log line.
- Type
- string
- Context
- sighup
- Measured default
- %m [%p]
- Unit
- —
- Metadata snapshot
- 18
Definition PG 18 manual
This is a printf-style string that is output at the beginning of each log line. % characters begin “escape sequences” that are replaced with status information as outlined below. Unrecognized escapes are ignored. Other characters are copied straight to the log line. Some escapes are only recognized by session processes, and will be treated as empty by background processes such as the main server process. Status information may be aligned either left or right by specifying a numeric literal after the % and before the option. A negative value will cause the status information to be padded on the right with spaces to give it a minimum width, whereas a positive value will pad on the left. Padding can be useful to aid human readability in log files.
This parameter can only be set in the postgresql.conf file or on the server command line. The default is '%m [%p] ' which logs a time stamp and the process ID.
| Escape | Effect | Session only |
|---|---|---|
%a | Application name | yes |
%u | User name | yes |
%d | Database name | yes |
%r | Remote host name or IP address, and remote port | yes |
%h | Remote host name or IP address | yes |
%L | Local address (the IP address on the server that the client connected to) | yes |
%b | Backend type | no |
%p | Process ID | no |
%P | Process ID of the parallel group leader, if this process is a parallel query worker | no |
%t | Time stamp without milliseconds | no |
%m | Time stamp with milliseconds | no |
%n | Time stamp with milliseconds (as a Unix epoch) | no |
%i | Command tag: type of session's current command | yes |
%e | SQLSTATE error code | no |
%c | Session ID: see below | no |
%l | Number of the log line for each session or process, starting at 1 | no |
%s | Process start time stamp | no |
%v | Virtual transaction ID (procNumber/localXID); see Section 67.1 | no |
%x | Transaction ID (0 if none is assigned); see Section 67.1 | no |
%q | Produces no output, but tells non-session processes to stop at this point in the string; ignored by session processes | no |
%Q | Query identifier of the current query. Query identifiers are not computed by default, so this field will be zero unless compute_query_id parameter is enabled or a third-party module that computes query identifiers is configured. | yes |
%% | Literal % | no |
The backend type corresponds to the column backend_type in the view pg_stat_activity, but additional types can appear in the log that don't show in that view.
The %c escape prints a quasi-unique session identifier, consisting of two 4-byte hexadecimal numbers (without leading zeros) separated by a dot. The numbers are the process start time and the process ID, so %c can also be used as a space saving way of printing those items. For example, to generate the session identifier from pg_stat_activity, use this query:
SELECT to_hex(trunc(EXTRACT(EPOCH FROM backend_start))::integer) || '.' ||
to_hex(pid)
FROM pg_stat_activity;
If you set a nonempty value for log_line_prefix, you should usually make its last character be a space, to provide visual separation from the rest of the log line. A punctuation character can be used too.
Syslog produces its own time stamp and process ID information, so you probably do not want to include those escapes if you are logging to syslog.
The %q escape is useful when including information that is only available in session (backend) context like user or database name. For example:
log_line_prefix = '%m [%p] %q%u@%d/%a '
The %Q escape always reports a zero identifier for lines output by log_statement because log_statement generates output before an identifier can be calculated, including invalid statements for which an identifier cannot be calculated.
Measured default history
| Version interval | Default |
|---|---|
| 9.0 – 9.6 | Empty string |
| 10 – 19 | %m [%p] |
Authored guidance from the GUC source snapshot; the version-specific manual above is the definition reference. View source ↗
How it works
log_line_prefix controls information prefixed to each log line. An empty string means no prefix. Percent escapes add session, user, database, process, application, and timing context; csvlog and jsonlog already carry structured fields separately.
log_line_prefix is a SIGHUP-context setting: a configuration reload activates the new server value without a restart; subsequent operations that consult it use the refreshed value.
It changes emitted diagnostic data rather than query semantics, but volume, sensitive content, log_line_prefix, destinations, collector throughput, and retention determine operational cost and usefulness.
Operational considerations
Editing log_line_prefix without reloading configuration and verifying the effective value and subsequent behavior.
Enabling richer logging without budgeting collector throughput, storage, retention, and downstream query cost.
Writing SQL text, bind values, identities, or host data without a redaction and access-control policy.
Omitting process, session, database, user, or application identity needed to correlate multiline text logs.
Workload guidance
OLAP: Analytical jobs can justify richer log_line_prefix telemetry, but account for long statements, large bind values, and bursty completion patterns in the log pipeline.
OLTP: Tune log_line_prefix against an explicit observability question and a measured log-volume budget. Prefer selective thresholds, sampling, or role-level overrides over indiscriminate capture.
SMALL: Keep log_line_prefix useful but bounded: verify disk, collector, retention, and redaction capacity before increasing detail or frequency.
Version history 10
- PG 17 → 18changed
- PG 16 → 17changed
- PG 15 → 16changed
- PG 13 → 14changed
- PG 12 → 13changed
- PG 9.6 → 10changed
- PG 9.5 → 9.6changed
- PG 9.4 → 9.5changed
- PG 9.3 → 9.4changed
- PG 9.1 → 9.2changed
Related entries
Further reading
Definition snapshot: english-manuals:cf479a91b9437ec2bb5d3bd6a53… · English manual source