在PostgreSQL 12中可以使用自定义域数组吗?

问题描述

我已将自定义tmoney声明为

create domain tmoney as decimal (13,4);

然后我在表声明中使用它的数组

create table test (
  id        int generated by default as identity primary key,volume    smallint[5] not null default '{0,0}',price     tmoney[5]   not null default '{0,0}'
);

insert into test(volume,price) 
values ('{1,10,50,100,250}','{10,9.75,9.5,9,8.75}');

在Postgresql 12中,没有解析异常,因为解析异常似乎以前已经存在(请参见Create array of custom domain postgres),但是,每当我尝试检索作为tmoney []插入的值时,都会发现DBCException。请注意,使用smallint []不会发生此错误

select * from test;

id|volume           |price                                        |
--|-----------------|---------------------------------------------|
 1|{1,250}|DBCException: Can't resolve data type _tmoney|

https://www.postgresql.org/docs/current/sql-createdomain.html上的文档仅指定了

tdata_type-域的基础数据类型。这可以包括数组说明符。

这与创建为的域一致

create domain tmoney as decimal (13,4)[];

create table test (
  id        int generated by default as identity primary key,price     tmoney  not null default '{0,8.75}');

select * from test;

id|volume           |price                   |
--|-----------------|------------------------|
 1|{1,250}|{10.0,9.0,8.75}|

但是,由于Postgresql 12解析器不会阻止在表声明中使用tmoney[5],所以我想知道是否有其他语法可以使我使用自定义域的第一个版本。

解决方法

使用域数组为introduced in v11

您的SQL语句可以与psql一起正常工作。

您必须使用可能不正确支持该客户端的其他客户端。 考虑使用该软件提交错误报告或增强请求。