This article was written in 2018. The table definitions below use the 23-column PostgreSQL 10 CSV format, and the Filebeat example uses the legacy prospectors configuration.
CSV logs can be imported into PostgreSQL tables or queried through file_fdw for analysis.
The last three settings collect runtime statistics; they do not control log output.
logging_collector and track_activity_query_size require a server restart.
The weekday filename repeats each week. Configure rotation and retention to match
how long you need to keep the files.
Log Collection
The original setup used Filebeat to forward local files to Kafka. Replace the
paths and broker address with your own. This legacy example starts at the end of
existing files and joins lines by a timestamp pattern, with a 20-line limit. It
does not provide general CSV record framing for arbitrary multiline messages.
PostgreSQL can query its logs as rows. The following table matches the
PostgreSQL 10 CSV fields.
Later server versions add columns; use the table definition for the server that
wrote the log:
Import a closed CSV log file with COPY. The server must be able to read the
file. For a file on the client machine, use psql’s \copy. Import complete files after rotation, and import each file only once.
Use file_fdw to query local CSV files without copying them into a table. Run the
setup with the required extension and server-file privileges. The example assumes
English weekday names and existing files under /pg/data/log; adjust the paths
to match the log output. Query an individual child table if some weekday files
do not yet exist.
CREATESCHEMAIFNOTEXISTSmonitor;-- This session only
SETsearch_path=monitor,pg_catalog;-- extension
CREATEEXTENSIONIFNOTEXISTSfile_fdwWITHSCHEMAmonitor;-- log parent table: empty
CREATETABLEmonitor.pg_log(log_timetimestamp(3)withtimezone,user_nametext,database_nametext,process_idinteger,connection_fromtext,session_idtext,session_line_numbigint,command_tagtext,session_start_timetimestampwithtimezone,virtual_transaction_idtext,transaction_idbigint,error_severitytext,sql_state_codetext,messagetext,detailtext,hinttext,internal_querytext,internal_query_posinteger,contexttext,querytext,query_posinteger,locationtext,application_nametext,PRIMARYKEY(session_id,session_line_num));COMMENTONTABLEmonitor.pg_logIS'PostgreSQL csv log schema';-- local file server
CREATESERVERIFNOTEXISTSpg_logFOREIGNDATAWRAPPERfile_fdw;-- Change filename to actual path
CREATEFOREIGNTABLEIFNOTEXISTSmonitor.pg_log_mon()INHERITS(monitor.pg_log)SERVERpg_logOPTIONS(filename'/pg/data/log/postgresql-Mon.csv',format'csv');CREATEFOREIGNTABLEIFNOTEXISTSmonitor.pg_log_tue()INHERITS(monitor.pg_log)SERVERpg_logOPTIONS(filename'/pg/data/log/postgresql-Tue.csv',format'csv');CREATEFOREIGNTABLEIFNOTEXISTSmonitor.pg_log_wed()INHERITS(monitor.pg_log)SERVERpg_logOPTIONS(filename'/pg/data/log/postgresql-Wed.csv',format'csv');CREATEFOREIGNTABLEIFNOTEXISTSmonitor.pg_log_thu()INHERITS(monitor.pg_log)SERVERpg_logOPTIONS(filename'/pg/data/log/postgresql-Thu.csv',format'csv');CREATEFOREIGNTABLEIFNOTEXISTSmonitor.pg_log_fri()INHERITS(monitor.pg_log)SERVERpg_logOPTIONS(filename'/pg/data/log/postgresql-Fri.csv',format'csv');CREATEFOREIGNTABLEIFNOTEXISTSmonitor.pg_log_sat()INHERITS(monitor.pg_log)SERVERpg_logOPTIONS(filename'/pg/data/log/postgresql-Sat.csv',format'csv');CREATEFOREIGNTABLEIFNOTEXISTSmonitor.pg_log_sun()INHERITS(monitor.pg_log)SERVERpg_logOPTIONS(filename'/pg/data/log/postgresql-Sun.csv',format'csv');
Extracting Log Fields
These functions extract duration, statement text, and IPv4-shaped text from
English log messages. Durations are in milliseconds. They handle the message
patterns shown here, not every extended-query or localized message format: