|
| 1 | +:sectnums: |
| 2 | +:sectnumlevels: 5 |
| 3 | + |
| 4 | += dblink |
| 5 | + |
| 6 | +== Overview |
| 7 | + |
| 8 | +`dblink` executes SQL in another IvorySQL or PostgreSQL database from the current database. It supports one-shot queries, reusable named connections, remote transactions, non-query commands, and asynchronous queries. |
| 9 | + |
| 10 | +This guide was validated with IvorySQL 5.6 (PostgreSQL 18.6). The bundled PostgreSQL regression suite passed, and named connections, DML, transactions, asynchronous results, UTF-8 data, and Oracle-compatible sessions were tested end to end. |
| 11 | + |
| 12 | +== Build and install |
| 13 | + |
| 14 | +`dblink` is included in the IvorySQL source tree and binary distributions normally install it. If it is absent, build it from the matching IvorySQL 5 source tree: |
| 15 | + |
| 16 | +[source,bash] |
| 17 | +---- |
| 18 | +git clone https://github.com/IvorySQL/IvorySQL.git |
| 19 | +cd IvorySQL |
| 20 | +git checkout IVORY_REL_5_STABLE |
| 21 | +cd contrib/dblink |
| 22 | +make USE_PGXS=1 PG_CONFIG=/path-to/ivorysql/bin/pg_config |
| 23 | +sudo make USE_PGXS=1 PG_CONFIG=/path-to/ivorysql/bin/pg_config install |
| 24 | +---- |
| 25 | + |
| 26 | +Create the extension in each local database that will call `dblink`: |
| 27 | + |
| 28 | +[source,sql] |
| 29 | +---- |
| 30 | +CREATE EXTENSION dblink; |
| 31 | +---- |
| 32 | + |
| 33 | +== Query a remote database |
| 34 | + |
| 35 | +Create sample data in the remote database: |
| 36 | + |
| 37 | +[source,sql] |
| 38 | +---- |
| 39 | +CREATE TABLE remote_measurement ( |
| 40 | + id integer PRIMARY KEY, |
| 41 | + label text NOT NULL, |
| 42 | + reading numeric NOT NULL |
| 43 | +); |
| 44 | +INSERT INTO remote_measurement VALUES (1, '北京', 21.5), (2, 'Shanghai', 24.0); |
| 45 | +---- |
| 46 | + |
| 47 | +Connect and declare the returned column types explicitly: |
| 48 | + |
| 49 | +[source,sql] |
| 50 | +---- |
| 51 | +SELECT dblink_connect( |
| 52 | + 'analytics', |
| 53 | + 'host=127.0.0.1 port=5432 dbname=remote_db user=ivorysql' |
| 54 | +); |
| 55 | +
|
| 56 | +SELECT * |
| 57 | +FROM dblink('analytics', |
| 58 | + 'SELECT id, label, reading FROM remote_measurement ORDER BY id') |
| 59 | + AS t(id integer, label text, reading numeric); |
| 60 | +---- |
| 61 | + |
| 62 | +Use `dblink_exec` for commands that do not return rows: |
| 63 | + |
| 64 | +[source,sql] |
| 65 | +---- |
| 66 | +SELECT dblink_exec('analytics', |
| 67 | + $$UPDATE remote_measurement SET reading = 22.0 WHERE id = 1$$); |
| 68 | +---- |
| 69 | + |
| 70 | +== Remote transactions and asynchronous queries |
| 71 | + |
| 72 | +[source,sql] |
| 73 | +---- |
| 74 | +SELECT dblink_exec('analytics', 'BEGIN'); |
| 75 | +SELECT dblink_exec('analytics', |
| 76 | + $$INSERT INTO remote_measurement VALUES (3, 'temporary', 0)$$); |
| 77 | +SELECT dblink_exec('analytics', 'ROLLBACK'); |
| 78 | +
|
| 79 | +SELECT dblink_send_query('analytics', |
| 80 | + 'SELECT id, label FROM remote_measurement ORDER BY id'); |
| 81 | +SELECT * FROM dblink_get_result('analytics') AS t(id integer, label text); |
| 82 | +-- Drain the final empty result before reusing the connection. |
| 83 | +SELECT * FROM dblink_get_result('analytics') AS t(id integer, label text); |
| 84 | +
|
| 85 | +SELECT dblink_disconnect('analytics'); |
| 86 | +---- |
| 87 | + |
| 88 | +== Oracle-compatible mode |
| 89 | + |
| 90 | +`dblink` uses PostgreSQL/libpq connections, including when the local session uses Oracle-compatible syntax. The following was validated through the IvorySQL Oracle-compatible port: |
| 91 | + |
| 92 | +[source,sql] |
| 93 | +---- |
| 94 | +SET ivorysql.compatible_mode = oracle; |
| 95 | +SELECT 1 FROM dual; |
| 96 | +
|
| 97 | +SELECT * |
| 98 | +FROM dblink('host=127.0.0.1 port=5432 dbname=remote_db user=ivorysql', |
| 99 | + 'SELECT id, label FROM remote_measurement ORDER BY id') |
| 100 | + AS t(id integer, label text); |
| 101 | +---- |
| 102 | + |
| 103 | +== Security and operational notes |
| 104 | + |
| 105 | +* Avoid embedding passwords in SQL. Prefer a service file, `.pgpass` with restrictive permissions, certificate authentication, or another server-side credential mechanism. |
| 106 | +* Grant `dblink` access only to trusted roles. Remote SQL runs with the privileges of the remote connection. |
| 107 | +* Always provide an explicit column definition for row-returning calls. |
| 108 | +* Fix the remote `search_path`, schema-qualify objects, and use TLS for untrusted networks. |
| 109 | +* A named connection is session-local. Close it with `dblink_disconnect`, and fully consume asynchronous results before sending another command. |
| 110 | + |
| 111 | +See https://www.postgresql.org/docs/current/dblink.html[the upstream dblink documentation] for the complete function reference. |
0 commit comments