Of course, besides copying logs directly to data tables for analysis, there’s another method that allows PostgreSQL to directly map its local CSVLOG as a foreign table for SQL-based direct access.
CREATESCHEMAIFNOTEXISTSmonitor;-- search path for su
ALTERROLEpostgresSETsearch_path=public,monitor;SETsearch_path=public,monitor;-- 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');
Processing Logs
You can use the following stored procedures to further extract statement execution times from log messages: