官术网_书友最值得收藏!

  • Learning PostgreSQL 11
  • Salahaldin Juba Andrey Volkov
  • 609字
  • 2021-07-02 13:11:44

Foreign data wrappers

In 2011, PostgreSQL 9.1 was released with a read-only support for SQL/Management of External Data (MED) ISO/IEC 9075-9:2003 standard. SQL/MED defines foreign data wrappers (FDWs) to allow the relational database to manage external data. FDW can be used to achieve data integration in a federated database-system environment. PostgreSQL supports RDBMS, NoSQL, and foreign data wrapper files, including Oracle, Redis, MongoDB, and delimited files.

A simple use case for FDWs is to have one database server for analytical purposes, and then ship the result of this server to another server that works as a caching layer.

Also, FDW can be used to test data changes. Imagine you have two databases, one with different data due to applying a certain development patch. One could use FDW to assess the effect of this patch by comparing the data from the two databases.

PostgreSQL supports postgres_fdw starting from release 9.3. postgres_fdw is used to enable data sharing and access between different PostgreSQL databases. It supports the SELECT, INSERT, UPDATE, and DELETE operations on foreign tables.

The following example shows you how to read comma-separated value (CSV) files using FDW; you can read CSV files to parse logs. Let's assume that we want to read the database logs generated by PostgreSQL. This is quite useful in a production environment as you can have statistics about executed queries; the table structure can be found in the documentation at https://www.postgresql.org/docs/current/static/runtime-config-logging.html. To enable CSV logging, you need to change the following values in postgresql.conf. For simplicity, all statements will be logged, but this isn't recommended in a production environment since it'll consume a lot of server resources:

log_destination = 'csvlog'
logging_collector = on
log_filename = 'postgresql.log'
log_statement = 'all'

For the changes to take effect, you need to restart PostgreSQL from the Terminal as follows:

sudo service postgresql restart 

To install the FDW file, we need to run the following command:

postgres=# CREATE EXTENSION file_fdw ;
CREATE EXTENSION

To access the file, we need to create the FDW server, as follows:

postgres=# CREATE SERVER fileserver FOREIGN DATA WRAPPER file_fdw;
CREATE SERVER

Also, we need to create an FDW table and link it to the log file; in our case, it's located in the log folder in the PostgreSQL cluster directory:

postgres=# CREATE FOREIGN TABLE postgres_log
( 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
) SERVER fileserver OPTIONS ( filename '/var/lib/postgresql/11/main/log/postgresql.csv', header 'true', format 'csv' );
CREATE FOREIGN TABLE

To test our example, let's get one log line in JSON format, as follows:

postgres=# SELECT row_to_json(postgres_log, true) FROM postgres_log limit 1;
row_to_json
------------------------------------------------------------------------
{"log_time":"2018-12-10T00:35:19.768+01:00", +
"user_name":null, +
"database_name":null, +
"process_id":25847, +
"connection_from":null, +
"session_id":"5c0da6b7.64f7", +
"session_line_num":1, +
"command_tag":null, +
"session_start_time":"2018-12-10T00:35:19+01:00", +
"virtual_transaction_id":null, +
"transaction_id":0, +
"error_severity":"LOG", +
"sql_state_code":"00000", +
"message":"database system was shut down at 2018-12-10 00:35:19 CET",+
"detail":null, +
"hint":null, +
"internal_query":null, +
"internal_query_pos":null, +
"context":null, +
"query":null, +
"query_pos":null, +
"location":null, +
"application_name":""}
(1 row)

As we've seen, you can store the PostgreSQL logs in the PostgreSQL cluster. This allows the developer to search for certain user actions. Also, it allows the administrators to conduct statistical analysis on performance, such as finding the slowest queries to tune the PostgreSQL server configuration or rewriting slow queries. 

The creation of the foreign data wrapper table depends on the log format. This format changes from one version to another. This information can be found in the documentation at  https://www.postgresql.org/docs/current/runtime-config-logging.html#RUNTIME-CONFIG-LOGGING-CSVLOG.
主站蜘蛛池模板: 定西市| 商南县| 聂荣县| 伽师县| 蓬溪县| 鹰潭市| 延长县| 万宁市| 工布江达县| 电白县| 积石山| 潢川县| 南岸区| 寿光市| 绥滨县| 遂溪县| 龙岩市| 深州市| 富阳市| 勐海县| 合山市| 通榆县| 铜陵市| 略阳县| 马鞍山市| 台东县| 福州市| 广安市| 娄烦县| 枝江市| 安康市| 罗山县| 都兰县| 郴州市| 石门县| 湛江市| 沁阳市| 大田县| 南阳市| 南充市| 兰考县|