gpt4 book ai didi

sql - 来自 FUNCTION args 的 PostgreSQL SET 配置

转载 作者:行者123 更新时间:2023-12-04 09:59:35 29 4
gpt4 key购买 nike

是否可以使用 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

提前致谢!

最佳答案

您可以使用 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'
;

关于sql - 来自 FUNCTION args 的 PostgreSQL SET 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61858992/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com