Skip to main content
Version: 1.0.16

file_fdw

The file_fdw module provides the foreign data wrapper file_fdw, which can be used to access data files in the server's file system, or to execute programs on the server and read their output. Data files or program output must be in a format readable by COPY FROM.

A foreign table created with this wrapper can have the following options:

filename specifies the file to be read. A relative path is relative to the data directory. Either filename or program must be specified, but not both.

program specifies the command to be executed. The standard output of the command will be read, as if using COPY FROM PROGRAM. Either program or filename must be specified, but not both.

format specifies the data format, the same as COPY's FORMAT option.

header specifies whether the data has a header row, the same as COPY's HEADER option.

delimiter specifies the data delimiter character, the same as COPY's DELIMITER option.

quote specifies the data quote character, the same as COPY's QUOTE option.

escape specifies the data escape character, the same as COPY's ESCAPE option.

null specifies the data null string, the same as COPY's NULL option.

encoding specifies the data encoding, the same as COPY's ENCODING option.

Note: While COPY allows options such as HEADER to be specified without a corresponding value, the foreign table option syntax requires a value in all cases. To activate COPY options that are normally written without a value, you can pass the value TRUE, since all these options are boolean. A column of a table created with this wrapper can have the following options: force_not_null is a boolean option. If true, it specifies that the column's values should not be matched against the null string (i.e., the table-level null option). This has the same effect as listing the column in COPY's FORCE_NOT_NULL option.

force_null is a boolean option. If true, it specifies that column values matching the null string will be returned as NULL, even if the value is quoted. Without this option, only unquoted values matching the null string are returned as NULL. This has the same effect as listing the column in COPY's FORCE_NULL option.

COPY's FORCE_QUOTE option is currently not supported by file_fdw.

These options can only be specified for a foreign table and its columns, not in the options of the file_fdw foreign data wrapper itself, nor in the options of a server or user mapping using the wrapper.

For security reasons, changing table-level options requires superuser privileges or the default role pg_read_server_files (when using filename) or the default role pg_execute_server_program (when using program): only specific users can control which file is read or which program is run. In principle, ordinary users could be allowed to change other options, but this is not currently supported.

When the program option is specified, remember that the option string is executed through the shell. If you want to pass any arguments to a command from an untrusted source, you must be careful to remove or escape any characters that may have special meaning to the shell. For security, it is best to use a fixed command string, or at least avoid passing any user input.

For a foreign table using file_fdw, EXPLAIN shows the file name to be read or the program to be run. For files, the file size (in bytes) is also displayed unless COSTS OFF is specified.

Example C.1. Creating a Foreign Table for CSV Logs

One use of file_fdw is to make the available Halo activity logs into a queryable table. To do this, you must first write the logs to a CSV file, which we will call pglog.csv here. First, install file_fdw as an extension:

test=## CREATE EXTENSION file_fdw;

CREATE EXTENSION

Then create a foreign server:

test=## CREATE SERVER pglog FOREIGN DATA WRAPPER file_fdw;

CREATE SERVER

Now you are ready to create the foreign data table. Using the CREATE FOREIGN TABLE command, you will need to define the columns for the table, the CSV file name, and the format:

test=## CREATE FOREIGN TABLE pglog (

test(## log_time timestamp(3) with time zone,

test(## user_name text,

test(## database_name text,

test(## process_id integer,

test(## connection_from text,

test(## session_id text,

test(## session_line_num bigint,

test(## command_tag text,

test(## session_start_time timestamp with time zone, virtual_transaction_id text,

test(## transaction_id bigint,

test(## error_severity text,

test(## sql_state_code text,

test(## message text,

test(## detail text,

test(## hint text, internal_query text,

test(## internal_query_pos integer,

test(## context text,

test(## query text,

test(## query_pos integer,

test(## location text,

test(## application_name text,

test(## backend_type text

test(## ) SERVER pglog

test-## OPTIONS ( filename 'log/pglog.csv', format 'csv' );

CREATE FOREIGN TABLE

Now you can query your logs directly. Of course, in production you would need to define some method for handling log rotation.