在Postgres中使用数组数据类型时,我遇到了需要提取维度和数据类型的情况.

dellstore_clear=# \d+ sal_emp
                                              Table "public.sal_emp"
     Column     |   Type    | Collation | Nullable | Default | Storage  | Compression | Stats target | Description
----------------+-----------+-----------+----------+---------+----------+-------------+--------------+-------------
 name           | text      |           |          |         | extended |             |              |
 pay_by_quarter | integer[] |           |          |         | extended |             |              |
 schedule       | text[]    |           |          |         | extended |             |              |
Access method: heap

dellstore_clear=#

可以使用以下命令检索列的维度

dellstore_clear=# select array_ndims(pay_by_quarter) as pay_by_quarter_dim, array_ndims(schedule) as schedule_dim from sal_emp limit 1 ;
 pay_by_quarter_dim | schedule_dim
--------------------+--------------
                  1 |            2
(1 row)

dellstore_clear=#

我试图在从INFORMATION_SCHEMA.COLUMNS中检索信息时将这两者结合起来,如下所示,

select column_name, 
case 
    when data_type='ARRAY' then udt_name::regtype::text
    else data_type
end as column_type,
case
    when data_type='ARRAY' then array_ndims(column_name)
    else 'NA'
end as array_dimension

from INFORMATION_SCHEMA.COLUMNS where TABLE_CATALOG LIKE 'dellstore_clear' and TABLE_SCHEMA like 'public' and TABLE_NAME like 'sal_emp' order by ordinal_position;

但是,这给出了如下错误,

ERROR:  function array_ndims(information_schema.sql_identifier) does not exist
LINE 7:     when data_type='ARRAY' then array_ndims(column_name)
                                        ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
dellstore_clear=#
dellstore_clear=#

如何将检索到的列名称值传递给同一查询中的后续函数?

推荐答案

下面演示了使用系统目录pg_attribute来检索每列的数组维数:

DROP TABLE IF EXISTS dims_demo;

CREATE TABLE IF NOT EXISTS dims_demo (
  id INTEGER GENERATED always AS IDENTITY PRIMARY KEY,
  a1 INTEGER[],
  a2 INTEGER[][]
);

SELECT column_name,
       CASE
         WHEN data_type = 'ARRAY' THEN udt_name::regtype::text
         ELSE data_type
       END AS column_type,
       CASE
         WHEN data_type = 'ARRAY' THEN a.attndims
       END AS array_dimension
  FROM information_schema.columns c
  JOIN pg_attribute a
    ON a.attrelid = (FORMAT($$%I.%I$$, c.table_schema, c.table_name))::regclass
       AND a.attname = c.column_name
  WHERE c.table_schema = CURRENT_SCHEMA()
    AND c.table_name = 'dims_demo'
  ORDER BY c.ordinal_position;

运行时,会产生以下输出:

column_name column_type array_dimension
id integer [null]
a1 integer[] 1
a2 integer[] 2

返回的维度数基于表定义.PostgreSQL不强制使用the declared个维度,因此无论列类型如何,数组列都可以包含任意数量的维度.

Postgresql相关问答推荐

使用Spring data jpa和CriteriaQuery在jsonb列中搜索操作

无法在timscaleDb中创建Hypertable

列索引8上的扫描错误,名称已复制:不支持扫描,正在存储驱动程序.值类型[]uint8到类型*[]*bool

如何在生成的列的表达式中使用TIMESTAMP WITH时区列?

INSERT 语句返回策略违规(USING 表达式)

空表上的 Postgres 意外行为

GORM 不会创建 many2many 关联

PostgreSQL SELECT 结果具有不同的 id,它更喜欢来自另一个表的特定值

无法从在 wsl 2 上运行的服务之一连接到在 wsl 2 上的容器中运行的 postgres 数据库

如何向文本字段添加长度约束

将 PostgreSQL 配置为仅适用于 LOCALHOST 或指定的 ip + 端口

Rails 4查询由单个属性唯一

从 PostgreSQL 中的字段中提取数字

仅在存在时重命名列

具有 DEFAULT NULL 的 MySQL 列 ?

避免通过 JPA 将null值插入数据库表

Postgis / Geodjango:无法确定数据库的 PostGIS 版本

使用 Spring、Hibernate 和 C3P0 管理多租户 Web 应用程序中的连接池

如何获取一个月的天数?

postgresql NOT ILIKE 子句不包含空字符串值