【问题标题】:PostgreSQL SET config from FUNCTION args来自 FUNCTION args 的 PostgreSQL SET 配置
【发布时间】:2020-05-17 21:20:19
【问题描述】:

是否可以使用 SQL FUNCTION 中的 arg 来设置配置值?

我正在尝试做这样的事情,但它在 PostgreSQL 10.12 上不起作用(配置的值必须是数字,而不是 arg):

CREATE OR REPLACE FUNCTION test(query text, threshold real)
RETURNS TABLE(
    t text,
    score real
  )
AS
'SELECT t, word_similarity(query, t) as score
 FROM test_trgm
 WHERE query <% t 
 ORDER BY score DESC'
language 'sql'
SET pg_trgm.word_similarity_threshold TO threshold;

这样做的目的是提高函数的性能,而不是使用类似的东西:

WHERE query <% t AND word_similarity(query, t) > threshold

提前致谢!

【问题讨论】:

    标签: sql postgresql trigram


    【解决方案1】:

    你可以在函数中使用SET_CONFIG()

    CREATE OR REPLACE FUNCTION testsim(query text, threshold real)
    RETURNS TABLE(
        t text,
        score real
      )
    AS
    $$
    
    select set_config('pg_trgm.word_similarity_threshold', threshold::text, true);
    
    SELECT t, word_similarity(query, t) as score
     FROM test_trgm
     WHERE query <% t 
     ORDER BY score DESC
     $$
    language 'sql'
    ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多