【问题标题】:How to get array elements' "numeric precision", "numeric scale" and "datetime precision" meta data in PostgreSQL?如何在 PostgreSQL 中获取数组元素的“数值精度”、“数值比例”和“日期时间精度”元数据?
【发布时间】:2019-08-03 08:13:16
【问题描述】:

对于一个开源项目,我需要获取PostgreSQL数据库中数组的“数值精度”、“数值刻度”和“日期时间精度”。

我正在使用information_schema.columns 并将其加入information_schema.element_types 以获取有关列中数组元素的信息,但它没有精度等。

element_types page 声明,(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(),但在文档中找不到它们。

如何获得columnsfunction argumentsdomains 中数组元素的“数值精度”、“数值刻度”和“日期时间精度”?

【问题讨论】:

    标签: arrays postgresql precision meta information-schema


    【解决方案1】:

    您可以使用来自pg_attribute 的信息并使用format_type() 来获得可读的显示:

    select attname, format_type(atttypid, c.atttypmod)
    from pg_attribute c
    where attrelid = 'public.type_table'::regclass
    and attnum > 0;
    

    【讨论】:

    • 感谢您的回答。 format_type 以文本形式提供精度和比例,例如 character varying(20),需要进一步解析。如果可能,您能否建议一个不解析的“数值精度”、“数值刻度”和“日期时间精度”的查询?
    • @ozm:该信息以某种方式编码在 atttypmod 中,除了使用 format_type() 之外,我不知道如何提取它
    猜你喜欢
    • 2017-04-27
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    相关资源
    最近更新 更多