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

廉价感情. 提交于 2020-03-02 06:16:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!