KingbaseES数据库使用kdb_database_link扩展常见问题

kdb_database_link主要功能是为了满足@link语法的适配,让用户应用的代码能够适用于更宽泛的产品而无需在移植时大量修改。

  • 支持连接管理,在适当的时候关闭连接减少远程数据库的资源开销。
  • 支持远程表(视图、物化视图)的查询,并且支持下推查询条件减少数据传输的网络开销。
  • 支持远程表的插入。
  • 支持远程序列的访问。
  • 支持远程用户自定义函数(存储过程)的访问。目前仅支持KingbaseES数据库。

一、环境要求:

1.配置好ODBC,并且通过isql可以正常访问远程数据库。

ODBC连接配置请参考:https://www.cnblogs.com/kingbase/p/14921071.html

[kes_v8r6c7b12@node2 ~]$ isql -v kes
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL> select count(1) from t1;
+---------------------+
| COUNT               |
+---------------------+
| 24                  |
+---------------------+
SQLRowCount returns 1
1 rows fetched

2.数据库有kdb_database_link扩展插件:

--登录数据库查询是否有kdb_database_link扩展插件
[kes_v8r6c7b12@node2 ~]$ ksql -Usystem -dtest -p5432
ksql (V8.0)
Type "help" for help.

test=# \dx
                                                                      List of installed extensions
           Name           | Version |    Schema    |                                                     Description                                                     
--------------------------+---------+--------------+---------------------------------------------------------------------------------------------------------------------
 btree_gin                | 1.3     | public       | support for indexing common datatypes in GIN
 btree_gist               | 1.5     | public       | support for indexing common datatypes in GiST
 dbms_ddl                 | 1.0     | sys          | DBMS_DDL system package
 dbms_lob                 | 1.1     | public       | dbms_lob package
 dbms_metadata            | 1.0     | public       | DBMS_METADATA system package
 dbms_obfuscation_toolkit | 1.0     | sys          | dbms_obfuscation_toolkit system package
 dbms_output              | 1.0     | sys          | DBMS_OUTPUT system package
 dbms_utility             | 1.0     | sys          | dbms_utility extension package
 kbcrypto                 | 1.3     | public       | cryptographic functions
 kdb_cast                 | 1.0     | sys          | kdb_cast extension
 kdb_database_link        | 1.0     | public       | connect to other databases with a database link object
 kdb_license              | 1.0     | pg_catalog   | kdb_license extension
 kdb_oracle_datatype      | 1.5     | sys          | kdb_oracle_datatype extension
 kdb_orafce               | 3.9     | public       | Functions and operators that emulate a subset of functions and packages from the Oracle RDBMS
 kdb_raw                  | 1.0     | public       | support RAW data and functions in KDB
 kdb_schedule             | 1.0     | sys          | A KingbaseES job scheduler
 kdb_tinyint              | 1.0     | pg_catalog   | Create a new data type tinyint and its functions operators and indexes
 kdb_utils_function       | 1.3     | public       | Functions and operators that emulate a subset of functions and packages from the Oracle RDBMS
 kingbase_version         | 1.0     | pg_catalog   | This is a utility that provides function related to version number,  it is used to get the Kingbase version number.
 owa_util                 | 1.0     | sys          | owa_util system package
 pageinspect              | 1.8     | public       | inspect the contents of database pages at a low level
 pldbgapi                 | 1.2     | public       | server-side support for debugging PL/pgSQL functions
 plpgsql                  | 1.0     | pg_catalog   | PL/pgSQL procedural language
 plsql                    | 1.0     | pg_catalog   | PL/SQL procedural language
 plsql_json               | 1.0     | public       | KingbaseES plsql_json
 plsql_pldbgapi           | 1.2     | public       | server-side support for debugging PL/SQL functions
 plsql_plprofiler         | 4.2     | public       | server-side support for profiling PL/SQL functions
 sys_anon                 | 1.0     | anon         | provides data masking functionality
 sys_bulkload             | 2.0     | public       | sys_bulkload is a high speed data loading utility for KingbaseES
 sys_freespacemap         | 1.2     | sys          | examine the free space map (FSM)
 sys_stat_statements      | 1.10    | public       | track parsing, planning and execution statistics of all SQL statements executed
 sys_trgm                 | 1.4     | public       | text similarity measurement and index searching based on trigrams
 sysaudit                 | 1.0     | sysaudit     | provides auditing functionality
 sysmac                   | 1.0     | sysmac       | Mac for Kingbase
 uuid-ossp                | 1.1     | public       | generate universally unique identifiers (UUIDs)
 xlog_record_read         | 1.0     | pg_catalog   | xlog_record_read functions
 zhparser                 | 1.0     | public       | a parser for full-text search of Chinese
(38 rows)

--如果数据库没有kdb_database_link扩展插件,使用以下命令创建
test=# create extension kdb_database_link ;
CREATE EXTENSION

二、适用于:

KingbaseES所有版本。

二、问题现象:

场景1:创建dblink连接提示 ERROR: foreign-data wrapper "kingbase_fdw" does not exist

--使用以下语句创建dblink报错
test=# create public database link kes_link connect to 'system' identified by 'system' using(DriverName='KES ODBC Driver',Host='192.168.10.40',Port=5432,Dbname='test',Dbtype='kingbase');
ERROR:  foreign-data wrapper "kingbase_fdw" does not exist
--创建dblink后重新登录数据库使用dblink查询报错

[kes_v8r6c7b12@node2 ~]$ ksql -Usystem -dtest -p5432
ksql (V8.0)
Type "help" for help.

test=# select * from t1@kes_link;
ERROR:  Unsupported for database link.

--查询dblink是否存在
test=# select * from user_db_links ;
 OWNER  | DB_LINK  | USERNAME |     HOST      |            CREATED            
--------+----------+----------+---------------+-------------------------------
 system | kes_link | system   | 192.168.10.40 | 2023-07-21 17:06:23.387985+08
(1 row)

test=# select * from all_db_links ;
 OWNER  | DB_LINK  | USERNAME |     HOST      |            CREATED            
--------+----------+----------+---------------+-------------------------------
 system | kes_link | system   | 192.168.10.40 | 2023-07-21 17:06:23.387985+08
(1 row)

场景3:删除dblink提示 ERROR: unrecognized class ID: 8070

test=# drop database link kes_link;      
ERROR:  unrecognized class ID: 8070
test=# drop database link kes_link;
ERROR:  database link "kes_link" does not exist

场景5:创建dblink提示:ERROR: unrecognized node type: 360

test=# create public database link kes_link connect to 'system' identified by 'system' using(DriverName='KES ODBC Driver',Host='192.168.10.40',Port=5432,Dbname='test',Dbtype='kingbase');
ERROR:  unrecognized node type: 360

三、解决方法:

场景1:

问题原因
  • kdb_database_link主要功能是为了兼容Oracle数据库@link语法的适配,实际是在kingbase_fdw上层进行了包装,所以还需要安装kingbase_fdw扩展。
问题解决:
--创建kingbase_fdw扩展插件
test=# create extension kingbase_fdw;
CREATE EXTENSION
--创建dblink
test=# create public database link kes_link connect to 'system' identified by 'system' using(DriverName='KES ODBC Driver',Host='192.168.10.40',Port=5432,Dbname='test',Dbtype='kingbase');
CREATE DATABASE LINK

--使用dblink查询数据
test=# select * from t1@kes_link;
 ID | NAME 
----+------
  1 | tx
  2 | llx
(2 rows)

场景2:

问题原因
  • 数据库kingbase.conf配置文件shared_preload_libraries 没有添加kdb_database_link扩展插件。
  • 创建kdb_database_link扩展插件后,在当前的连接可以使用dblink查询数据。主要是由于创建kdb_database_link扩展插件后扩展调用的kdb_database_link共享库文件在当前连接进程里面有加载。
  • 退出或者建立新的连接后,新开的连接进程里面未加载kdb_database_link扩展插件,使用dblink查询就会报错。
问题解决:
  • 在数据库kingbase.conf配置文件shared_preload_libraries添加 kdb_database_link扩展。
--在kingbase.conf文件shared_preload_libraries添加kdb_database_link
shared_preload_libraries = 'kdb_database_link,...'

--添加后重启数据库
[kes_v8r6c7b12@node2 ~]$ sys_ctl -D ora_data/ restart
waiting for server to shut down.... done
server stopped
waiting for server to start....2023-07-21 17:29:07.986 CST [20547] LOG:  sepapower extension initialized
2023-07-21 17:29:07.993 CST [20547] LOG:  starting KingbaseES V008R006C007B0012 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46), 64-bit
2023-07-21 17:29:07.993 CST [20547] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2023-07-21 17:29:07.993 CST [20547] LOG:  listening on IPv6 address "::", port 5432
2023-07-21 17:29:07.999 CST [20547] LOG:  listening on Unix socket "/tmp/.s.KINGBASE.5432"
2023-07-21 17:29:08.046 CST [20547] LOG:  redirecting log output to logging collector process
2023-07-21 17:29:08.046 CST [20547] HINT:  Future log output will appear in directory "sys_log".
 done
server started

--再次登录数据库进行查询
[kes_v8r6c7b12@node2 ~]$ ksql -Usystem -dtest -p5432
ksql (V8.0)
Type "help" for help.

test=# select * from user_db_links ;
 OWNER  | DB_LINK  | USERNAME |     HOST      |            CREATED            
--------+----------+----------+---------------+-------------------------------
 system | kes_link | system   | 192.168.10.40 | 2023-07-21 17:06:23.387985+08
(1 row)

test=# select * from t1@kes_link;   
 ID | NAME 
----+------
  1 | tx
  2 | llx
(2 rows)

场景3:

问题原因:
  • 数据库kingbase.conf配置文件shared_preload_libraries 没有添加kdb_database_link扩展插件。
问题解决:
  • 在数据库kingbase.conf配置文件shared_preload_libraries添加 kdb_database_link扩展。

场景4:

问题原因:
  • 删除dblink时未指定dblink所在的schema。
问题解决:
  • 删除dblink时指定dblink所在schema。
--删除成功
test=# drop database link public.kes_link;
DROP DATABASE LINK

场景5:

问题原因:
  • 数据库kingbase.conf配置文件shared_preload_libraries 没有添加kdb_database_link扩展插件。
问题解决:
  • 在数据库kingbase.conf配置文件shared_preload_libraries添加 kdb_database_link扩展。
--kingbase.conf配置文件shared_preload_libraries添加 kdb_database_link扩展后重启数据库
--再次登录创建成功
[kes_v8r6c7b12@node2 ~]$ ksql -Usystem -dtest -p5432
ksql (V8.0)
Type "help" for help.

test=# create public database link kes_link connect to 'system' identified by 'system' using(DriverName='KES ODBC Driver',Host='192.168.10.40',Port=5432,Dbname='test',Dbtype='kingbase');
CREATE DATABASE LINK
test=# select * from t1@kes_link;                                                                                                                                                         
 ID | NAME 
----+------
  1 | tx
  2 | llx
(2 rows)

四、总结:

  • shared_preload_libraries是配置参数,用来指定在数据库服务器启动过程中应加载到内存中的共享库。共享库是KingbaseES扩展功能模块。
  • shared_preload_libraries参数的值是一个以逗号分隔的共享库名称列表,如果名称中包含空格或逗号,需要用双引号括起来。例如:"my library"
  • 此参数只能在服务器启动时设置,共享库需要在启动时加载才能正常工作,不能在运行时修改。如果修改了这个参数的值,需要重启服务器才能生效。
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐