Skip to main content

tracingChannels

import { tracingChannels } from 'postgrejs';

The node:diagnostics_channel tracing channels postgrejs publishes on, since 3.15.0. Each value is a Node TracingChannel, typed with the context it publishes; the same channel is also reachable by name with diagnosticsChannel.tracingChannel('postgrejs:query') and so on. A subscriber gets start, end, asyncStart, asyncEnd and error events with the same context object for one operation. Nothing is published while a channel has no subscribers.

See the Tracing Channels guide for usage.

KeyChannel nameContext
querypostgrejs:queryQueryTraceContext
executepostgrejs:executeExecuteTraceContext
preparepostgrejs:preparePrepareTraceContext
statementExecutepostgrejs:statement:executeStatementExecuteTraceContext
pipelinepostgrejs:pipelinePipelineTraceContext
cursorFetchpostgrejs:cursor:fetchCursorFetchTraceContext
copypostgrejs:copyCopyTraceContext
poolAcquirepostgrejs:pool:acquirePoolAcquireTraceContext
connectpostgrejs:connectConnectTraceContext

Every context below also has error?: unknown, set once the operation fails. Where a context has a writable sql, a subscriber may replace it in start and postgrejs sends the replaced text. See Changing the SQL that is sent.

QueryTraceContext​

Published on postgrejs:query for one Connection.query(), and for pool.query() through the connection it lands on.

KeyTypeDescription
sqlstringThe statement as it will be sent. Writable in start
paramCountnumberHow many bind parameters it carries. Their values are not published
connectionConnectionThe connection that runs the statement
resultQueryResultSet once the query resolves

ExecuteTraceContext​

Published on postgrejs:execute for one Connection.execute() (the Simple Query protocol, one or more statements in a script), and for each transaction command a transaction method sends.

KeyTypeDescription
sqlstringThe script as it will be sent. Writable in start
connectionConnectionThe connection that runs the script
commandTransactionCommandSet when a transaction method sent the script, to the command it is
resultScriptResultSet once the script resolves

A transaction method publishes only when it sends something: a nested startTransaction() or commit() that only moves the depth count publishes nothing. The BEGIN an autoCommit: false statement opens for itself, and the savepoint rollbackOnError puts around a statement, are part of that statement and are not published.

TransactionCommand​

type TransactionCommand =
| 'BEGIN'
| 'COMMIT'
| 'ROLLBACK'
| 'SAVEPOINT'
| 'RELEASE SAVEPOINT'
| 'ROLLBACK TO SAVEPOINT'
| 'PREPARE TRANSACTION'
| 'COMMIT PREPARED'
| 'ROLLBACK PREPARED';

The commands postgrejs sends for startTransaction(), commit(), rollback(), the savepoint methods and two-phase commit.

PrepareTraceContext​

Published on postgrejs:prepare for one Connection.prepare() or pool.prepare() call: the Parse/Describe round trip that creates a prepared statement on the server. copyFromRows() publishes here too when it looks up the columns of its table before the COPY; table is set then, and result is not, since no prepared statement is left behind.

KeyTypeDescription
sqlstringThe statement as it will be prepared. Writable in start; the replaced text is what every later execution runs
paramTypeCountnumberHow many parameter types the caller declared. The server may resolve more
connectionConnectionThe connection the statement is prepared on
tablestringThe table whose columns copyFromRows() is looking up
resultPreparedStatementThe prepared statement, once the server has accepted it

StatementExecuteTraceContext​

Published on postgrejs:statement:execute for one execute() or executeBatch() call on a prepared statement. A statement that Connection.query() prepares and runs for itself, as it does for a cursor, is published on postgrejs:query only.

KeyTypeDescription
sqlstringThe text the statement was prepared with. Read-only: an execution sends no statement text
statementPreparedStatementThe prepared statement being run
connectionConnectionThe connection the statement runs on
paramCountnumberHow many bind parameters each execution carries: the length of the first set of a batch
batchSizenumberHow many parameter sets an executeBatch() runs. Not set for execute()
resultQueryResult | BatchResultA QueryResult from execute(), a BatchResult from executeBatch()

PipelineTraceContext​

Published on postgrejs:pipeline for one Connection.pipeline() call: every statement sent before any reply is read, closed by a single Sync. When the server rejects one, error.failedIndex says which.

KeyTypeDescription
statements{ sql: string; paramCount: number }[]The statements, in the order they are sent. Each sql is writable in start; parameter values are not published
connectionConnectionThe connection that runs the pipeline
resultQueryResult[]One result per statement, by position

CursorFetchTraceContext​

Published on postgrejs:cursor:fetch for each batch a cursor reads: one Execute of the portal behind it, for up to fetchCount rows, and the decoding of the rows that come back. The statement that opened the cursor is published on postgrejs:query or postgrejs:statement:execute, and is over by the time the first batch is read.

KeyTypeDescription
sqlstringThe statement the cursor reads the result of. Read-only
connectionConnectionThe connection the cursor reads from
cursorCursorThe cursor reading the batch
fetchCountnumberHow many rows were asked for
rowCountnumberHow many rows came back
donebooleantrue when no rows came back, so the cursor closed itself

CopyTraceContext​

Published on postgrejs:copy for one COPY, from copyTo(), copyFrom() or copyFromRows(). The operation lasts from the moment the statement is sent until the server reports it finished, not until copyTo() or copyFrom() resolves; those resolve as soon as the server accepts the copy. rowCount and bytes are set by the time it ends, whether it succeeded or not.

KeyTypeDescription
sqlstringThe COPY statement as it will be sent. Writable in start
direction'to' | 'from'to for COPY ... TO STDOUT, from for COPY ... FROM STDIN
connectionConnectionThe connection that runs the copy
tablestringThe destination table, when copyFromRows() was given one by name
rowCountnumberRows the server reported for the copy
bytesnumberBytes of CopyData sent or received
abortedbooleantrue when a copyTo() stream was destroyed by its consumer before the end. The server still finishes the copy and its output is discarded, so this is not an error

PoolAcquireTraceContext​

Published on postgrejs:pool:acquire while a caller waits for a connection from a pool, including the time it takes to open a new one.

KeyTypeDescription
poolPoolThe pool being waited on
resultConnectionThe connection handed out

ConnectTraceContext​

Published on postgrejs:connect while a new connection is opened: the socket, TLS, authentication and the startup commands.

KeyTypeDescription
configConnectionConfigurationThe configuration being connected with. It includes the password, so don't log it as it is
poolPoolThe pool opening the connection, when a pool is
processIDnumberThe server's backend process id, set once the connection is ready
hoststringThe host connected to, set when the attempt ends: with several hosts configured, the one reached, or the last one tried if none was
portnumberThe port of host