Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
F.15. file_fdw — access data files in the server's file system
Prev UpAppendix F. Additional Supplied Modules and ExtensionsHome Next

F.15. file_fdw — access data files in the server's file system#

Thefile_fdw module provides the foreign-data wrapperfile_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. The data file or program output must be in a format that can be read byCOPY FROM; seeCOPY for details. Access to data files is currently read-only.

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

filename

Specifies the file to be read. Relative paths are relative to the data directory. Eitherfilename orprogram must be specified, but not both.

program

Specifies the command to be executed. The standard output of this command will be read as thoughCOPY FROM PROGRAM were used. Eitherprogram orfilename must be specified, but not both.

format

Specifies the data format, the same asCOPY'sFORMAT option.

header

Specifies whether the data has a header line, the same asCOPY'sHEADER option.

delimiter

Specifies the data delimiter character, the same asCOPY'sDELIMITER option.

quote

Specifies the data quote character, the same asCOPY'sQUOTE option.

escape

Specifies the data escape character, the same asCOPY'sESCAPE option.

null

Specifies the data null string, the same asCOPY'sNULL option.

encoding

Specifies the data encoding, the same asCOPY'sENCODING option.

Note that whileCOPY allows options such asHEADER to be specified without a corresponding value, the foreign table option syntax requires a value to be present in all cases. To activateCOPY options typically written without a value, you can pass the value TRUE, since all such options are Booleans.

A column of a foreign table created using this wrapper can have the following options:

force_not_null

This is a Boolean option. If true, it specifies that values of the column should not be matched against the null string (that is, the table-levelnull option). This has the same effect as listing the column inCOPY'sFORCE_NOT_NULL option.

force_null

This is a Boolean option. If true, it specifies that values of the column which match the null string are returned asNULL even if the value is quoted. Without this option, only unquoted values matching the null string are returned asNULL. This has the same effect as listing the column inCOPY'sFORCE_NULL option.

COPY'sFORCE_QUOTE option is currently not supported byfile_fdw.

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

Changing table-level options requires being a superuser or having the privileges of the rolepg_read_server_files (to use a filename) or the rolepg_execute_server_program (to use a program), 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 change the other options, but that's not supported at present.

When specifying theprogram option, keep in mind that the option 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.

For a foreign table usingfile_fdw,EXPLAIN shows the name of the file to be read or program to be run. For a file, unlessCOSTS OFF is specified, the file size (in bytes) is shown as well.

Example F.1. Create a Foreign Table for PostgreSQL CSV Logs

One of the obvious uses forfile_fdw is to make the PostgreSQL activity log available as a table for querying. To do this, first you must belogging to a CSV file, which here we will callpglog.csv. First, installfile_fdw as an extension:

CREATE EXTENSION file_fdw;

Then create a foreign server:

CREATE SERVER pglog FOREIGN DATA WRAPPER file_fdw;

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

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,  application_name text,  backend_type text,  leader_pid integer,  query_id bigint) SERVER pglogOPTIONS ( filename 'log/pglog.csv', format 'csv' );

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.


Example F.2. Create a Foreign Table with an Option on a Column

To set theforce_null option for a column, use theOPTIONS keyword.

CREATE FOREIGN TABLE films ( code char(5) NOT NULL, title text NOT NULL, rating text OPTIONS (force_null 'true')) SERVER film_serverOPTIONS ( filename 'films/db.csv', format 'csv' );


Prev Up Next
F.14. earthdistance — calculate great-circle distances Home F.16. fuzzystrmatch — determine string similarities and distance
pdfepub
Go to PostgreSQL 17
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp