问题
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?
回答1:
This is untested, but should work in all PostgreSQL versions:
UPDATE plays SET album = substring (album FROM '^6,(?:(.+),)?tv\d+');
回答2:
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.
来源:https://stackoverflow.com/questions/46645905/set-returning-functions-are-not-allowed-in-update-when-using-postgres-10