gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

406 行
33KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>F.33. postgres_fdw</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="pgvisibility.html" title="F.32. pg_visibility" /><link rel="next" href="seg.html" title="F.34. seg" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">F.33. postgres_fdw</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="pgvisibility.html" title="F.32. pg_visibility">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="contrib.html" title="Appendix F. Additional Supplied Modules">Up</a></td><th width="60%" align="center">Appendix F. Additional Supplied Modules</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="seg.html" title="F.34. seg">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="POSTGRES-FDW"><div class="titlepage"><div><div><h2 class="title" style="clear: both">F.33. postgres_fdw</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="postgres-fdw.html#id-1.11.7.42.10">F.33.1. FDW Options of postgres_fdw</a></span></dt><dt><span class="sect2"><a href="postgres-fdw.html#id-1.11.7.42.11">F.33.2. Connection Management</a></span></dt><dt><span class="sect2"><a href="postgres-fdw.html#id-1.11.7.42.12">F.33.3. Transaction Management</a></span></dt><dt><span class="sect2"><a href="postgres-fdw.html#id-1.11.7.42.13">F.33.4. Remote Query Optimization</a></span></dt><dt><span class="sect2"><a href="postgres-fdw.html#id-1.11.7.42.14">F.33.5. Remote Query Execution Environment</a></span></dt><dt><span class="sect2"><a href="postgres-fdw.html#id-1.11.7.42.15">F.33.6. Cross-Version Compatibility</a></span></dt><dt><span class="sect2"><a href="postgres-fdw.html#id-1.11.7.42.16">F.33.7. Examples</a></span></dt><dt><span class="sect2"><a href="postgres-fdw.html#id-1.11.7.42.17">F.33.8. Author</a></span></dt></dl></div><a id="id-1.11.7.42.2" class="indexterm"></a><p>
  3. The <code class="filename">postgres_fdw</code> module provides the foreign-data wrapper
  4. <code class="literal">postgres_fdw</code>, which can be used to access data
  5. stored in external <span class="productname">PostgreSQL</span> servers.
  6. </p><p>
  7. The functionality provided by this module overlaps substantially
  8. with the functionality of the older <a class="xref" href="dblink.html" title="F.10. dblink">dblink</a> module.
  9. But <code class="filename">postgres_fdw</code> provides more transparent and
  10. standards-compliant syntax for accessing remote tables, and can give
  11. better performance in many cases.
  12. </p><p>
  13. To prepare for remote access using <code class="filename">postgres_fdw</code>:
  14. </p><div class="orderedlist"><ol class="orderedlist compact" type="1"><li class="listitem"><p>
  15. Install the <code class="filename">postgres_fdw</code> extension using <a class="xref" href="sql-createextension.html" title="CREATE EXTENSION"><span class="refentrytitle">CREATE EXTENSION</span></a>.
  16. </p></li><li class="listitem"><p>
  17. Create a foreign server object, using <a class="xref" href="sql-createserver.html" title="CREATE SERVER"><span class="refentrytitle">CREATE SERVER</span></a>,
  18. to represent each remote database you want to connect to.
  19. Specify connection information, except <code class="literal">user</code> and
  20. <code class="literal">password</code>, as options of the server object.
  21. </p></li><li class="listitem"><p>
  22. Create a user mapping, using <a class="xref" href="sql-createusermapping.html" title="CREATE USER MAPPING"><span class="refentrytitle">CREATE USER MAPPING</span></a>, for
  23. each database user you want to allow to access each foreign server.
  24. Specify the remote user name and password to use as
  25. <code class="literal">user</code> and <code class="literal">password</code> options of the
  26. user mapping.
  27. </p></li><li class="listitem"><p>
  28. Create a foreign table, using <a class="xref" href="sql-createforeigntable.html" title="CREATE FOREIGN TABLE"><span class="refentrytitle">CREATE FOREIGN TABLE</span></a>
  29. or <a class="xref" href="sql-importforeignschema.html" title="IMPORT FOREIGN SCHEMA"><span class="refentrytitle">IMPORT FOREIGN SCHEMA</span></a>,
  30. for each remote table you want to access. The columns of the foreign
  31. table must match the referenced remote table. You can, however, use
  32. table and/or column names different from the remote table's, if you
  33. specify the correct remote names as options of the foreign table object.
  34. </p></li></ol></div><p>
  35. </p><p>
  36. Now you need only <code class="command">SELECT</code> from a foreign table to access
  37. the data stored in its underlying remote table. You can also modify
  38. the remote table using <code class="command">INSERT</code>, <code class="command">UPDATE</code>, or
  39. <code class="command">DELETE</code>. (Of course, the remote user you have specified
  40. in your user mapping must have privileges to do these things.)
  41. </p><p>
  42. Note that <code class="filename">postgres_fdw</code> currently lacks support for
  43. <code class="command">INSERT</code> statements with an <code class="literal">ON CONFLICT DO
  44. UPDATE</code> clause. However, the <code class="literal">ON CONFLICT DO NOTHING</code>
  45. clause is supported, provided a unique index inference specification
  46. is omitted.
  47. Note also that <code class="filename">postgres_fdw</code> supports row movement
  48. invoked by <code class="command">UPDATE</code> statements executed on partitioned
  49. tables, but it currently does not handle the case where a remote partition
  50. chosen to insert a moved row into is also an <code class="command">UPDATE</code>
  51. target partition that will be updated later.
  52. </p><p>
  53. It is generally recommended that the columns of a foreign table be declared
  54. with exactly the same data types, and collations if applicable, as the
  55. referenced columns of the remote table. Although <code class="filename">postgres_fdw</code>
  56. is currently rather forgiving about performing data type conversions at
  57. need, surprising semantic anomalies may arise when types or collations do
  58. not match, due to the remote server interpreting <code class="literal">WHERE</code> clauses
  59. slightly differently from the local server.
  60. </p><p>
  61. Note that a foreign table can be declared with fewer columns, or with a
  62. different column order, than its underlying remote table has. Matching
  63. of columns to the remote table is by name, not position.
  64. </p><div class="sect2" id="id-1.11.7.42.10"><div class="titlepage"><div><div><h3 class="title">F.33.1. FDW Options of postgres_fdw</h3></div></div></div><div class="sect3" id="id-1.11.7.42.10.2"><div class="titlepage"><div><div><h4 class="title">F.33.1.1. Connection Options</h4></div></div></div><p>
  65. A foreign server using the <code class="filename">postgres_fdw</code> foreign data wrapper
  66. can have the same options that <span class="application">libpq</span> accepts in
  67. connection strings, as described in <a class="xref" href="libpq-connect.html#LIBPQ-PARAMKEYWORDS" title="33.1.2. Parameter Key Words">Section 33.1.2</a>,
  68. except that these options are not allowed:
  69. </p><div class="itemizedlist"><ul class="itemizedlist compact" style="list-style-type: disc; "><li class="listitem"><p>
  70. <code class="literal">user</code> and <code class="literal">password</code> (specify these
  71. in a user mapping, instead)
  72. </p></li><li class="listitem"><p>
  73. <code class="literal">client_encoding</code> (this is automatically set from the local
  74. server encoding)
  75. </p></li><li class="listitem"><p>
  76. <code class="literal">fallback_application_name</code> (always set to
  77. <code class="literal">postgres_fdw</code>)
  78. </p></li></ul></div><p>
  79. </p><p>
  80. Only superusers may connect to foreign servers without password
  81. authentication, so always specify the <code class="literal">password</code> option
  82. for user mappings belonging to non-superusers.
  83. </p></div><div class="sect3" id="id-1.11.7.42.10.3"><div class="titlepage"><div><div><h4 class="title">F.33.1.2. Object Name Options</h4></div></div></div><p>
  84. These options can be used to control the names used in SQL statements
  85. sent to the remote <span class="productname">PostgreSQL</span> server. These
  86. options are needed when a foreign table is created with names different
  87. from the underlying remote table's names.
  88. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">schema_name</code></span></dt><dd><p>
  89. This option, which can be specified for a foreign table, gives the
  90. schema name to use for the foreign table on the remote server. If this
  91. option is omitted, the name of the foreign table's schema is used.
  92. </p></dd><dt><span class="term"><code class="literal">table_name</code></span></dt><dd><p>
  93. This option, which can be specified for a foreign table, gives the
  94. table name to use for the foreign table on the remote server. If this
  95. option is omitted, the foreign table's name is used.
  96. </p></dd><dt><span class="term"><code class="literal">column_name</code></span></dt><dd><p>
  97. This option, which can be specified for a column of a foreign table,
  98. gives the column name to use for the column on the remote server.
  99. If this option is omitted, the column's name is used.
  100. </p></dd></dl></div></div><div class="sect3" id="id-1.11.7.42.10.4"><div class="titlepage"><div><div><h4 class="title">F.33.1.3. Cost Estimation Options</h4></div></div></div><p>
  101. <code class="filename">postgres_fdw</code> retrieves remote data by executing queries
  102. against remote servers, so ideally the estimated cost of scanning a
  103. foreign table should be whatever it costs to be done on the remote
  104. server, plus some overhead for communication. The most reliable way to
  105. get such an estimate is to ask the remote server and then add something
  106. for overhead — but for simple queries, it may not be worth the cost
  107. of an additional remote query to get a cost estimate.
  108. So <code class="filename">postgres_fdw</code> provides the following options to control
  109. how cost estimation is done:
  110. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">use_remote_estimate</code></span></dt><dd><p>
  111. This option, which can be specified for a foreign table or a foreign
  112. server, controls whether <code class="filename">postgres_fdw</code> issues remote
  113. <code class="command">EXPLAIN</code> commands to obtain cost estimates.
  114. A setting for a foreign table overrides any setting for its server,
  115. but only for that table.
  116. The default is <code class="literal">false</code>.
  117. </p></dd><dt><span class="term"><code class="literal">fdw_startup_cost</code></span></dt><dd><p>
  118. This option, which can be specified for a foreign server, is a numeric
  119. value that is added to the estimated startup cost of any foreign-table
  120. scan on that server. This represents the additional overhead of
  121. establishing a connection, parsing and planning the query on the
  122. remote side, etc.
  123. The default value is <code class="literal">100</code>.
  124. </p></dd><dt><span class="term"><code class="literal">fdw_tuple_cost</code></span></dt><dd><p>
  125. This option, which can be specified for a foreign server, is a numeric
  126. value that is used as extra cost per-tuple for foreign-table
  127. scans on that server. This represents the additional overhead of
  128. data transfer between servers. You might increase or decrease this
  129. number to reflect higher or lower network delay to the remote server.
  130. The default value is <code class="literal">0.01</code>.
  131. </p></dd></dl></div><p>
  132. When <code class="literal">use_remote_estimate</code> is true,
  133. <code class="filename">postgres_fdw</code> obtains row count and cost estimates from the
  134. remote server and then adds <code class="literal">fdw_startup_cost</code> and
  135. <code class="literal">fdw_tuple_cost</code> to the cost estimates. When
  136. <code class="literal">use_remote_estimate</code> is false,
  137. <code class="filename">postgres_fdw</code> performs local row count and cost estimation
  138. and then adds <code class="literal">fdw_startup_cost</code> and
  139. <code class="literal">fdw_tuple_cost</code> to the cost estimates. This local
  140. estimation is unlikely to be very accurate unless local copies of the
  141. remote table's statistics are available. Running
  142. <a class="xref" href="sql-analyze.html" title="ANALYZE"><span class="refentrytitle">ANALYZE</span></a> on the foreign table is the way to update
  143. the local statistics; this will perform a scan of the remote table and
  144. then calculate and store statistics just as though the table were local.
  145. Keeping local statistics can be a useful way to reduce per-query planning
  146. overhead for a remote table — but if the remote table is
  147. frequently updated, the local statistics will soon be obsolete.
  148. </p></div><div class="sect3" id="id-1.11.7.42.10.5"><div class="titlepage"><div><div><h4 class="title">F.33.1.4. Remote Execution Options</h4></div></div></div><p>
  149. By default, only <code class="literal">WHERE</code> clauses using built-in operators and
  150. functions will be considered for execution on the remote server. Clauses
  151. involving non-built-in functions are checked locally after rows are
  152. fetched. If such functions are available on the remote server and can be
  153. relied on to produce the same results as they do locally, performance can
  154. be improved by sending such <code class="literal">WHERE</code> clauses for remote
  155. execution. This behavior can be controlled using the following option:
  156. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">extensions</code></span></dt><dd><p>
  157. This option is a comma-separated list of names
  158. of <span class="productname">PostgreSQL</span> extensions that are installed, in
  159. compatible versions, on both the local and remote servers. Functions
  160. and operators that are immutable and belong to a listed extension will
  161. be considered shippable to the remote server.
  162. This option can only be specified for foreign servers, not per-table.
  163. </p><p>
  164. When using the <code class="literal">extensions</code> option, <span class="emphasis"><em>it is the
  165. user's responsibility</em></span> that the listed extensions exist and behave
  166. identically on both the local and remote servers. Otherwise, remote
  167. queries may fail or behave unexpectedly.
  168. </p></dd><dt><span class="term"><code class="literal">fetch_size</code></span></dt><dd><p>
  169. This option specifies the number of rows <code class="filename">postgres_fdw</code>
  170. should get in each fetch operation. It can be specified for a foreign
  171. table or a foreign server. The option specified on a table overrides
  172. an option specified for the server.
  173. The default is <code class="literal">100</code>.
  174. </p></dd></dl></div></div><div class="sect3" id="id-1.11.7.42.10.6"><div class="titlepage"><div><div><h4 class="title">F.33.1.5. Updatability Options</h4></div></div></div><p>
  175. By default all foreign tables using <code class="filename">postgres_fdw</code> are assumed
  176. to be updatable. This may be overridden using the following option:
  177. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">updatable</code></span></dt><dd><p>
  178. This option controls whether <code class="filename">postgres_fdw</code> allows foreign
  179. tables to be modified using <code class="command">INSERT</code>, <code class="command">UPDATE</code> and
  180. <code class="command">DELETE</code> commands. It can be specified for a foreign table
  181. or a foreign server. A table-level option overrides a server-level
  182. option.
  183. The default is <code class="literal">true</code>.
  184. </p><p>
  185. Of course, if the remote table is not in fact updatable, an error
  186. would occur anyway. Use of this option primarily allows the error to
  187. be thrown locally without querying the remote server. Note however
  188. that the <code class="literal">information_schema</code> views will report a
  189. <code class="filename">postgres_fdw</code> foreign table to be updatable (or not)
  190. according to the setting of this option, without any check of the
  191. remote server.
  192. </p></dd></dl></div></div><div class="sect3" id="id-1.11.7.42.10.7"><div class="titlepage"><div><div><h4 class="title">F.33.1.6. Importing Options</h4></div></div></div><p>
  193. <code class="filename">postgres_fdw</code> is able to import foreign table definitions
  194. using <a class="xref" href="sql-importforeignschema.html" title="IMPORT FOREIGN SCHEMA"><span class="refentrytitle">IMPORT FOREIGN SCHEMA</span></a>. This command creates
  195. foreign table definitions on the local server that match tables or
  196. views present on the remote server. If the remote tables to be imported
  197. have columns of user-defined data types, the local server must have
  198. compatible types of the same names.
  199. </p><p>
  200. Importing behavior can be customized with the following options
  201. (given in the <code class="command">IMPORT FOREIGN SCHEMA</code> command):
  202. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">import_collate</code></span></dt><dd><p>
  203. This option controls whether column <code class="literal">COLLATE</code> options
  204. are included in the definitions of foreign tables imported
  205. from a foreign server. The default is <code class="literal">true</code>. You might
  206. need to turn this off if the remote server has a different set of
  207. collation names than the local server does, which is likely to be the
  208. case if it's running on a different operating system.
  209. </p></dd><dt><span class="term"><code class="literal">import_default</code></span></dt><dd><p>
  210. This option controls whether column <code class="literal">DEFAULT</code> expressions
  211. are included in the definitions of foreign tables imported
  212. from a foreign server. The default is <code class="literal">false</code>. If you
  213. enable this option, be wary of defaults that might get computed
  214. differently on the local server than they would be on the remote
  215. server; <code class="function">nextval()</code> is a common source of problems.
  216. The <code class="command">IMPORT</code> will fail altogether if an imported default
  217. expression uses a function or operator that does not exist locally.
  218. </p></dd><dt><span class="term"><code class="literal">import_not_null</code></span></dt><dd><p>
  219. This option controls whether column <code class="literal">NOT NULL</code>
  220. constraints are included in the definitions of foreign tables imported
  221. from a foreign server. The default is <code class="literal">true</code>.
  222. </p></dd></dl></div><p>
  223. Note that constraints other than <code class="literal">NOT NULL</code> will never be
  224. imported from the remote tables. Although <span class="productname">PostgreSQL</span>
  225. does support <code class="literal">CHECK</code> constraints on foreign tables, there is no
  226. provision for importing them automatically, because of the risk that a
  227. constraint expression could evaluate differently on the local and remote
  228. servers. Any such inconsistency in the behavior of a <code class="literal">CHECK</code>
  229. constraint could lead to hard-to-detect errors in query optimization.
  230. So if you wish to import <code class="literal">CHECK</code> constraints, you must do so
  231. manually, and you should verify the semantics of each one carefully.
  232. For more detail about the treatment of <code class="literal">CHECK</code> constraints on
  233. foreign tables, see <a class="xref" href="sql-createforeigntable.html" title="CREATE FOREIGN TABLE"><span class="refentrytitle">CREATE FOREIGN TABLE</span></a>.
  234. </p><p>
  235. Tables or foreign tables which are partitions of some other table are
  236. automatically excluded. Partitioned tables are imported, unless they
  237. are a partition of some other table. Since all data can be accessed
  238. through the partitioned table which is the root of the partitioning
  239. hierarchy, this approach should allow access to all the data without
  240. creating extra objects.
  241. </p></div></div><div class="sect2" id="id-1.11.7.42.11"><div class="titlepage"><div><div><h3 class="title">F.33.2. Connection Management</h3></div></div></div><p>
  242. <code class="filename">postgres_fdw</code> establishes a connection to a
  243. foreign server during the first query that uses a foreign table
  244. associated with the foreign server. This connection is kept and
  245. re-used for subsequent queries in the same session. However, if
  246. multiple user identities (user mappings) are used to access the foreign
  247. server, a connection is established for each user mapping.
  248. </p></div><div class="sect2" id="id-1.11.7.42.12"><div class="titlepage"><div><div><h3 class="title">F.33.3. Transaction Management</h3></div></div></div><p>
  249. During a query that references any remote tables on a foreign server,
  250. <code class="filename">postgres_fdw</code> opens a transaction on the
  251. remote server if one is not already open corresponding to the current
  252. local transaction. The remote transaction is committed or aborted when
  253. the local transaction commits or aborts. Savepoints are similarly
  254. managed by creating corresponding remote savepoints.
  255. </p><p>
  256. The remote transaction uses <code class="literal">SERIALIZABLE</code>
  257. isolation level when the local transaction has <code class="literal">SERIALIZABLE</code>
  258. isolation level; otherwise it uses <code class="literal">REPEATABLE READ</code>
  259. isolation level. This choice ensures that if a query performs multiple
  260. table scans on the remote server, it will get snapshot-consistent results
  261. for all the scans. A consequence is that successive queries within a
  262. single transaction will see the same data from the remote server, even if
  263. concurrent updates are occurring on the remote server due to other
  264. activities. That behavior would be expected anyway if the local
  265. transaction uses <code class="literal">SERIALIZABLE</code> or <code class="literal">REPEATABLE READ</code>
  266. isolation level, but it might be surprising for a <code class="literal">READ
  267. COMMITTED</code> local transaction. A future
  268. <span class="productname">PostgreSQL</span> release might modify these rules.
  269. </p><p>
  270. Note that it is currently not supported by
  271. <code class="filename">postgres_fdw</code> to prepare the remote transaction for
  272. two-phase commit.
  273. </p></div><div class="sect2" id="id-1.11.7.42.13"><div class="titlepage"><div><div><h3 class="title">F.33.4. Remote Query Optimization</h3></div></div></div><p>
  274. <code class="filename">postgres_fdw</code> attempts to optimize remote queries to reduce
  275. the amount of data transferred from foreign servers. This is done by
  276. sending query <code class="literal">WHERE</code> clauses to the remote server for
  277. execution, and by not retrieving table columns that are not needed for
  278. the current query. To reduce the risk of misexecution of queries,
  279. <code class="literal">WHERE</code> clauses are not sent to the remote server unless they use
  280. only data types, operators, and functions that are built-in or belong to an
  281. extension that's listed in the foreign server's <code class="literal">extensions</code>
  282. option. Operators and functions in such clauses must
  283. be <code class="literal">IMMUTABLE</code> as well.
  284. For an <code class="command">UPDATE</code> or <code class="command">DELETE</code> query,
  285. <code class="filename">postgres_fdw</code> attempts to optimize the query execution by
  286. sending the whole query to the remote server if there are no query
  287. <code class="literal">WHERE</code> clauses that cannot be sent to the remote server,
  288. no local joins for the query, no row-level local <code class="literal">BEFORE</code> or
  289. <code class="literal">AFTER</code> triggers or stored generated columns on the target
  290. table, and no <code class="literal">CHECK OPTION</code> constraints from parent
  291. views. In <code class="command">UPDATE</code>,
  292. expressions to assign to target columns must use only built-in data types,
  293. <code class="literal">IMMUTABLE</code> operators, or <code class="literal">IMMUTABLE</code> functions,
  294. to reduce the risk of misexecution of the query.
  295. </p><p>
  296. When <code class="filename">postgres_fdw</code> encounters a join between foreign tables on
  297. the same foreign server, it sends the entire join to the foreign server,
  298. unless for some reason it believes that it will be more efficient to fetch
  299. rows from each table individually, or unless the table references involved
  300. are subject to different user mappings. While sending the <code class="literal">JOIN</code>
  301. clauses, it takes the same precautions as mentioned above for the
  302. <code class="literal">WHERE</code> clauses.
  303. </p><p>
  304. The query that is actually sent to the remote server for execution can
  305. be examined using <code class="command">EXPLAIN VERBOSE</code>.
  306. </p></div><div class="sect2" id="id-1.11.7.42.14"><div class="titlepage"><div><div><h3 class="title">F.33.5. Remote Query Execution Environment</h3></div></div></div><p>
  307. In the remote sessions opened by <code class="filename">postgres_fdw</code>,
  308. the <a class="xref" href="runtime-config-client.html#GUC-SEARCH-PATH">search_path</a> parameter is set to
  309. just <code class="literal">pg_catalog</code>, so that only built-in objects are visible
  310. without schema qualification. This is not an issue for queries
  311. generated by <code class="filename">postgres_fdw</code> itself, because it always
  312. supplies such qualification. However, this can pose a hazard for
  313. functions that are executed on the remote server via triggers or rules
  314. on remote tables. For example, if a remote table is actually a view,
  315. any functions used in that view will be executed with the restricted
  316. search path. It is recommended to schema-qualify all names in such
  317. functions, or else attach <code class="literal">SET search_path</code> options
  318. (see <a class="xref" href="sql-createfunction.html" title="CREATE FUNCTION"><span class="refentrytitle">CREATE FUNCTION</span></a>) to such functions
  319. to establish their expected search path environment.
  320. </p><p>
  321. <code class="filename">postgres_fdw</code> likewise establishes remote session settings
  322. for various parameters:
  323. </p><div class="itemizedlist"><ul class="itemizedlist compact" style="list-style-type: disc; "><li class="listitem"><p>
  324. <a class="xref" href="runtime-config-client.html#GUC-TIMEZONE">TimeZone</a> is set to <code class="literal">UTC</code>
  325. </p></li><li class="listitem"><p>
  326. <a class="xref" href="runtime-config-client.html#GUC-DATESTYLE">DateStyle</a> is set to <code class="literal">ISO</code>
  327. </p></li><li class="listitem"><p>
  328. <a class="xref" href="runtime-config-client.html#GUC-INTERVALSTYLE">IntervalStyle</a> is set to <code class="literal">postgres</code>
  329. </p></li><li class="listitem"><p>
  330. <a class="xref" href="runtime-config-client.html#GUC-EXTRA-FLOAT-DIGITS">extra_float_digits</a> is set to <code class="literal">3</code> for remote
  331. servers 9.0 and newer and is set to <code class="literal">2</code> for older versions
  332. </p></li></ul></div><p>
  333. These are less likely to be problematic than <code class="varname">search_path</code>, but
  334. can be handled with function <code class="literal">SET</code> options if the need arises.
  335. </p><p>
  336. It is <span class="emphasis"><em>not</em></span> recommended that you override this behavior by
  337. changing the session-level settings of these parameters; that is likely
  338. to cause <code class="filename">postgres_fdw</code> to malfunction.
  339. </p></div><div class="sect2" id="id-1.11.7.42.15"><div class="titlepage"><div><div><h3 class="title">F.33.6. Cross-Version Compatibility</h3></div></div></div><p>
  340. <code class="filename">postgres_fdw</code> can be used with remote servers dating back
  341. to <span class="productname">PostgreSQL</span> 8.3. Read-only capability is available
  342. back to 8.1. A limitation however is that <code class="filename">postgres_fdw</code>
  343. generally assumes that immutable built-in functions and operators are
  344. safe to send to the remote server for execution, if they appear in a
  345. <code class="literal">WHERE</code> clause for a foreign table. Thus, a built-in
  346. function that was added since the remote server's release might be sent
  347. to it for execution, resulting in <span class="quote">“<span class="quote">function does not exist</span>”</span> or
  348. a similar error. This type of failure can be worked around by
  349. rewriting the query, for example by embedding the foreign table
  350. reference in a sub-<code class="literal">SELECT</code> with <code class="literal">OFFSET 0</code> as an
  351. optimization fence, and placing the problematic function or operator
  352. outside the sub-<code class="literal">SELECT</code>.
  353. </p></div><div class="sect2" id="id-1.11.7.42.16"><div class="titlepage"><div><div><h3 class="title">F.33.7. Examples</h3></div></div></div><p>
  354. Here is an example of creating a foreign table with
  355. <code class="literal">postgres_fdw</code>. First install the extension:
  356. </p><pre class="programlisting">
  357. CREATE EXTENSION postgres_fdw;
  358. </pre><p>
  359. Then create a foreign server using <a class="xref" href="sql-createserver.html" title="CREATE SERVER"><span class="refentrytitle">CREATE SERVER</span></a>.
  360. In this example we wish to connect to a <span class="productname">PostgreSQL</span> server
  361. on host <code class="literal">192.83.123.89</code> listening on
  362. port <code class="literal">5432</code>. The database to which the connection is made
  363. is named <code class="literal">foreign_db</code> on the remote server:
  364. </p><pre class="programlisting">
  365. CREATE SERVER foreign_server
  366. FOREIGN DATA WRAPPER postgres_fdw
  367. OPTIONS (host '192.83.123.89', port '5432', dbname 'foreign_db');
  368. </pre><p>
  369. </p><p>
  370. A user mapping, defined with <a class="xref" href="sql-createusermapping.html" title="CREATE USER MAPPING"><span class="refentrytitle">CREATE USER MAPPING</span></a>, is
  371. needed as well to identify the role that will be used on the remote
  372. server:
  373. </p><pre class="programlisting">
  374. CREATE USER MAPPING FOR local_user
  375. SERVER foreign_server
  376. OPTIONS (user 'foreign_user', password 'password');
  377. </pre><p>
  378. </p><p>
  379. Now it is possible to create a foreign table with
  380. <a class="xref" href="sql-createforeigntable.html" title="CREATE FOREIGN TABLE"><span class="refentrytitle">CREATE FOREIGN TABLE</span></a>. In this example we
  381. wish to access the table named <code class="structname">some_schema.some_table</code>
  382. on the remote server. The local name for it will
  383. be <code class="structname">foreign_table</code>:
  384. </p><pre class="programlisting">
  385. CREATE FOREIGN TABLE foreign_table (
  386. id integer NOT NULL,
  387. data text
  388. )
  389. SERVER foreign_server
  390. OPTIONS (schema_name 'some_schema', table_name 'some_table');
  391. </pre><p>
  392. It's essential that the data types and other properties of the columns
  393. declared in <code class="command">CREATE FOREIGN TABLE</code> match the actual remote table.
  394. Column names must match as well, unless you attach <code class="literal">column_name</code>
  395. options to the individual columns to show how they are named in the remote
  396. table.
  397. In many cases, use of <a class="xref" href="sql-importforeignschema.html" title="IMPORT FOREIGN SCHEMA"><span class="refentrytitle">IMPORT FOREIGN SCHEMA</span></a> is
  398. preferable to constructing foreign table definitions manually.
  399. </p></div><div class="sect2" id="id-1.11.7.42.17"><div class="titlepage"><div><div><h3 class="title">F.33.8. Author</h3></div></div></div><p>
  400. Shigeru Hanada <code class="email">&lt;<a class="email" href="mailto:shigeru.hanada@gmail.com">shigeru.hanada@gmail.com</a>&gt;</code>
  401. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="pgvisibility.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="contrib.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="seg.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">F.32. pg_visibility </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> F.34. seg</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1