diff --git a/CN/modules/ROOT/nav.adoc b/CN/modules/ROOT/nav.adoc index 68f93003..dd5aa589 100644 --- a/CN/modules/ROOT/nav.adoc +++ b/CN/modules/ROOT/nav.adoc @@ -76,6 +76,7 @@ *** xref:master/ecosystem_components/zhparser.adoc[zhparser] *** xref:master/ecosystem_components/pgbackrest.adoc[pgBackRest] *** xref:master/ecosystem_components/set_user.adoc[set_user] +*** xref:master/ecosystem_components/dblink.adoc[dblink] * 监控运维 ** xref:master/getting-started/daily_monitoring.adoc[日常监控] ** xref:master/getting-started/daily_maintenance.adoc[日常维护] diff --git a/CN/modules/ROOT/pages/master/ecosystem_components/dblink.adoc b/CN/modules/ROOT/pages/master/ecosystem_components/dblink.adoc new file mode 100644 index 00000000..5c5bba28 --- /dev/null +++ b/CN/modules/ROOT/pages/master/ecosystem_components/dblink.adoc @@ -0,0 +1,111 @@ +:sectnums: +:sectnumlevels: 5 + += dblink + +== 概述 + +`dblink` 可从当前数据库向另一个 IvorySQL 或 PostgreSQL 数据库执行 SQL,支持一次性查询、可复用的命名连接、远程事务、非查询命令和异步查询。 + +本文使用 IvorySQL 5.6(PostgreSQL 18.6)完成验证:自带 PostgreSQL 回归测试通过,并实际覆盖了命名连接、DML、事务、异步结果、UTF-8 数据和 Oracle 兼容模式会话。 + +== 编译安装 + +`dblink` 位于 IvorySQL 源码树中,二进制发行版通常已包含它。如当前安装中缺失,请从匹配的 IvorySQL 5 源码编译: + +[source,bash] +---- +git clone https://github.com/IvorySQL/IvorySQL.git +cd IvorySQL +git checkout IVORY_REL_5_STABLE +cd contrib/dblink +make USE_PGXS=1 PG_CONFIG=/path-to/ivorysql/bin/pg_config +sudo make USE_PGXS=1 PG_CONFIG=/path-to/ivorysql/bin/pg_config install +---- + +在每个需要调用 `dblink` 的本地数据库中创建扩展: + +[source,sql] +---- +CREATE EXTENSION dblink; +---- + +== 查询远程数据库 + +先在远程数据库创建测试数据: + +[source,sql] +---- +CREATE TABLE remote_measurement ( + id integer PRIMARY KEY, + label text NOT NULL, + reading numeric NOT NULL +); +INSERT INTO remote_measurement VALUES (1, '北京', 21.5), (2, 'Shanghai', 24.0); +---- + +建立命名连接,并显式声明返回列类型: + +[source,sql] +---- +SELECT dblink_connect( + 'analytics', + 'host=127.0.0.1 port=5432 dbname=remote_db user=ivorysql' +); + +SELECT * +FROM dblink('analytics', + 'SELECT id, label, reading FROM remote_measurement ORDER BY id') + AS t(id integer, label text, reading numeric); +---- + +使用 `dblink_exec` 执行不返回行的命令: + +[source,sql] +---- +SELECT dblink_exec('analytics', + $$UPDATE remote_measurement SET reading = 22.0 WHERE id = 1$$); +---- + +== 远程事务与异步查询 + +[source,sql] +---- +SELECT dblink_exec('analytics', 'BEGIN'); +SELECT dblink_exec('analytics', + $$INSERT INTO remote_measurement VALUES (3, 'temporary', 0)$$); +SELECT dblink_exec('analytics', 'ROLLBACK'); + +SELECT dblink_send_query('analytics', + 'SELECT id, label FROM remote_measurement ORDER BY id'); +SELECT * FROM dblink_get_result('analytics') AS t(id integer, label text); +-- 复用连接前,继续调用一次以取尽最后的空结果。 +SELECT * FROM dblink_get_result('analytics') AS t(id integer, label text); + +SELECT dblink_disconnect('analytics'); +---- + +== Oracle 兼容模式 + +即使本地会话使用 Oracle 兼容语法,`dblink` 仍通过 PostgreSQL/libpq 协议连接。以下语句已通过 IvorySQL Oracle 兼容端口验证: + +[source,sql] +---- +SET ivorysql.compatible_mode = oracle; +SELECT 1 FROM dual; + +SELECT * +FROM dblink('host=127.0.0.1 port=5432 dbname=remote_db user=ivorysql', + 'SELECT id, label FROM remote_measurement ORDER BY id') + AS t(id integer, label text); +---- + +== 安全与运维说明 + +* 避免在 SQL 中直接写密码。优先使用 service 文件、权限受限的 `.pgpass`、证书认证或其他服务端凭据机制。 +* 只向可信角色授予 `dblink` 权限;远程 SQL 使用远程连接角色的权限执行。 +* 对返回行的调用始终显式声明列定义。 +* 固定远程 `search_path`、使用带 schema 的对象名,并在不可信网络上启用 TLS。 +* 命名连接只属于当前会话。用 `dblink_disconnect` 关闭连接,并在发送下一条命令前取尽异步结果。 + +完整函数说明请参阅 https://www.postgresql.org/docs/current/dblink.html[PostgreSQL dblink 文档]。 diff --git a/CN/modules/ROOT/pages/master/ecosystem_components/ecosystem_overview.adoc b/CN/modules/ROOT/pages/master/ecosystem_components/ecosystem_overview.adoc index 074ce67c..933022ea 100644 --- a/CN/modules/ROOT/pages/master/ecosystem_components/ecosystem_overview.adoc +++ b/CN/modules/ROOT/pages/master/ecosystem_components/ecosystem_overview.adoc @@ -41,6 +41,7 @@ IvorySQL 作为一款兼容 Oracle 且基于 PostgreSQL 的高级开源数据库 | 28 | xref:master/ecosystem_components/zhparser.adoc[zhparser] | master branch | 用于中文全文搜索的PostgreSQL插件,基于SCWS(即:简易中文分词系统)实现了一个中文解析器 | 搜索引擎、关键字提取 | 29 | xref:master/ecosystem_components/pgbackrest.adoc[pgBackRest] | 2.58.0 | 可靠的 PostgreSQL 备份和恢复解决方案 | 容灾备份、大库备份、异地/多层容灾 | 30 | xref:master/ecosystem_components/set_user.adoc[set_user] | REL4_2_0 | PostgreSQL 安全审计扩展,可控角色切换,支持白名单、强制审计、拦截高危操作 | 可控角色切换、权限管理、审计日志 +| 31 | xref:master/ecosystem_components/dblink.adoc[dblink] | IvorySQL 5.6 | 在另一个 IvorySQL 或 PostgreSQL 数据库中执行查询和命令 | 跨库查询、数据交换、管理自动化 |==== 这些插件均经过 IvorySQL 团队的测试和适配,确保在 IvorySQL 环境下稳定运行。用户可以根据业务需求选择合适的插件,进一步提升数据库系统的能力和灵活性。 diff --git a/EN/modules/ROOT/nav.adoc b/EN/modules/ROOT/nav.adoc index 1103c116..685dbac1 100644 --- a/EN/modules/ROOT/nav.adoc +++ b/EN/modules/ROOT/nav.adoc @@ -76,6 +76,7 @@ *** xref:master/ecosystem_components/zhparser_en.adoc[zhparser] *** xref:master/ecosystem_components/pgbackrest.adoc[pgBackRest] *** xref:master/ecosystem_components/set_user.adoc[set_user] +*** xref:master/ecosystem_components/dblink.adoc[dblink] * Monitor and O&M ** xref:master/getting-started/daily_monitoring.adoc[Monitoring] ** xref:master/getting-started/daily_maintenance.adoc[Maintenance] diff --git a/EN/modules/ROOT/pages/master/ecosystem_components/dblink.adoc b/EN/modules/ROOT/pages/master/ecosystem_components/dblink.adoc new file mode 100644 index 00000000..b4d9214b --- /dev/null +++ b/EN/modules/ROOT/pages/master/ecosystem_components/dblink.adoc @@ -0,0 +1,111 @@ +:sectnums: +:sectnumlevels: 5 + += dblink + +== Overview + +`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. + +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. + +== Build and install + +`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: + +[source,bash] +---- +git clone https://github.com/IvorySQL/IvorySQL.git +cd IvorySQL +git checkout IVORY_REL_5_STABLE +cd contrib/dblink +make USE_PGXS=1 PG_CONFIG=/path-to/ivorysql/bin/pg_config +sudo make USE_PGXS=1 PG_CONFIG=/path-to/ivorysql/bin/pg_config install +---- + +Create the extension in each local database that will call `dblink`: + +[source,sql] +---- +CREATE EXTENSION dblink; +---- + +== Query a remote database + +Create sample data in the remote database: + +[source,sql] +---- +CREATE TABLE remote_measurement ( + id integer PRIMARY KEY, + label text NOT NULL, + reading numeric NOT NULL +); +INSERT INTO remote_measurement VALUES (1, '北京', 21.5), (2, 'Shanghai', 24.0); +---- + +Connect and declare the returned column types explicitly: + +[source,sql] +---- +SELECT dblink_connect( + 'analytics', + 'host=127.0.0.1 port=5432 dbname=remote_db user=ivorysql' +); + +SELECT * +FROM dblink('analytics', + 'SELECT id, label, reading FROM remote_measurement ORDER BY id') + AS t(id integer, label text, reading numeric); +---- + +Use `dblink_exec` for commands that do not return rows: + +[source,sql] +---- +SELECT dblink_exec('analytics', + $$UPDATE remote_measurement SET reading = 22.0 WHERE id = 1$$); +---- + +== Remote transactions and asynchronous queries + +[source,sql] +---- +SELECT dblink_exec('analytics', 'BEGIN'); +SELECT dblink_exec('analytics', + $$INSERT INTO remote_measurement VALUES (3, 'temporary', 0)$$); +SELECT dblink_exec('analytics', 'ROLLBACK'); + +SELECT dblink_send_query('analytics', + 'SELECT id, label FROM remote_measurement ORDER BY id'); +SELECT * FROM dblink_get_result('analytics') AS t(id integer, label text); +-- Drain the final empty result before reusing the connection. +SELECT * FROM dblink_get_result('analytics') AS t(id integer, label text); + +SELECT dblink_disconnect('analytics'); +---- + +== Oracle-compatible mode + +`dblink` uses PostgreSQL/libpq connections, including when the local session uses Oracle-compatible syntax. The following was validated through the IvorySQL Oracle-compatible port: + +[source,sql] +---- +SET ivorysql.compatible_mode = oracle; +SELECT 1 FROM dual; + +SELECT * +FROM dblink('host=127.0.0.1 port=5432 dbname=remote_db user=ivorysql', + 'SELECT id, label FROM remote_measurement ORDER BY id') + AS t(id integer, label text); +---- + +== Security and operational notes + +* Avoid embedding passwords in SQL. Prefer a service file, `.pgpass` with restrictive permissions, certificate authentication, or another server-side credential mechanism. +* Grant `dblink` access only to trusted roles. Remote SQL runs with the privileges of the remote connection. +* Always provide an explicit column definition for row-returning calls. +* Fix the remote `search_path`, schema-qualify objects, and use TLS for untrusted networks. +* A named connection is session-local. Close it with `dblink_disconnect`, and fully consume asynchronous results before sending another command. + +See https://www.postgresql.org/docs/current/dblink.html[the upstream dblink documentation] for the complete function reference. diff --git a/EN/modules/ROOT/pages/master/ecosystem_components/ecosystem_overview.adoc b/EN/modules/ROOT/pages/master/ecosystem_components/ecosystem_overview.adoc index 4e3270c9..fcb87579 100644 --- a/EN/modules/ROOT/pages/master/ecosystem_components/ecosystem_overview.adoc +++ b/EN/modules/ROOT/pages/master/ecosystem_components/ecosystem_overview.adoc @@ -42,6 +42,7 @@ IvorySQL, as an advanced open-source database compatible with Oracle and based o |*28*| xref:master/ecosystem_components/zhparser_en.adoc[zhparser] | master branch | PostgreSQL extension for full-text search of Chinese language (Mandarin Chinese). It implements a Chinese language parser base on the | Search engine、keyword extraction |*29*| xref:master/ecosystem_components/pgbackrest.adoc[pgBackRest] | 2.58.0 | pgBackRest is a reliable backup and restore solution for PostgreSQL that seamlessly scales up to the largest databases and workloads | Disaster recovery backup, large database backup, off-site/multi-tier disaster recovery | *30* | xref:master/ecosystem_components/set_user.adoc[set_user] | REL4_2_0 | PostgreSQL security auditing extension with controlled role switching, supporting allowlists, enforced auditing, and blocking of high-risk operations | Controlled role switching, privilege management, audit logging +| *31* | xref:master/ecosystem_components/dblink.adoc[dblink] | IvorySQL 5.6 | Executes queries and commands in another IvorySQL or PostgreSQL database | Cross-database queries, data exchange, administrative automation |==== These plugins have all been tested and adapted by the IvorySQL team to ensure stable operation in the IvorySQL environment. Users can select appropriate plugins based on business needs to further enhance the capabilities and flexibility of the database system.