使用Postgres 10时,UPDATE中不允许设置返回函数

时间:2017-10-09 11:57:42

标签: postgresql postgresql-10

我们有一个旧的Flyway数据库更新

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

...从9.2到9.6的任何Postgres版本运行良好,但最新的Postgres 10失败。即使在没有任何JDBC的情况下直接运行也会发生。

ERROR: set-returning functions are not allowed in UPDATE

从版本10发行说明中我没有注意到是否存在向后不兼容性?有解决方法吗?

2 个答案:

答案 0 :(得分:2)

我遇到了一个更普遍的问题,我需要从正则表达式中进行第二场比赛。

解决方案是嵌套的子选择

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

或修改正则表达式以返回一组并应用@LaurenzAlbe的答案:

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

在某些情况下,修改正则表达式并不理想。

原始格式为

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

junkwhat_i_want是相当复杂的rexex片段。

答案 1 :(得分:1)

这是未经测试的,但应该适用于所有PostgreSQL版本:

UPDATE plays SET album = substring (album FROM '^6,(?:(.+),)?tv\d+');