JavaScript response filter - Postgres

The JavaScript response filter is the most flexible of all response filters. It can potentially be called for any packet type, and can modify traffic in any way it sees fit.

Parameters

  • Packet types : optional. If specified, a comma-separated list of packet types for which this filter should be called, for instance: DataRow,EmptyQueryResponse. Spaces are ignored. If left blank, this filter will get called for every packet received from the Postgres database server. Any response packet type can be specified.

Context

The following variables will always be defined when your JavaScript executes:

  • log : the log object

  • context : contains all the variables you'll commonly use:

    • packet : the packet being filtered

    • result : the result object, used to cancel the current packet if desired

    • filterContext: an object containing variables attached to this filter. Any changes to this object will be visible to all invocations of this filter.

    • connectionContext: an object containing variables attached to the connection. Any changes to this object will be visible to all filters defined on this connection.

Example

After a SQL command or query has executed, the server sends a CommandComplete packet summarizing the result. You could define a response filter specifically for this type of packet:

let tag = context.packet.getCommandTag();
if (tag.toLowerCase().startsWith("update ")) {
log.info("Update was completed: " + tag);
}