1

I found this very similar question but all of the answer started with a 'select' statement. I want to check whether a string is contained in a constant array with about 30 other strings. I could write a long x == a OR x == b OR... statement but I thought there might be a cleaner way.

So this doesn't work as a constraint check: SELECT language = ANY ('{"en", "pt", "es", "fr"}'::text[])

2
  • 1
    Just remove the select then it should work in a check constraint. Commented Jul 29, 2015 at 8:41
  • This works, thanks for that. But this ANY function seems to be too complex for postgres to automatically ignore for selects on partitioned tables. Commented Jul 29, 2015 at 9:10

1 Answer 1

1

Just removing the SELECT works:

CHECK(
    language = ANY ('{"en", "pt", "es", "fr"}'::text[])
)

But as a_horse_with_no_name pointed out:

Not using an array is even better, as this does not break the partitioning optimization.

CHECK(
    not( language in ('en', 'pt', 'es'))
)

Now SELECT * FROM myTable WHERE language='de'; will not even look at this table.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't see a reason why you need an array at all: Just use check (language in ('en', 'pt', 'es', 'fr'))
That is indeed much better. The Array actually broke the partitioning optimization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.