dblink

dblink — executes a query in a remote database

Synopsis

  1. dblink(text connname, text sql [, bool fail_on_error]) returns setof record
  2. dblink(text connstr, text sql [, bool fail_on_error]) returns setof record
  3. dblink(text sql [, bool fail_on_error]) returns setof record

Description

dblinkexecutes a query (usually aSELECT, but it can be any SQL statement that returns rows) in a remote database.

When twotextarguments are given, the first one is first looked up as a persistent connection’s name; if found, the command is executed on that connection. If not found, the first argument is treated as a connection info string as fordblink_connect, and the indicated connection is made just for the duration of this command.

Arguments

connname

Name of the connection to use; omit this parameter to use the unnamed connection.

connstr

A connection info string, as previously described fordblink_connect.

sql

The SQL query that you wish to execute in the remote database, for exampleselect * from foo.

fail_on_error

If true (the default when omitted) then an error thrown on the remote side of the connection causes an error to also be thrown locally. If false, the remote error is locally reported as a NOTICE, and the function returns no rows.

Return Value

The function returns the row(s) produced by the query. Sincedblinkcan be used with any query, it is declared to returnrecord, rather than specifying any particular set of columns. This means that you must specify the expected set of columns in the calling query — otherwisePostgreSQLwould not know what to expect. Here is an example:

  1. SELECT *
  2. FROM dblink('dbname=mydb', 'select proname, prosrc from pg_proc')
  3. AS t1(proname name, prosrc text)
  4. WHERE proname LIKE 'bytea%';

The“alias”part of theFROMclause must specify the column names and types that the function will return. (Specifying column names in an alias is actually standard SQL syntax, but specifying column types is aPostgreSQLextension.) This allows the system to understand what*should expand to, and whatpronamein theWHEREclause refers to, in advance of trying to execute the function. At run time, an error will be thrown if the actual query result from the remote database does not have the same number of columns shown in theFROMclause. The column names need not match, however, anddblinkdoes not insist on exact type matches either. It will succeed so long as the returned data strings are valid input for the column type declared in theFROMclause.

Notes

A convenient way to usedblinkwith predetermined queries is to create a view. This allows the column type information to be buried in the view, instead of having to spell it out in every query. For example,

  1. CREATE VIEW myremote_pg_proc AS
  2. SELECT *
  3. FROM dblink('dbname=postgres', 'select proname, prosrc from pg_proc')
  4. AS t1(proname name, prosrc text);
  5. SELECT * FROM myremote_pg_proc WHERE proname LIKE 'bytea%';

Examples

  1. SELECT * FROM dblink('dbname=postgres', 'select proname, prosrc from pg_proc')
  2. AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
  3. proname | prosrc
  4. ------------+------------
  5. byteacat | byteacat
  6. byteaeq | byteaeq
  7. bytealt | bytealt
  8. byteale | byteale
  9. byteagt | byteagt
  10. byteage | byteage
  11. byteane | byteane
  12. byteacmp | byteacmp
  13. bytealike | bytealike
  14. byteanlike | byteanlike
  15. byteain | byteain
  16. byteaout | byteaout
  17. (12 rows)
  18. SELECT dblink_connect('dbname=postgres');
  19. dblink_connect
  20. ----------------
  21. OK
  22. (1 row)
  23. SELECT * FROM dblink('select proname, prosrc from pg_proc')
  24. AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
  25. proname | prosrc
  26. ------------+------------
  27. byteacat | byteacat
  28. byteaeq | byteaeq
  29. bytealt | bytealt
  30. byteale | byteale
  31. byteagt | byteagt
  32. byteage | byteage
  33. byteane | byteane
  34. byteacmp | byteacmp
  35. bytealike | bytealike
  36. byteanlike | byteanlike
  37. byteain | byteain
  38. byteaout | byteaout
  39. (12 rows)
  40. SELECT dblink_connect('myconn', 'dbname=regression');
  41. dblink_connect
  42. ----------------
  43. OK
  44. (1 row)
  45. SELECT * FROM dblink('myconn', 'select proname, prosrc from pg_proc')
  46. AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
  47. proname | prosrc
  48. ------------+------------
  49. bytearecv | bytearecv
  50. byteasend | byteasend
  51. byteale | byteale
  52. byteagt | byteagt
  53. byteage | byteage
  54. byteane | byteane
  55. byteacmp | byteacmp
  56. bytealike | bytealike
  57. byteanlike | byteanlike
  58. byteacat | byteacat
  59. byteaeq | byteaeq
  60. bytealt | bytealt
  61. byteain | byteain
  62. byteaout | byteaout
  63. (14 rows)