今天群里一个哥们发了个错误信息:
1ERROR: cross-database references are not implemented: "public.test.id" 2 3********** 错误 ********** 4 5ERROR: cross-database references are not implemented: "public.test.id" 6SQL 状态: 0A000
看到贴出的SQL语句时就发现他的错误了
1comment on table public.test is '测试表'; 2comment on table public.test.id is '测试ID'; 3..
给字段加备注的时候应该是column,而不是table。修改完备注SQL就不会报错。
报这种错误,得需要提醒一下,postgresql的体系结构认为不同的数据库是独立的,所以并不支持跨库查询。而且执行一个查询时,postgresql首先会检查自身的dbname(一般连接的当前DB名,可不写),然后是schema(因为一个DB里可包含多个schema,默认连接是public),再最后才是数据库对象(table,view,function...)。
所以当出现cross-database错误时我们就可以认为postgres认为用户做了一次非规范的跨库操作了。
回过头来想一想,什么情况下会报这种错误呢,我整理了一下,有以下这么几种情况:
**测试环境:
**postgresql 9.1.2
1postgres=# \l 2 资料库列表 3 名称 | 拥有者 | 字元编码 | Collate | Ctype | 存取权限 4-----------+----------+----------+---------+-------+----------------------- 5 d_kenyon | postgres | UTF8 | C | C | 6 postgres | postgres | UTF8 | C | C | 7 template0 | postgres | UTF8 | C | C | =c/postgres + 8 | | | | | postgres=CTc/postgres 9 template1 | postgres | UTF8 | C | C | =c/postgres + 10 | | | | | postgres=CTc/postgres 11(4 行记录) 12 13postgres=# \d 14 关联列表 15 架构模式 | 名称 | 型别 | 拥有者 16----------+----------------+--------+---------- 17 public | pgweb | 资料表 | postgres 18 public | t_kenyon | 资料表 | postgres 19 public | tbl_order_item | 资料表 | postgres 20 public | tbl_orders | 资料表 | postgres 21 public | tbl_product | 资料表 | postgres 22(5 行记录) 23 24postgres=# select current_user; 25 current_user 26-------------- 27 postgres 28(1 行记录) 29 30postgres=# \c d_kenyon 31You are now connected to database "d_kenyon" as user "postgres". 32d_kenyon=# \d 33 关联列表 34 架构模式 | 名称 | 型别 | 拥有者 35----------+-------+--------+---------- 36 public | test | 资料表 | postgres 37 public | test2 | 资料表 | postgres 38(2 行记录)
1.真正地做了一次跨库查询
1d_kenyon=# select *from postgres.public.t_kenyon limit 1; 2ERROR: cross-database references are not implemented: "postgres.public.t_kenyon 3" 4第1行select *from postgres.public.t_kenyon limit 1; 5 ^ 6d_kenyon=#
2.没有做跨库查询,但是pg误认为你做了一次跨库操作
1postgres=# \d 2 关联列表 3 架构模式 | 名称 | 型别 | 拥有者 4----------+----------------+--------+---------- 5 public | pgweb | 资料表 | postgres 6 public | t_kenyon | 资料表 | postgres 7 public | tbl_order_item | 资料表 | postgres 8 public | tbl_orders | 资料表 | postgres 9 public | tbl_product | 资料表 | postgres 10(5 行记录) 11 12postgres=# \d t_kenyon 13 资料表 "public.t_kenyon" 14 栏位 | 型别 | 修饰词 15--------+---------+-------- 16 id | integer | 17 t_name | text | 18 19postgres=# select * from public.t_kenyon limit 1; 20 id | t_name 21----+-------- 22 1 | kenyon 23(1 行记录) 24 25postgres=# select * from public.t_kenyon.id limit 1; 26ERROR: cross-database references are not implemented: "public.t_kenyon.id" 27第1行select * from public.t_kenyon.id limit 1; 28 29postgres=# select * from public.t_kenyon.22 limit 1; 30ERROR: syntax error at or near ".22" 31第1行select * from public.t_kenyon.22 limit 1; 32 ^ 33postgres=# select * from public.t_kenyon.x22 limit 1; 34ERROR: cross-database references are not implemented: "public.t_kenyon.x22" 35第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下的数据。