在 PostgreSql 中查找包含字符串的第一个数组值

Finding first array value that contains a string in PostgreSql

SELECT array_position(ARRAY, 'fillfactor=%') as position
FROM table;

我希望能够使用类似于上面的代码。我不知道填充因子是什么,所以我无法进行精确的字符串匹配,而是想获取以该语句开头的第一个值。请注意,此示例假定 ARRAY 是一个 text[] 列。有人可以帮忙吗?

没有像 array_regex_position 或类似的内置函数可以执行此操作。您将需要取消嵌套数组并对结果元素使用模式匹配运算符或函数。假设您正在查询 pg_class.reloptions,这应该给您大致的想法(这是在 10.5 上,我现在没有任何 9.X 服务器 运行):

testdb=# create table t_with_fillfactor(asdf text) with (fillfactor=50);
CREATE TABLE
testdb=# select relname,
    substr(reloption, length('fillfactor=')+1)::integer as fill_factor
from pg_class, unnest(reloptions) as reloption
where relname='t_with_fillfactor'
  and reloption ~ 'fillfactor=.*';
      relname      | fill_factor 
-------------------+-------------
 t_with_fillfactor |          50
(1 row)