2011-02-20 20:06:59 +01:00
|
|
|
<!-- doc/src/sgml/file-fdw.sgml -->
|
|
|
|
|
2011-05-08 04:29:20 +02:00
|
|
|
<sect1 id="file-fdw" xreflabel="file_fdw">
|
2011-02-20 20:06:59 +01:00
|
|
|
<title>file_fdw</title>
|
|
|
|
|
|
|
|
<indexterm zone="file-fdw">
|
|
|
|
<primary>file_fdw</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
|
|
|
<para>
|
2017-10-09 03:44:17 +02:00
|
|
|
The <filename>file_fdw</filename> module provides the foreign-data wrapper
|
2011-02-20 20:06:59 +01:00
|
|
|
<function>file_fdw</function>, which can be used to access data
|
2016-09-29 19:32:27 +02:00
|
|
|
files in the server's file system, or to execute programs on the server
|
|
|
|
and read their output. The data file or program output must be in a format
|
2011-02-20 20:06:59 +01:00
|
|
|
that can be read by <command>COPY FROM</command>;
|
2017-11-23 15:39:47 +01:00
|
|
|
see <xref linkend="sql-copy"/> for details.
|
2016-09-29 19:32:27 +02:00
|
|
|
Access to data files is currently read-only.
|
2011-02-20 20:06:59 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
A foreign table created using this wrapper can have the following options:
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>filename</literal></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2020-08-31 13:03:54 +02:00
|
|
|
Specifies the file to be read. Relative paths are relative to the
|
|
|
|
data directory.
|
2016-09-29 19:32:27 +02:00
|
|
|
Either <literal>filename</literal> or <literal>program</literal> must be
|
|
|
|
specified, but not both.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>program</literal></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Specifies the command to be executed. The standard output of this
|
2017-10-09 03:44:17 +02:00
|
|
|
command will be read as though <command>COPY FROM PROGRAM</command> were used.
|
2016-09-29 19:32:27 +02:00
|
|
|
Either <literal>program</literal> or <literal>filename</literal> must be
|
|
|
|
specified, but not both.
|
2011-02-20 20:06:59 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>format</literal></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2016-09-29 19:32:27 +02:00
|
|
|
Specifies the data format,
|
2017-10-09 03:44:17 +02:00
|
|
|
the same as <command>COPY</command>'s <literal>FORMAT</literal> option.
|
2011-02-20 20:06:59 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>header</literal></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2016-09-29 19:32:27 +02:00
|
|
|
Specifies whether the data has a header line,
|
2017-10-09 03:44:17 +02:00
|
|
|
the same as <command>COPY</command>'s <literal>HEADER</literal> option.
|
2011-02-20 20:06:59 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>delimiter</literal></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2016-09-29 19:32:27 +02:00
|
|
|
Specifies the data delimiter character,
|
2017-10-09 03:44:17 +02:00
|
|
|
the same as <command>COPY</command>'s <literal>DELIMITER</literal> option.
|
2011-02-20 20:06:59 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>quote</literal></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2016-09-29 19:32:27 +02:00
|
|
|
Specifies the data quote character,
|
2017-10-09 03:44:17 +02:00
|
|
|
the same as <command>COPY</command>'s <literal>QUOTE</literal> option.
|
2011-02-20 20:06:59 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>escape</literal></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2016-09-29 19:32:27 +02:00
|
|
|
Specifies the data escape character,
|
2017-10-09 03:44:17 +02:00
|
|
|
the same as <command>COPY</command>'s <literal>ESCAPE</literal> option.
|
2011-02-20 20:06:59 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>null</literal></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2016-09-29 19:32:27 +02:00
|
|
|
Specifies the data null string,
|
2017-10-09 03:44:17 +02:00
|
|
|
the same as <command>COPY</command>'s <literal>NULL</literal> option.
|
2011-02-20 20:06:59 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
2011-02-21 06:08:04 +01:00
|
|
|
<varlistentry>
|
|
|
|
<term><literal>encoding</literal></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2016-09-29 19:32:27 +02:00
|
|
|
Specifies the data encoding,
|
2017-10-09 03:44:17 +02:00
|
|
|
the same as <command>COPY</command>'s <literal>ENCODING</literal> option.
|
2011-02-21 06:08:04 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
2011-02-20 20:06:59 +01:00
|
|
|
</variablelist>
|
|
|
|
|
2013-09-23 20:57:01 +02:00
|
|
|
<para>
|
2017-10-09 03:44:17 +02:00
|
|
|
Note that while <command>COPY</command> allows options such as <literal>HEADER</literal>
|
2016-09-29 19:32:27 +02:00
|
|
|
to be specified without a corresponding value, the foreign table option
|
2014-03-04 23:31:59 +01:00
|
|
|
syntax requires a value to be present in all cases. To activate
|
2017-10-09 03:44:17 +02:00
|
|
|
<command>COPY</command> options typically written without a value, you can pass
|
2016-09-29 19:32:27 +02:00
|
|
|
the value TRUE, since all such options are Booleans.
|
2013-09-23 20:57:01 +02:00
|
|
|
</para>
|
|
|
|
|
2011-02-20 20:06:59 +01:00
|
|
|
<para>
|
2011-09-16 22:35:51 +02:00
|
|
|
A column of a foreign table created using this wrapper can have the
|
|
|
|
following options:
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>force_not_null</literal></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2012-06-07 23:06:20 +02:00
|
|
|
This is a Boolean option. If true, it specifies that values of the
|
2011-09-16 22:35:51 +02:00
|
|
|
column should not be matched against the null string (that is, the
|
2016-09-29 19:32:27 +02:00
|
|
|
table-level <literal>null</literal> option). This has the same effect
|
2017-10-09 03:44:17 +02:00
|
|
|
as listing the column in <command>COPY</command>'s
|
2011-09-16 22:35:51 +02:00
|
|
|
<literal>FORCE_NOT_NULL</literal> option.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
2014-03-04 23:31:59 +01:00
|
|
|
<varlistentry>
|
|
|
|
<term><literal>force_null</literal></term>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
This is a Boolean option. If true, it specifies that values of the
|
2017-10-09 03:44:17 +02:00
|
|
|
column which match the null string are returned as <literal>NULL</literal>
|
2014-03-04 23:31:59 +01:00
|
|
|
even if the value is quoted. Without this option, only unquoted
|
2017-10-09 03:44:17 +02:00
|
|
|
values matching the null string are returned as <literal>NULL</literal>.
|
2014-03-04 23:31:59 +01:00
|
|
|
This has the same effect as listing the column in
|
2017-10-09 03:44:17 +02:00
|
|
|
<command>COPY</command>'s <literal>FORCE_NULL</literal> option.
|
2014-03-04 23:31:59 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
2011-09-16 22:35:51 +02:00
|
|
|
</variablelist>
|
|
|
|
|
|
|
|
<para>
|
2019-06-09 04:25:56 +02:00
|
|
|
<command>COPY</command>'s <literal>FORCE_QUOTE</literal> option is
|
Remove WITH OIDS support, change oid catalog column visibility.
Previously tables declared WITH OIDS, including a significant fraction
of the catalog tables, stored the oid column not as a normal column,
but as part of the tuple header.
This special column was not shown by default, which was somewhat odd,
as it's often (consider e.g. pg_class.oid) one of the more important
parts of a row. Neither pg_dump nor COPY included the contents of the
oid column by default.
The fact that the oid column was not an ordinary column necessitated a
significant amount of special case code to support oid columns. That
already was painful for the existing, but upcoming work aiming to make
table storage pluggable, would have required expanding and duplicating
that "specialness" significantly.
WITH OIDS has been deprecated since 2005 (commit ff02d0a05280e0).
Remove it.
Removing includes:
- CREATE TABLE and ALTER TABLE syntax for declaring the table to be
WITH OIDS has been removed (WITH (oids[ = true]) will error out)
- pg_dump does not support dumping tables declared WITH OIDS and will
issue a warning when dumping one (and ignore the oid column).
- restoring an pg_dump archive with pg_restore will warn when
restoring a table with oid contents (and ignore the oid column)
- COPY will refuse to load binary dump that includes oids.
- pg_upgrade will error out when encountering tables declared WITH
OIDS, they have to be altered to remove the oid column first.
- Functionality to access the oid of the last inserted row (like
plpgsql's RESULT_OID, spi's SPI_lastoid, ...) has been removed.
The syntax for declaring a table WITHOUT OIDS (or WITH (oids = false)
for CREATE TABLE) is still supported. While that requires a bit of
support code, it seems unnecessary to break applications / dumps that
do not use oids, and are explicit about not using them.
The biggest user of WITH OID columns was postgres' catalog. This
commit changes all 'magic' oid columns to be columns that are normally
declared and stored. To reduce unnecessary query breakage all the
newly added columns are still named 'oid', even if a table's column
naming scheme would indicate 'reloid' or such. This obviously
requires adapting a lot code, mostly replacing oid access via
HeapTupleGetOid() with access to the underlying Form_pg_*->oid column.
The bootstrap process now assigns oids for all oid columns in
genbki.pl that do not have an explicit value (starting at the largest
oid previously used), only oids assigned later by oids will be above
FirstBootstrapObjectId. As the oid column now is a normal column the
special bootstrap syntax for oids has been removed.
Oids are not automatically assigned during insertion anymore, all
backend code explicitly assigns oids with GetNewOidWithIndex(). For
the rare case that insertions into the catalog via SQL are called for
the new pg_nextoid() function can be used (which only works on catalog
tables).
The fact that oid columns on system tables are now normal columns
means that they will be included in the set of columns expanded
by * (i.e. SELECT * FROM pg_class will now include the table's oid,
previously it did not). It'd not technically be hard to hide oid
column by default, but that'd mean confusing behavior would either
have to be carried forward forever, or it'd cause breakage down the
line.
While it's not unlikely that further adjustments are needed, the
scope/invasiveness of the patch makes it worthwhile to get merge this
now. It's painful to maintain externally, too complicated to commit
after the code code freeze, and a dependency of a number of other
patches.
Catversion bump, for obvious reasons.
Author: Andres Freund, with contributions by John Naylor
Discussion: https://postgr.es/m/20180930034810.ywp2c7awz7opzcfr@alap3.anarazel.de
2018-11-21 00:36:57 +01:00
|
|
|
currently not supported by <literal>file_fdw</literal>.
|
2011-02-20 20:06:59 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2011-09-16 22:35:51 +02:00
|
|
|
These options can only be specified for a foreign table or its columns, not
|
2017-10-09 03:44:17 +02:00
|
|
|
in the options of the <literal>file_fdw</literal> foreign-data wrapper, nor in the
|
2011-02-20 20:06:59 +01:00
|
|
|
options of a server or user mapping using the wrapper.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2018-04-06 20:47:10 +02:00
|
|
|
Changing table-level options requires being a superuser or having the privileges
|
2021-04-01 21:32:06 +02:00
|
|
|
of the role <literal>pg_read_server_files</literal> (to use a filename) or
|
|
|
|
the role <literal>pg_execute_server_program</literal> (to use a program),
|
2018-04-06 20:47:10 +02:00
|
|
|
for security reasons: only certain users should be able to control which file is
|
|
|
|
read or which program is run. In principle regular users could be allowed to
|
2016-09-29 19:32:27 +02:00
|
|
|
change the other options, but that's not supported at present.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2017-10-09 03:44:17 +02:00
|
|
|
When specifying the <literal>program</literal> option, keep in mind that the option
|
2016-09-29 19:32:27 +02:00
|
|
|
string is executed by the shell. If you need to pass any arguments to the
|
|
|
|
command that come from an untrusted source, you must be careful to strip or
|
|
|
|
escape any characters that might have special meaning to the shell.
|
|
|
|
For security reasons, it is best to use a fixed command string, or at least
|
|
|
|
avoid passing any user input in it.
|
2011-02-20 20:06:59 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2017-10-09 03:44:17 +02:00
|
|
|
For a foreign table using <literal>file_fdw</literal>, <command>EXPLAIN</command> shows
|
2016-09-29 19:32:27 +02:00
|
|
|
the name of the file to be read or program to be run.
|
2017-10-09 03:44:17 +02:00
|
|
|
For a file, unless <literal>COSTS OFF</literal> is
|
2011-02-20 20:06:59 +01:00
|
|
|
specified, the file size (in bytes) is shown as well.
|
|
|
|
</para>
|
|
|
|
|
2011-12-01 15:33:59 +01:00
|
|
|
<example>
|
2020-03-13 15:45:37 +01:00
|
|
|
<title>Create a Foreign Table for PostgreSQL CSV Logs</title>
|
2013-03-10 19:14:53 +01:00
|
|
|
|
2011-12-01 15:33:59 +01:00
|
|
|
<para>
|
2017-10-09 03:44:17 +02:00
|
|
|
One of the obvious uses for <literal>file_fdw</literal> is to make
|
2011-12-01 15:33:59 +01:00
|
|
|
the PostgreSQL activity log available as a table for querying. To
|
2020-08-31 22:59:59 +02:00
|
|
|
do this, first you must be <link
|
|
|
|
linkend="runtime-config-logging-csvlog">logging to a CSV file,</link>
|
|
|
|
which here we
|
2017-10-09 03:44:17 +02:00
|
|
|
will call <literal>pglog.csv</literal>. First, install <literal>file_fdw</literal>
|
2011-12-01 15:33:59 +01:00
|
|
|
as an extension:
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
CREATE EXTENSION file_fdw;
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
<para>
|
2013-01-25 16:13:41 +01:00
|
|
|
Then create a foreign server:
|
2011-12-01 15:33:59 +01:00
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
CREATE SERVER pglog FOREIGN DATA WRAPPER file_fdw;
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Now you are ready to create the foreign data table. Using the
|
2017-10-09 03:44:17 +02:00
|
|
|
<command>CREATE FOREIGN TABLE</command> command, you will need to define
|
2012-06-07 23:06:20 +02:00
|
|
|
the columns for the table, the CSV file name, and its format:
|
2011-12-01 15:33:59 +01:00
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
CREATE FOREIGN TABLE pglog (
|
|
|
|
log_time timestamp(3) with time zone,
|
|
|
|
user_name text,
|
|
|
|
database_name text,
|
|
|
|
process_id integer,
|
|
|
|
connection_from text,
|
|
|
|
session_id text,
|
|
|
|
session_line_num bigint,
|
|
|
|
command_tag text,
|
|
|
|
session_start_time timestamp with time zone,
|
|
|
|
virtual_transaction_id text,
|
|
|
|
transaction_id bigint,
|
|
|
|
error_severity text,
|
|
|
|
sql_state_code text,
|
|
|
|
message text,
|
|
|
|
detail text,
|
|
|
|
hint text,
|
|
|
|
internal_query text,
|
|
|
|
internal_query_pos integer,
|
|
|
|
context text,
|
|
|
|
query text,
|
|
|
|
query_pos integer,
|
|
|
|
location text,
|
2020-03-23 23:38:42 +01:00
|
|
|
application_name text,
|
2020-09-16 11:47:39 +02:00
|
|
|
backend_type text,
|
2021-04-08 04:30:30 +02:00
|
|
|
leader_pid integer,
|
|
|
|
query_id bigint
|
2011-12-01 15:33:59 +01:00
|
|
|
) SERVER pglog
|
2020-09-09 12:20:53 +02:00
|
|
|
OPTIONS ( filename 'log/pglog.csv', format 'csv' );
|
2011-12-01 15:33:59 +01:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2013-03-10 19:14:53 +01:00
|
|
|
That's it — now you can query your log directly. In production, of
|
|
|
|
course, you would need to define some way to deal with log rotation.
|
2011-12-01 15:33:59 +01:00
|
|
|
</para>
|
|
|
|
</example>
|
|
|
|
|
2011-02-20 20:06:59 +01:00
|
|
|
</sect1>
|