对于一个开源项目,我需要获取PostgreSQL数据库中数组的“数字精度”,“数字比例”和“日期时间精度”。
我正在使用 information_schema.columns 并将其加入 information_schema.element_types 来获取有关列中数组元素的信息,但它没有精度等。
element_types 页面指出,(numeric_ precision、numeric_scale 和 datetime_ precision) 始终为 null,因为此信息不适用于 PostgreSQL 中的数组元素数据类型
我以为我误解了这个陈述,因为我可以在表中定义具有数字精度的数组列。例如:
CREATE TABLE public.type_table (
id SERIAL,
some_field NUMERIC(4,2) [],
CONSTRAINT type_table_key PRIMARY KEY(id)
)
我还看到一些函数
information_schema._pg_datetime_precision()
、information_schema._pg_numeric_precision()
和 information_schema._pg_numeric_scale()
,但在文档中找不到它们。
如何获得
columns
、function arguments
和 domains
中数组元素的“数字精度”、“数字比例”和“日期时间精度”?
pg_attribute
中的信息并使用 format_type()
获得可读的显示:
select attname, format_type(atttypid, c.atttypmod)
from pg_attribute c
where attrelid = 'public.type_table'::regclass
and attnum > 0;