我刚刚发现我可以将任何类型的值插入到 PostgreSQL (9.6) 类型的列中text:
drop table if exists d cascade;
create table d ( a text );
insert into d values ( 42 );
insert into d values ( true );
select a, pg_typeof( a ) from d;
a | pg_typeof
------+-----------
42 | text
true | text
(2 rows)
Run Code Online (Sandbox Code Playgroud)
这是故意的功能吗?我做错了什么吗?有没有设置可以避免这种情况?这是否违反了 RDBMS 应该是类型安全的假设?
我知道这text在 PostgreSQL 中就像一个包罗万象的东西,这通常很方便,因为您可以编写任意类型的字符串表示。但是有时您肯定希望确保只有字符串被插入到给定的列中,以排除隐式转换值。
我能做些什么来避免“随意”类型转换?
我正在寻找一种方法来消除 PostgreSQL 数组中的重复项,同时保留其元素的顺序。我目前拥有的是以下功能:
create function array_unique( anyarray )
returns anyarray immutable strict language sql as $$
select array( select distinct unnest( $1 ) ); $$;
create function array_unique_sorted( anyarray )
returns anyarray immutable strict language sql as $$
select array( select distinct unnest( $1 ) order by 1 ); $$;
/* ### TAINT there ought to be a simpler, declarative solution */
create function array_unique_stable( text[] )
returns text[] immutable strict parallel safe language plpgsql as $$
declare
R text[] …Run Code Online (Sandbox Code Playgroud)