]> begriffs open source - ai-pg/blob - full-docs/txt/file-fdw.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / file-fdw.txt
1
2 F.15. file_fdw — access data files in the server's file system #
3
4    The file_fdw module provides the foreign-data wrapper file_fdw, which
5    can be used to access data files in the server's file system, or to
6    execute programs on the server and read their output. The data file or
7    program output must be in a format that can be read by COPY FROM; see
8    COPY for details. Access to data files is currently read-only.
9
10    A foreign table created using this wrapper can have the following
11    options:
12
13    filename
14           Specifies the file to be read. Relative paths are relative to
15           the data directory. Either filename or program must be
16           specified, but not both.
17
18    program
19           Specifies the command to be executed. The standard output of
20           this command will be read as though COPY FROM PROGRAM were used.
21           Either program or filename must be specified, but not both.
22
23    format
24           Specifies the data format, the same as COPY's FORMAT option.
25
26    header
27           Specifies whether the data has a header line, the same as COPY's
28           HEADER option.
29
30    delimiter
31           Specifies the data delimiter character, the same as COPY's
32           DELIMITER option.
33
34    quote
35           Specifies the data quote character, the same as COPY's QUOTE
36           option.
37
38    escape
39           Specifies the data escape character, the same as COPY's ESCAPE
40           option.
41
42    null
43           Specifies the data null string, the same as COPY's NULL option.
44
45    encoding
46           Specifies the data encoding, the same as COPY's ENCODING option.
47
48    on_error
49           Specifies how to behave when encountering an error converting a
50           column's input value into its data type, the same as COPY's
51           ON_ERROR option.
52
53    reject_limit
54           Specifies the maximum number of errors tolerated while
55           converting a column's input value to its data type, the same as
56           COPY's REJECT_LIMIT option.
57
58    log_verbosity
59           Specifies the amount of messages emitted by file_fdw, the same
60           as COPY's LOG_VERBOSITY option.
61
62    Note that while COPY allows options such as HEADER to be specified
63    without a corresponding value, the foreign table option syntax requires
64    a value to be present in all cases. To activate COPY options typically
65    written without a value, you can pass the value TRUE, since all such
66    options are Booleans.
67
68    A column of a foreign table created using this wrapper can have the
69    following options:
70
71    force_not_null
72           This is a Boolean option. If true, it specifies that values of
73           the column should not be matched against the null string (that
74           is, the table-level null option). This has the same effect as
75           listing the column in COPY's FORCE_NOT_NULL option.
76
77    force_null
78           This is a Boolean option. If true, it specifies that values of
79           the column which match the null string are returned as NULL even
80           if the value is quoted. Without this option, only unquoted
81           values matching the null string are returned as NULL. This has
82           the same effect as listing the column in COPY's FORCE_NULL
83           option.
84
85    COPY's FORCE_QUOTE option is currently not supported by file_fdw.
86
87    These options can only be specified for a foreign table or its columns,
88    not in the options of the file_fdw foreign-data wrapper, nor in the
89    options of a server or user mapping using the wrapper.
90
91    Changing table-level options requires being a superuser or having the
92    privileges of the role pg_read_server_files (to use a filename) or the
93    role pg_execute_server_program (to use a program), for security
94    reasons: only certain users should be able to control which file is
95    read or which program is run. In principle regular users could be
96    allowed to change the other options, but that's not supported at
97    present.
98
99    When specifying the program option, keep in mind that the option string
100    is executed by the shell. If you need to pass any arguments to the
101    command that come from an untrusted source, you must be careful to
102    strip or escape any characters that might have special meaning to the
103    shell. For security reasons, it is best to use a fixed command string,
104    or at least avoid passing any user input in it.
105
106    For a foreign table using file_fdw, EXPLAIN shows the name of the file
107    to be read or program to be run. For a file, unless COSTS OFF is
108    specified, the file size (in bytes) is shown as well.
109
110    Example F.1. Create a Foreign Table for PostgreSQL CSV Logs
111
112    One of the obvious uses for file_fdw is to make the PostgreSQL activity
113    log available as a table for querying. To do this, first you must be
114    logging to a CSV file, which here we will call pglog.csv. First,
115    install file_fdw as an extension:
116 CREATE EXTENSION file_fdw;
117
118    Then create a foreign server:
119 CREATE SERVER pglog FOREIGN DATA WRAPPER file_fdw;
120
121    Now you are ready to create the foreign data table. Using the CREATE
122    FOREIGN TABLE command, you will need to define the columns for the
123    table, the CSV file name, and its format:
124 CREATE FOREIGN TABLE pglog (
125   log_time timestamp(3) with time zone,
126   user_name text,
127   database_name text,
128   process_id integer,
129   connection_from text,
130   session_id text,
131   session_line_num bigint,
132   command_tag text,
133   session_start_time timestamp with time zone,
134   virtual_transaction_id text,
135   transaction_id bigint,
136   error_severity text,
137   sql_state_code text,
138   message text,
139   detail text,
140   hint text,
141   internal_query text,
142   internal_query_pos integer,
143   context text,
144   query text,
145   query_pos integer,
146   location text,
147   application_name text,
148   backend_type text,
149   leader_pid integer,
150   query_id bigint
151 ) SERVER pglog
152 OPTIONS ( filename 'log/pglog.csv', format 'csv' );
153
154    That's it — now you can query your log directly. In production, of
155    course, you would need to define some way to deal with log rotation.
156
157    Example F.2. Create a Foreign Table with an Option on a Column
158
159    To set the force_null option for a column, use the OPTIONS keyword.
160 CREATE FOREIGN TABLE films (
161  code char(5) NOT NULL,
162  title text NOT NULL,
163  rating text OPTIONS (force_null 'true')
164 ) SERVER film_server
165 OPTIONS ( filename 'films/db.csv', format 'csv' );