4

Postgres conveniently allows :

SELECT * FROM foo WHERE bar ~ ANY (?)

Where ? is an array of any 1+ regex patterns for column "bar" to match.

How can I elegantly do the opposite - pick out rows where 1+ columns match one regex ?

SELECT * FROM foo WHERE ANY (bar, baz, qux) ~ ?

I pored over Row and Array Comparisons, and Functions and Operators, and more, but docs are light on examples.
Feels like I've got all the pieces, but can't connect 'em.

"row_constructor operator row_constructor" seems closer to what I need, but ~ is not available :

Row constructor comparisons are allowed when the operator is =, <>, <, <=, > or >=.

Besides which, I'd have to somehow explode 1 bound regex pattern into a row on the right-hand side.
Postgres complains that the right-hand side must be an array, when I've tried.

It's not clear to me whether Composite Type Comparisons would support "~" :

Composite type comparisons are allowed when the operator is =, <>, <, <=, > or >=, or has semantics similar to one of these.

Besides which, I'm even more lost as to how I'd construct multiple columns into a record on the left side, and 1 regex pattern into a record on the right of the operator "~".

This is an approximation of what I want in behaviour :

SELECT * FROM foo WHERE CONCAT(bar::text, baz::text, qux::text) ~ ?

but not ideal for reasons you can guess (and requires special handling/escaping depending on the regex pattern).

asked Mar 30, 2022 at 19:50
1
  • 1
    Define your own operator with ordering of the arguments reversed from ~, then use it with ANY. See dba.stackexchange.com/questions/228235/… for a similar example. Commented Mar 31, 2022 at 3:15

1 Answer 1

2

I believe the only way to do this is using an EXISTS

SELECT *
FROM foo
WHERE EXISTS(SELECT 1
 FROM (VALUES(bar), (baz), (qux)) v(Val)
 WHERE v.Val ~ 'ab.'
);

db<>fiddle

This may be more or less verbose and/or performant than just writing each comparison individually.

answered Mar 30, 2022 at 22:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.