加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

PostgreSQL报跨库异常及解决一例

发布时间:2020-12-13 17:41:00 所属栏目:百科 来源:网络整理
导读:今天群里一个哥们发了个错误信息: ERROR: cross-database references are not implemented: "public.test.id"********** 错误 **********ERROR: cross-database references are not implemented: "public.test.id"SQL 状态: 0A000 看到贴出的SQL语句时就发现
今天群里一个哥们发了个错误信息:
ERROR:  cross-database references are not implemented: "public.test.id"

********** 错误 **********

ERROR: cross-database references are not implemented: "public.test.id"
SQL 状态: 0A000
看到贴出的SQL语句时就发现他的错误了
comment on table public.test is '测试表';
comment on table public.test.id is '测试ID';
..
给字段加备注的时候应该是column,而不是table。修改完备注SQL就不会报错。
报这种错误,得需要提醒一下,postgresql的体系结构认为不同的数据库是独立的,所以并不支持跨库查询。而且执行一个查询时,postgresql首先会检查自身的dbname(一般连接的当前DB名,可不写),然后是schema(因为一个DB里可包含多个schema,默认连接是public),再最后才是数据库对象(table,view,function...)。
所以当出现cross-database错误时我们就可以认为postgres认为用户做了一次非规范的跨库操作了。

回过头来想一想,什么情况下会报这种错误呢,我整理了一下,有以下这么几种情况:

测试环境:
postgresql 9.1.2
postgres=# l
                                资料库列表
   名称    |  拥有者  | 字元编码 | Collate | Ctype |       存取权限
-----------+----------+----------+---------+-------+-----------------------
 d_kenyon  | postgres | UTF8     | C       | C     |
 postgres  | postgres | UTF8     | C       | C     |
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
(4 行记录)

postgres=# d
                   关联列表
 架构模式 |      名称      |  型别  |  拥有者
----------+----------------+--------+----------
 public   | pgweb          | 资料表 | postgres
 public   | t_kenyon       | 资料表 | postgres
 public   | tbl_order_item | 资料表 | postgres
 public   | tbl_orders     | 资料表 | postgres
 public   | tbl_product    | 资料表 | postgres
(5 行记录)

postgres=# select current_user;
 current_user
--------------
 postgres
(1 行记录)

postgres=# c d_kenyon
You are now connected to database "d_kenyon" as user "postgres".
d_kenyon=# d
               关联列表
 架构模式 | 名称  |  型别  |  拥有者
----------+-------+--------+----------
 public   | test  | 资料表 | postgres
 public   | test2 | 资料表 | postgres
(2 行记录)
1.真正地做了一次跨库查询
d_kenyon=# select *from postgres.public.t_kenyon limit 1;
ERROR:  cross-database references are not implemented: "postgres.public.t_kenyon
"
第1行select *from postgres.public.t_kenyon limit 1;
                  ^
d_kenyon=#
2.没有做跨库查询,但是pg误认为你做了一次跨库操作
postgres=# d
                   关联列表
 架构模式 |      名称      |  型别  |  拥有者
----------+----------------+--------+----------
 public   | pgweb          | 资料表 | postgres
 public   | t_kenyon       | 资料表 | postgres
 public   | tbl_order_item | 资料表 | postgres
 public   | tbl_orders     | 资料表 | postgres
 public   | tbl_product    | 资料表 | postgres
(5 行记录)

postgres=# d t_kenyon
 资料表 "public.t_kenyon"
  栏位  |  型别   | 修饰词
--------+---------+--------
 id     | integer |
 t_name | text    |

postgres=# select * from public.t_kenyon limit 1;
 id | t_name
----+--------
  1 | kenyon
(1 行记录)

postgres=# select * from public.t_kenyon.id limit 1;
ERROR:  cross-database references are not implemented: "public.t_kenyon.id"
第1行select * from public.t_kenyon.id limit 1;

postgres=# select * from public.t_kenyon.22 limit 1;
ERROR:  syntax error at or near ".22"
第1行select * from public.t_kenyon.22 limit 1;
                                  ^
postgres=# select * from public.t_kenyon.x22 limit 1;
ERROR:  cross-database references are not implemented: "public.t_kenyon.x22"
第1行select * from public.t_kenyon.x22 limit 1;
第二种场景里面还有多种方式会触发,比如最开始提到的comment,insert...select...也有可能,第二种场景里有一个很有意思的发现,当最后一个实心点后面跟的是数字时并不报跨库错误,而是语法错误。

扩展: 1.schema是 postgresql里一个相对特别的数据库对象,可以简单将理解成是一个容器,类似oracle的一个namespace,但是更灵活,可对schema授相应的用户权限来控制; 2.除此之外,可以将DB中的数据按相应的功能做水平切分,存放到不同的schema里面; 3.不同的用户可以设置不同的默认schema,可参考search_path这个变量; 4.目前版本JDBC连接postgresql时需要先连接上来,然后执行set search_path to xxx,不然可能取不到特定schema下的数据。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读