CHECK constraint for array column to verify length > 0 I'm playing with postgres table validation rules and trying to set a CHECK constraint for an array column. An idea is to allow only arrays with length > 0.

Here is how I want to implement it:

create table words_table (
  id serial primary key, 
  words varchar(20)[] CHECK (array_length(words, 1) &gt; 0)
);


But looks like it doesn't work. O_o

insert into words_table (words) values ('{}');
//INSERT 0 1


How to implement such a constraint?
The crucial point you may not be aware of, quoting the manual:


  It should be noted that a check constraint is satisfied if the check
  expression evaluates to true or the null value.


Bold emphasis mine.

Using cardinality() fixes this case like a_horse explains. 

Explicitly disallowing empty arrays would do the same, cheaper:

CHECK (words &lt;&gt; '{}')


Both solutions still allow that words itself is NULL. You might want to add a NOT NULL constraint.

Either way, be aware that NULL always passes CHECK constraints. The general solution to disallow NULL for expressions:

CHECK (&lt;expression&gt; IS TRUE)