How to find out the array dimensions of functions in postgres?

huangapple go评论62阅读模式
英文:

How to find out the array dimensions of functions in postgres?

问题

I'll only translate the code part for you:

我可以从information_schema.parameters(或pg_proc)中找到有关参数的信息,但是我无法找出如何找到数组参数的维度?

例如:

CREATE OR REPLACE FUNCTION pg_temp.one_dim(arr TEXT[]) RETURNS TEXT[]
LANGUAGE sql
AS $$
    SELECT arr;
$$;

CREATE OR REPLACE FUNCTION pg_temp.two_dim(arr TEXT[][]) RETURNS TEXT[][]
LANGUAGE sql
AS $$
    SELECT arr;
$$;

SELECT *
FROM information_schema.parameters p
WHERE p.parameter_name = 'arr';

返回:

"specific_catalog","specific_schema","specific_name","ordinal_position","parameter_mode","is_result","as_locator","parameter_name","data_type","character_maximum_length","character_octet_length","character_set_catalog","character_set_schema","character_set_name","collation_catalog","collation_schema","collation_name","numeric_precision","numeric_precision_radix","numeric_scale","datetime_precision","interval_type","interval_precision","udt_catalog","udt_schema","udt_name","scope_catalog","scope_schema","scope_name","maximum_cardinality","dtd_identifier","parameter_default"
"postgres","pg_temp_10","one_dim_119648",1,"IN","NO","NO","arr","ARRAY",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,"postgres","pg_catalog","_text",NULL,NULL,NULL,NULL,"1",NULL
"postgres","pg_temp_10","two_dim_119649",1,"IN","NO","NO","arr","ARRAY",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,"postgres","pg_catalog","_text",NULL,NULL,NULL,NULL,"1",NULL
英文:

I can find information about parameters from the information_schema.parameters (or pg_proc), but I can't work out how to find the dimensions of an array parameter?

For example:

CREATE OR REPLACE FUNCTION pg_temp.one_dim(arr TEXT[]) RETURNS TEXT[]
LANGUAGE sql
AS $$
    SELECT arr;
$$;

CREATE OR REPLACE FUNCTION pg_temp.two_dim(arr TEXT[][]) RETURNS TEXT[][]
LANGUAGE sql
AS $$
    SELECT arr;
$$;

SELECT *
FROM information_schema.parameters p
WHERE p.parameter_name = 'arr';

Returns:

"specific_catalog","specific_schema","specific_name","ordinal_position","parameter_mode","is_result","as_locator","parameter_name","data_type","character_maximum_length","character_octet_length","character_set_catalog","character_set_schema","character_set_name","collation_catalog","collation_schema","collation_name","numeric_precision","numeric_precision_radix","numeric_scale","datetime_precision","interval_type","interval_precision","udt_catalog","udt_schema","udt_name","scope_catalog","scope_schema","scope_name","maximum_cardinality","dtd_identifier","parameter_default"
"postgres","pg_temp_10","one_dim_119648",1,"IN","NO","NO","arr","ARRAY",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,"postgres","pg_catalog","_text",NULL,NULL,NULL,NULL,"1",NULL
"postgres","pg_temp_10","two_dim_119649",1,"IN","NO","NO","arr","ARRAY",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,"postgres","pg_catalog","_text",NULL,NULL,NULL,NULL,"1",NULL

答案1

得分: 1

你无法改变它。维度是值的一部分,而不是类型。PostgreSQL 数组是多维的。

=> 选择 pg_typeof(NULL::text[]);
┌───────────┐
│ pg_typeof │
├───────────┤
│ text[]    │
└───────────┘
(1 行)

=> 选择 pg_typeof(NULL::text[][]);
┌───────────┐
│ pg_typeof │
├───────────┤
│ text[]    │
└───────────┘
(1 行)
英文:

You can't. The dimensions are part of the value, not the type. PostgreSQL arrays are multi-dimensional.

=> SELECT pg_typeof(NULL::text[]);
┌───────────┐
│ pg_typeof │
├───────────┤
│ text[]    │
└───────────┘
(1 row)

=> SELECT pg_typeof(NULL::text[][]);
┌───────────┐
│ pg_typeof │
├───────────┤
│ text[]    │
└───────────┘
(1 row)

huangapple
  • 本文由 发表于 2023年4月11日 16:16:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定