<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >postgres_fdw</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 9.3.9 Documentation" HREF="index.html"><LINK REL="UP" TITLE="Additional Supplied Modules" HREF="contrib.html"><LINK REL="PREVIOUS" TITLE="pg_trgm" HREF="pgtrgm.html"><LINK REL="NEXT" TITLE="seg" HREF="seg.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2015-06-13T20:07:22"></HEAD ><BODY CLASS="SECT1" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" ><A HREF="index.html" >PostgreSQL 9.3.9 Documentation</A ></TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A TITLE="pg_trgm" HREF="pgtrgm.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="contrib.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" >Appendix F. Additional Supplied Modules</TD ><TD WIDTH="20%" ALIGN="right" VALIGN="top" ><A TITLE="seg" HREF="seg.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="POSTGRES-FDW" >F.31. postgres_fdw</A ></H1 ><P > The <TT CLASS="FILENAME" >postgres_fdw</TT > module provides the foreign-data wrapper <TT CLASS="LITERAL" >postgres_fdw</TT >, which can be used to access data stored in external <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > servers. </P ><P > The functionality provided by this module overlaps substantially with the functionality of the older <A HREF="dblink.html" >dblink</A > module. But <TT CLASS="FILENAME" >postgres_fdw</TT > provides more transparent and standards-compliant syntax for accessing remote tables, and can give better performance in many cases. </P ><P > To prepare for remote access using <TT CLASS="FILENAME" >postgres_fdw</TT >: <P ></P ></P><OL COMPACT="COMPACT" TYPE="1" ><LI ><P > Install the <TT CLASS="FILENAME" >postgres_fdw</TT > extension using <A HREF="sql-createextension.html" >CREATE EXTENSION</A >. </P ></LI ><LI ><P > Create a foreign server object, using <A HREF="sql-createserver.html" >CREATE SERVER</A >, to represent each remote database you want to connect to. Specify connection information, except <TT CLASS="LITERAL" >user</TT > and <TT CLASS="LITERAL" >password</TT >, as options of the server object. </P ></LI ><LI ><P > Create a user mapping, using <A HREF="sql-createusermapping.html" >CREATE USER MAPPING</A >, for each database user you want to allow to access each foreign server. Specify the remote user name and password to use as <TT CLASS="LITERAL" >user</TT > and <TT CLASS="LITERAL" >password</TT > options of the user mapping. </P ></LI ><LI ><P > Create a foreign table, using <A HREF="sql-createforeigntable.html" >CREATE FOREIGN TABLE</A >, for each remote table you want to access. The columns of the foreign table must match the referenced remote table. You can, however, use table and/or column names different from the remote table's, if you specify the correct remote names as options of the foreign table object. </P ></LI ></OL ><P> </P ><P > Now you need only <TT CLASS="COMMAND" >SELECT</TT > from a foreign table to access the data stored in its underlying remote table. You can also modify the remote table using <TT CLASS="COMMAND" >INSERT</TT >, <TT CLASS="COMMAND" >UPDATE</TT >, or <TT CLASS="COMMAND" >DELETE</TT >. (Of course, the remote user you have specified in your user mapping must have privileges to do these things.) </P ><P > It is generally recommended that the columns of a foreign table be declared with exactly the same data types, and collations if applicable, as the referenced columns of the remote table. Although <TT CLASS="FILENAME" >postgres_fdw</TT > is currently rather forgiving about performing data type conversions at need, surprising semantic anomalies may arise when types or collations do not match, due to the remote server interpreting <TT CLASS="LITERAL" >WHERE</TT > clauses slightly differently from the local server. </P ><P > Note that a foreign table can be declared with fewer columns, or with a different column order, than its underlying remote table has. Matching of columns to the remote table is by name, not position. </P ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN155658" >F.31.1. FDW Options of postgres_fdw</A ></H2 ><DIV CLASS="SECT3" ><H3 CLASS="SECT3" ><A NAME="AEN155660" >F.31.1.1. Connection Options</A ></H3 ><P > A foreign server using the <TT CLASS="FILENAME" >postgres_fdw</TT > foreign data wrapper can have the same options that <SPAN CLASS="APPLICATION" >libpq</SPAN > accepts in connection strings, as described in <A HREF="libpq-connect.html#LIBPQ-PARAMKEYWORDS" >Section 31.1.2</A >, except that these options are not allowed: <P ></P ></P><UL COMPACT="COMPACT" ><LI ><P > <TT CLASS="LITERAL" >user</TT > and <TT CLASS="LITERAL" >password</TT > (specify these for a user mapping, instead) </P ></LI ><LI ><P > <TT CLASS="LITERAL" >client_encoding</TT > (this is automatically set from the local server encoding) </P ></LI ><LI ><P > <TT CLASS="LITERAL" >fallback_application_name</TT > (always set to <TT CLASS="LITERAL" >postgres_fdw</TT >) </P ></LI ></UL ><P> </P ><P > Only superusers may connect to foreign servers without password authentication, so always specify the <TT CLASS="LITERAL" >password</TT > option for user mappings belonging to non-superusers. </P ></DIV ><DIV CLASS="SECT3" ><H3 CLASS="SECT3" ><A NAME="AEN155680" >F.31.1.2. Object Name Options</A ></H3 ><P > These options can be used to control the names used in SQL statements sent to the remote <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > server. These options are needed when a foreign table is created with names different from the underlying remote table's names. </P ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="LITERAL" >schema_name</TT ></DT ><DD ><P > This option, which can be specified for a foreign table, gives the schema name to use for the foreign table on the remote server. If this option is omitted, the name of the foreign table's schema is used. </P ></DD ><DT ><TT CLASS="LITERAL" >table_name</TT ></DT ><DD ><P > This option, which can be specified for a foreign table, gives the table name to use for the foreign table on the remote server. If this option is omitted, the foreign table's name is used. </P ></DD ><DT ><TT CLASS="LITERAL" >column_name</TT ></DT ><DD ><P > This option, which can be specified for a column of a foreign table, gives the column name to use for the column on the remote server. If this option is omitted, the column's name is used. </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="SECT3" ><H3 CLASS="SECT3" ><A NAME="AEN155700" >F.31.1.3. Cost Estimation Options</A ></H3 ><P > <TT CLASS="FILENAME" >postgres_fdw</TT > retrieves remote data by executing queries against remote servers, so ideally the estimated cost of scanning a foreign table should be whatever it costs to be done on the remote server, plus some overhead for communication. The most reliable way to get such an estimate is to ask the remote server and then add something for overhead — but for simple queries, it may not be worth the cost of an additional remote query to get a cost estimate. So <TT CLASS="FILENAME" >postgres_fdw</TT > provides the following options to control how cost estimation is done: </P ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="LITERAL" >use_remote_estimate</TT ></DT ><DD ><P > This option, which can be specified for a foreign table or a foreign server, controls whether <TT CLASS="FILENAME" >postgres_fdw</TT > issues remote <TT CLASS="COMMAND" >EXPLAIN</TT > commands to obtain cost estimates. A setting for a foreign table overrides any setting for its server, but only for that table. The default is <TT CLASS="LITERAL" >false</TT >. </P ></DD ><DT ><TT CLASS="LITERAL" >fdw_startup_cost</TT ></DT ><DD ><P > This option, which can be specified for a foreign server, is a numeric value that is added to the estimated startup cost of any foreign-table scan on that server. This represents the additional overhead of establishing a connection, parsing and planning the query on the remote side, etc. The default value is <TT CLASS="LITERAL" >100</TT >. </P ></DD ><DT ><TT CLASS="LITERAL" >fdw_tuple_cost</TT ></DT ><DD ><P > This option, which can be specified for a foreign server, is a numeric value that is used as extra cost per-tuple for foreign-table scans on that server. This represents the additional overhead of data transfer between servers. You might increase or decrease this number to reflect higher or lower network delay to the remote server. The default value is <TT CLASS="LITERAL" >0.01</TT >. </P ></DD ></DL ></DIV ><P > When <TT CLASS="LITERAL" >use_remote_estimate</TT > is true, <TT CLASS="FILENAME" >postgres_fdw</TT > obtains row count and cost estimates from the remote server and then adds <TT CLASS="LITERAL" >fdw_startup_cost</TT > and <TT CLASS="LITERAL" >fdw_tuple_cost</TT > to the cost estimates. When <TT CLASS="LITERAL" >use_remote_estimate</TT > is false, <TT CLASS="FILENAME" >postgres_fdw</TT > performs local row count and cost estimation and then adds <TT CLASS="LITERAL" >fdw_startup_cost</TT > and <TT CLASS="LITERAL" >fdw_tuple_cost</TT > to the cost estimates. This local estimation is unlikely to be very accurate unless local copies of the remote table's statistics are available. Running <A HREF="sql-analyze.html" >ANALYZE</A > on the foreign table is the way to update the local statistics; this will perform a scan of the remote table and then calculate and store statistics just as though the table were local. Keeping local statistics can be a useful way to reduce per-query planning overhead for a remote table — but if the remote table is frequently updated, the local statistics will soon be obsolete. </P ></DIV ><DIV CLASS="SECT3" ><H3 CLASS="SECT3" ><A NAME="AEN155736" >F.31.1.4. Updatability Options</A ></H3 ><P > By default all foreign tables using <TT CLASS="FILENAME" >postgres_fdw</TT > are assumed to be updatable. This may be overridden using the following option: </P ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="LITERAL" >updatable</TT ></DT ><DD ><P > This option controls whether <TT CLASS="FILENAME" >postgres_fdw</TT > allows foreign tables to be modified using <TT CLASS="COMMAND" >INSERT</TT >, <TT CLASS="COMMAND" >UPDATE</TT > and <TT CLASS="COMMAND" >DELETE</TT > commands. It can be specified for a foreign table or a foreign server. A table-level option overrides a server-level option. The default is <TT CLASS="LITERAL" >true</TT >. </P ><P > Of course, if the remote table is not in fact updatable, an error would occur anyway. Use of this option primarily allows the error to be thrown locally without querying the remote server. Note however that the <TT CLASS="LITERAL" >information_schema</TT > views will report a <TT CLASS="FILENAME" >postgres_fdw</TT > foreign table to be updatable (or not) according to the setting of this option, without any check of the remote server. </P ></DD ></DL ></DIV ></DIV ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN155754" >F.31.2. Connection Management</A ></H2 ><P > <TT CLASS="FILENAME" >postgres_fdw</TT > establishes a connection to a foreign server during the first query that uses a foreign table associated with the foreign server. This connection is kept and re-used for subsequent queries in the same session. However, if multiple user identities (user mappings) are used to access the foreign server, a connection is established for each user mapping. </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN155758" >F.31.3. Transaction Management</A ></H2 ><P > During a query that references any remote tables on a foreign server, <TT CLASS="FILENAME" >postgres_fdw</TT > opens a transaction on the remote server if one is not already open corresponding to the current local transaction. The remote transaction is committed or aborted when the local transaction commits or aborts. Savepoints are similarly managed by creating corresponding remote savepoints. </P ><P > The remote transaction uses <TT CLASS="LITERAL" >SERIALIZABLE</TT > isolation level when the local transaction has <TT CLASS="LITERAL" >SERIALIZABLE</TT > isolation level; otherwise it uses <TT CLASS="LITERAL" >REPEATABLE READ</TT > isolation level. This choice ensures that if a query performs multiple table scans on the remote server, it will get snapshot-consistent results for all the scans. A consequence is that successive queries within a single transaction will see the same data from the remote server, even if concurrent updates are occurring on the remote server due to other activities. That behavior would be expected anyway if the local transaction uses <TT CLASS="LITERAL" >SERIALIZABLE</TT > or <TT CLASS="LITERAL" >REPEATABLE READ</TT > isolation level, but it might be surprising for a <TT CLASS="LITERAL" >READ COMMITTED</TT > local transaction. A future <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > release might modify these rules. </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN155770" >F.31.4. Remote Query Optimization</A ></H2 ><P > <TT CLASS="FILENAME" >postgres_fdw</TT > attempts to optimize remote queries to reduce the amount of data transferred from foreign servers. This is done by sending query <TT CLASS="LITERAL" >WHERE</TT > clauses to the remote server for execution, and by not retrieving table columns that are not needed for the current query. To reduce the risk of misexecution of queries, <TT CLASS="LITERAL" >WHERE</TT > clauses are not sent to the remote server unless they use only built-in data types, operators, and functions. Operators and functions in the clauses must be <TT CLASS="LITERAL" >IMMUTABLE</TT > as well. </P ><P > The query that is actually sent to the remote server for execution can be examined using <TT CLASS="COMMAND" >EXPLAIN VERBOSE</TT >. </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN155779" >F.31.5. Cross-Version Compatibility</A ></H2 ><P > <TT CLASS="FILENAME" >postgres_fdw</TT > can be used with remote servers dating back to <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > 8.3. Read-only capability is available back to 8.1. A limitation however is that <TT CLASS="FILENAME" >postgres_fdw</TT > generally assumes that immutable built-in functions and operators are safe to send to the remote server for execution, if they appear in a <TT CLASS="LITERAL" >WHERE</TT > clause for a foreign table. Thus, a built-in function that was added since the remote server's release might be sent to it for execution, resulting in <SPAN CLASS="QUOTE" >"function does not exist"</SPAN > or a similar error. This type of failure can be worked around by rewriting the query, for example by embedding the foreign table reference in a sub-<TT CLASS="LITERAL" >SELECT</TT > with <TT CLASS="LITERAL" >OFFSET 0</TT > as an optimization fence, and placing the problematic function or operator outside the sub-<TT CLASS="LITERAL" >SELECT</TT >. </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN155790" >F.31.6. Author</A ></H2 ><P > Shigeru Hanada <CODE CLASS="EMAIL" ><<A HREF="mailto:shigeru.hanada@gmail.com" >shigeru.hanada@gmail.com</A >></CODE > </P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="pgtrgm.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="seg.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >pg_trgm</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="contrib.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >seg</TD ></TR ></TABLE ></DIV ></BODY ></HTML >