Re: What's the best way in postgres to use ANY() with LIKE '%'?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Ryan Murphy <ryanfmurphy(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: What's the best way in postgres to use ANY() with LIKE '%'?
Date: 2017-09-05 03:09:15
Message-ID: 30239.1504580955@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Ryan Murphy <ryanfmurphy(at)gmail(dot)com> writes:
> But is there any way to do:
> select * from post
> where any(tags) LIKE 'music%';
> ??
> This doesn't work because ANY is only allowed on the right.

Yeah. The traditional answer is "make yourself a reverse LIKE
operator, one that takes the pattern on the left".

You can brute-force this in a couple of lines with a SQL function
and a CREATE OPERATOR command. But it won't be tremendously
efficient like that. If performance is critical it'd be worth
writing the shim function in C.

I'm not sure why we've never got round to providing such a thing
in core ... probably lack of consensus on what to name the reverse
operator. You'd need to support regex cases as well, so there's
more than one operator name to come up with.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ryan Murphy 2017-09-05 03:26:02 Re: What's the best way in postgres to use ANY() with LIKE '%'?
Previous Message reugen1984 2017-09-05 02:37:23 hidden maintenance_work_mem limitations of a Windows build