Skip to main content
Ctrl+K

Tantor SE 15.12 Documentation

EN RU

Table of contents:

  • Preface
    • 1.  What Is Tantor SE?
    • 2. A Brief History of PostgreSQL
    • 3. What are the differences between Tantor SE 15 and PostgreSQL 15
    • 4. Conventions
    • 5. Bug Reporting Guidelines
  • Part I. Tutorial
    • Chapter 1. Synopsis
    • Chapter 2. The SQL Language
    • Chapter 3. Advanced Features
  • Part II. The SQL Language
    • Chapter 4. SQL Syntax
    • Chapter 5. Data Definition
    • Chapter 6. Data Manipulation
    • Chapter 7. Queries
    • Chapter 8. Data Types
    • Chapter 9. Functions and Operators
    • Chapter 10. Type Conversion
    • Chapter 11. Indexes
    • Chapter 12. Full Text Search
    • Chapter 13. Concurrency Control
    • Chapter 14. Performance Tips
    • Chapter 15. Parallel Query
    • Chapter 16. Autonomous Transactions
  • Part III. Server Administration
    • Chapter 17. Installation from Binaries
    • Chapter 18. Server Setup and Operation
    • Chapter 19. Server Configuration
    • Chapter 20. Client Authentication
    • Chapter 21. Database Roles
    • Chapter 22. Managing Databases
    • Chapter 23. Localization
    • Chapter 24. Routine Database Maintenance Tasks
    • Chapter 25. Backup and Restore
    • Chapter 26. High Availability, Load Balancing, and Replication
    • Chapter 27. Monitoring Database Activity
    • Chapter 28. Monitoring Disk Usage
    • Chapter 29. Reliability and the Write-Ahead Log
    • Chapter 30. Logical Replication
    • Chapter 31. Just-in-Time Compilation (JIT)
  • Part IV. Client Interfaces
    • Chapter 32. libpq — C Library
    • Chapter 33. Large Objects
    • Chapter 34. ECPG — Embedded SQL in C
    • Chapter 35. The Information Schema
  • Part V. Server Programming
    • Chapter 36. Extending SQL
    • Chapter 37. Triggers
    • Chapter 38. Event Triggers
    • Chapter 39. The Rule System
    • Chapter 40. Procedural Languages
    • Chapter 41. PL/pgSQL — SQL Procedural Language
    • Chapter 42. PL/Tcl — Tcl Procedural Language
    • Chapter 43. PL/Perl — Perl Procedural Language
    • Chapter 44. PL/Python — Python Procedural Language
    • Chapter 45. Server Programming Interface
    • Chapter 46. Background Worker Processes
    • Chapter 47. Logical Decoding
    • Chapter 48. Replication Progress Tracking
    • Chapter 49. Archive Modules
  • Part VI. Reference
    • SQL Commands
    • PostgreSQL Client Applications
    • PostgreSQL Server Applications
  • Part VII. Internals
    • Chapter 50. Overview of PostgreSQL Internals
    • Chapter 51. System Catalogs
    • Chapter 52. System Views
    • Chapter 53. Frontend/Backend Protocol
    • Chapter 54. PostgreSQL Coding Conventions
    • Chapter 55. Native Language Support
    • Chapter 56. Writing a Procedural Language Handler
    • Chapter 57. Writing a Foreign Data Wrapper
    • Chapter 58. Writing a Table Sampling Method
    • Chapter 59. Writing a Custom Scan Provider
    • Chapter 60. Genetic Query Optimizer
    • Chapter 61. Table Access Method Interface Definition
    • Chapter 62. Index Access Method Interface Definition
    • Chapter 63. Generic WAL Records
    • Chapter 64. Custom WAL Resource Managers
    • Chapter 65. B-Tree Indexes
    • Chapter 66. GiST Indexes
    • Chapter 67. SP-GiST Indexes
    • Chapter 68. GIN Indexes
    • Chapter 69. BRIN Indexes
    • Chapter 70. Hash Indexes
    • Chapter 71. Database Physical Storage
    • Chapter 72. System Catalog Declarations and Initial Contents
    • Chapter 73. How the Planner Uses Statistics
    • Chapter 74. Backup Manifest Format
  • Part VIII. Appendixes
    • Appendix A. Tantor SE Error Codes
    • Appendix B. Date/Time Support
    • Appendix C. SQL Key Words
    • Appendix D. SQL Conformance
    • Appendix E. Release Notes
    • Appendix F. Additional Supplied Modules
    • Appendix G. Additional Supplied Programs
    • Appendix H. Additional External Modules
    • Appendix I. External Projects
    • Appendix J. Tantor SE Limits
    • Appendix K. Acronyms
    • Appendix L. Glossary
    • Appendix M. Color Support
    • Appendix N. Obsolete or Renamed Features
  • Bibliography
  • Index

dblink

dblink#

dblink
dblink
Prev UpF.15. dblinkHome Next

dblink

dblink — executes a query in a remote database

Synopsis

dblink(text connname, text sql [, bool fail_on_error]) returns setof record
dblink(text connstr, text sql [, bool fail_on_error]) returns setof record
dblink(text sql [, bool fail_on_error]) returns setof record

Description

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

When two text arguments 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 for dblink_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 for dblink_connect.

sql

The SQL query that you wish to execute in the remote database, for example select * 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. Since dblink can be used with any query, it is declared to return record, rather than specifying any particular set of columns. This means that you must specify the expected set of columns in the calling query — otherwise Tantor SE would not know what to expect. Here is an example:

SELECT *
    FROM dblink('dbname=mydb options=-csearch_path=',
                'select proname, prosrc from pg_proc')
      AS t1(proname name, prosrc text)
    WHERE proname LIKE 'bytea%';

The “alias” part of the FROM clause 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 a Tantor SE extension.) This allows the system to understand what * should expand to, and what proname in the WHERE clause 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 the FROM clause. The column names need not match, however, and dblink does 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 the FROM clause.

Notes

A convenient way to use dblink with 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,

CREATE VIEW myremote_pg_proc AS
  SELECT *
    FROM dblink('dbname=postgres options=-csearch_path=',
                'select proname, prosrc from pg_proc')
    AS t1(proname name, prosrc text);

SELECT * FROM myremote_pg_proc WHERE proname LIKE 'bytea%';

Examples

SELECT * FROM dblink('dbname=postgres options=-csearch_path=',
                     'select proname, prosrc from pg_proc')
  AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
  proname   |   prosrc
------------+------------
 byteacat   | byteacat
 byteaeq    | byteaeq
 bytealt    | bytealt
 byteale    | byteale
 byteagt    | byteagt
 byteage    | byteage
 byteane    | byteane
 byteacmp   | byteacmp
 bytealike  | bytealike
 byteanlike | byteanlike
 byteain    | byteain
 byteaout   | byteaout
(12 rows)

SELECT dblink_connect('dbname=postgres options=-csearch_path=');
 dblink_connect
----------------
 OK
(1 row)

SELECT * FROM dblink('select proname, prosrc from pg_proc')
  AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
  proname   |   prosrc
------------+------------
 byteacat   | byteacat
 byteaeq    | byteaeq
 bytealt    | bytealt
 byteale    | byteale
 byteagt    | byteagt
 byteage    | byteage
 byteane    | byteane
 byteacmp   | byteacmp
 bytealike  | bytealike
 byteanlike | byteanlike
 byteain    | byteain
 byteaout   | byteaout
(12 rows)

SELECT dblink_connect('myconn', 'dbname=regression options=-csearch_path=');
 dblink_connect
----------------
 OK
(1 row)

SELECT * FROM dblink('myconn', 'select proname, prosrc from pg_proc')
  AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
  proname   |   prosrc
------------+------------
 bytearecv  | bytearecv
 byteasend  | byteasend
 byteale    | byteale
 byteagt    | byteagt
 byteage    | byteage
 byteane    | byteane
 byteacmp   | byteacmp
 bytealike  | bytealike
 byteanlike | byteanlike
 byteacat   | byteacat
 byteaeq    | byteaeq
 bytealt    | bytealt
 byteain    | byteain
 byteaout   | byteaout
(14 rows)

Prev Up Next
dblink_disconnect Home dblink_exec
Commercial use is possible only with written permission from the company LLC “Tantor Labs”
© LLC "Tantor Labs"
  • Technical Support Policy
  • Cookie Processing Policy
  • Website User Agreement
Products
  • Tantor DBMS
  • Tantor Platform
  • Documentation
Contacts
  • +7 495 369-48-16
  • info@tantorlabs.ru
15.12.0 - 3069e28c - 2025-04-10 15:18:39