Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set-returning functions are not allowed in UPDATE when using Postgres 10

We have an old Flyway database update

UPDATE plays SET album = (regexp_matches(album, '^6,(?:(.+),)?tv\d+'))[1]

...that runs fine with any Postgres version from 9.2 to 9.6 but fails with latest Postgres 10. Happens even when ran directly without any JDBC.

ERROR: set-returning functions are not allowed in UPDATE

Is there a backwards incompatibility I didn't notice from version 10 release notes? Is there a workaround?

like image 618
Anze Rehar Avatar asked Oct 09 '17 11:10

Anze Rehar


2 Answers

This is untested, but should work in all PostgreSQL versions:

UPDATE plays SET album = substring (album FROM '^6,(?:(.+),)?tv\d+');
like image 128
Laurenz Albe Avatar answered Oct 21 '22 23:10

Laurenz Albe


I had a more general problem where I needed the second match from a regex.

The solution was a nested subselect

SET my_column = (SELECT a.matches[2] from 
    (SELECT regexp_matches(my_column, '^(junk)?(what_i_want)$') matches) a)

Or modify the regex to return one group and apply @LaurenzAlbe 's answer:

SET my_column = substring (my_column FROM '^junk?(what_i_want)$')

There may be cases where modifying the regex is not ideal.

The original was of the form

SET my_column = regexp_matches(my_column, '^(junk)?(what_i_want)$')[2]

Where junk and what_i_want were fairly complex rexex fragments.

like image 35
Scott Carey Avatar answered Oct 21 '22 23:10

Scott Carey