This is a PostgreSQL extension built using Foreign-Data Wrapper facility to
enable reading log files via SQL. It basically provides SQL interface to create
foreign tables for each PostgreSQL log file through which the file contents can
be read and analyzed. Only superusers are allowed to create this extension.
SQL functions
To create foreign table, use:
create_foreign_table_for_log_file(IN table_name TEXT, IN server_name TEXT, IN log_file_name TEXT)
To list files and their sizes present in PostgreSQL log directory, use:
list_postgres_log_files(OUT file_name TEXT, OUT file_size_bytes BIGINT)
The file name must be a plain name from that listing. A foreign table can only
read files directly inside log_directory: absolute paths, directory
separators, . and .. are rejected, both when the table is created and
every time it is read. That also means the contents of log_directory are the
threat model. If it is set to . (the data directory), files such as
postgresql.conf and pg_hba.conf are readable through log_fdw by anyone who
can create a foreign table on the server.
Note that list_postgres_log_files() function is a wrapper around PostgreSQL’s
core function pg_ls_logdir
and exists for compatibility reasons.
By default, use of this extension’s functions is restricted to superusers.
Access may be granted by superusers to others using GRANT as needed.
For instance, following are the minimal things that one needs to do for
enabling others to use the extension’s functions:
CREATE ROLE foo; -- a non-superuser
GRANT pg_monitor TO foo; -- do this only when list_postgres_log_files() is used because the underlying function pg_ls_logdir() needs it
GRANT CREATE ON SCHEMA bar TO foo; -- to create foreign tables in schema named bar
GRANT USAGE ON FOREIGN SERVER log_fdw_server TO foo; -- to use log_fdw foreign server
SET ROLE foo;
SELECT * FROM create_foreign_table_for_log_file('log_file_tbl', 'log_fdw_server', 'log_file.csv');
Building inside a PostgreSQL source tree is not covered by meson.build; use
the Makefile for that.
Running the tests
The tests need a server with logging_collector enabled, so they always run
against a temporary instance configured from log_fdw.conf.
Build
Command
In a PostgreSQL source tree (contrib/postgresql-logfdw)
make check
Out of tree, Makefile
make USE_PGXS=1 install, then make USE_PGXS=1 standalone-check
Out of tree, meson
ninja -C build install, then meson test -C build
Both out-of-tree targets require the extension to be installed first, because
pg_regress resolves CREATE EXTENSION through the server’s own sharedir
and pkglibdir. Installing is a separate step because it usually needs
privileges (sudo) that the test must not run with; a test run against an
installed module older than the one just built is refused rather than passing
silently. Do not combine the two into make -j install standalone-check: GNU
make runs command-line goals in parallel under -j. make check is not
available out of tree (PGXS defines it as a stub) and installcheck is
intentionally disabled, hence standalone-check.
postgres=# \df
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+-----------------------------------+------------------+------------------------------------------------------------------------------+------
public | create_foreign_table_for_log_file | void | table_name text, server_name text, log_file_name text | func
public | create_foreign_table_for_log_file | void | table_name text, server_name text, log_file_name text, if_not_exists boolean | func
public | list_postgres_log_files | SETOF record | OUT file_name text, OUT file_size_bytes bigint | func
public | log_fdw_handler | fdw_handler | | func
public | log_fdw_validator | void | text[], oid | func
(5 rows)
postgres=# SELECT * FROM list_postgres_log_files() ORDER BY 1 DESC LIMIT 2;
file_name | file_size_bytes
---------------------------+-----------------
postgresql-2022-11-28.log | 1754
postgresql-2022-11-28.csv | 1948
(2 rows)
Create server:
postgres=# CREATE SERVER log_fdw_server FOREIGN DATA WRAPPER log_fdw;
CREATE SERVER
Create foreign tables from csv files and log files:
postgres=# SELECT * FROM create_foreign_table_for_log_file('postgresql_2022_11_28_csv','log_fdw_server','postgresql-2022-11-28.csv');
create_foreign_table_for_log_file
-----------------------------------
(1 row)
postgres=# SELECT * FROM create_foreign_table_for_log_file('postgresql_2022_11_28_log','log_fdw_server','postgresql-2022-11-28.log');
create_foreign_table_for_log_file
-----------------------------------
(1 row)
See foreign tables created:
postgres=# \detr
List of foreign tables
Schema | Table | Server
--------+---------------------------+----------------
public | postgresql_2022_11_28_csv | log_fdw_server
public | postgresql_2022_11_28_log | log_fdw_server
(2 rows)
Read log file contents via foreign tables created:
SELECT * FROM postgresql_2022_11_14_log LIMIT 2;
postgres=# \x
Expanded display is on.
postgres=# select * from postgresql_2022_11_28_log limit 2;
-[ RECORD 1 ]---------------------------------------------------------------------------------------------------------------------------
log_entry | 2022-11-28 20:37:51.767 UTC 14170 637e8d69.375a 7 2022-11-23 21:15:21 UTC 0 00000LOG: received fast shutdown request
-[ RECORD 2 ]---------------------------------------------------------------------------------------------------------------------------
log_entry | 2022-11-28 20:37:51.769 UTC 14170 637e8d69.375a 8 2022-11-23 21:15:21 UTC 0 00000LOG: aborting any active transactions
postgres=# DROP EXTENSION log_fdw CASCADE;
NOTICE: drop cascades to 3 other objects
DETAIL: drop cascades to server log_fdw_server
drop cascades to foreign table postgresql_2022_11_28_csv
drop cascades to foreign table postgresql_2022_11_28_log
DROP EXTENSION
Compatibility with PostgreSQL
This extension currently works well with PostgreSQL version 14, 15 and 16devel.
log_fdw
This is a PostgreSQL extension built using Foreign-Data Wrapper facility to enable reading log files via SQL. It basically provides SQL interface to create foreign tables for each PostgreSQL log file through which the file contents can be read and analyzed. Only superusers are allowed to create this extension.
SQL functions
To create foreign table, use:
To list files and their sizes present in PostgreSQL log directory, use:
The file name must be a plain name from that listing. A foreign table can only read files directly inside
log_directory: absolute paths, directory separators,.and..are rejected, both when the table is created and every time it is read. That also means the contents oflog_directoryare the threat model. If it is set to.(the data directory), files such aspostgresql.confandpg_hba.confare readable through log_fdw by anyone who can create a foreign table on the server. Note thatlist_postgres_log_files()function is a wrapper around PostgreSQL’s core function pg_ls_logdir and exists for compatibility reasons.By default, use of this extension’s functions is restricted to superusers. Access may be granted by superusers to others using GRANT as needed. For instance, following are the minimal things that one needs to do for enabling others to use the extension’s functions:
Quick install instructions
Clone the repository from https://github.com/aws/postgresql-logfdw:
Building with make
pg_configof the target installation must be onPATH:Building with meson
meson.buildbuilds against an installed PostgreSQL (14 or later) located throughpg_config:Building inside a PostgreSQL source tree is not covered by
meson.build; use the Makefile for that.Running the tests
The tests need a server with
logging_collectorenabled, so they always run against a temporary instance configured fromlog_fdw.conf.contrib/postgresql-logfdw)make checkmake USE_PGXS=1 install, thenmake USE_PGXS=1 standalone-checkninja -C build install, thenmeson test -C buildBoth out-of-tree targets require the extension to be installed first, because
pg_regressresolvesCREATE EXTENSIONthrough the server’s ownsharedirandpkglibdir. Installing is a separate step because it usually needs privileges (sudo) that the test must not run with; a test run against an installed module older than the one just built is refused rather than passing silently. Do not combine the two intomake -j install standalone-check: GNU make runs command-line goals in parallel under-j.make checkis not available out of tree (PGXS defines it as a stub) andinstallcheckis intentionally disabled, hencestandalone-check.Usage
Create extension:
See functions created by extension:
Create server:
Create foreign tables from csv files and log files:
See foreign tables created:
Read log file contents via foreign tables created:
SELECT * FROM postgresql_2022_11_14_log LIMIT 2;
SELECT * FROM postgresql_2022_11_28_csv LIMIT 2;
Remove extension:
DROP EXTENSION log_fdw CASCADE;
Compatibility with PostgreSQL
This extension currently works well with PostgreSQL version 14, 15 and 16devel.
LICENSE
See LICENSE for detailed information.
Contributing
See CODE_OF_CONDUCT and CONTRIBUTING for detailed information.