是否可以重写此plpgsql过程以避免两次调用 json.get_user_notifications ?
CREATE OR REPLACE FUNCTION json.rowset_notifications(arg1 integer, arg2 integer, arg3 integer)
RETURNS SETOF json.notification AS
$BODY$
DECLARE
pmax_limit boolean;
BEGIN
pmax_limit := (SELECT COUNT(*) > 200 FROM json.get_user_notifications($1, $2, $3));
IF pmax_limit THEN
RETURN QUERY SELECT n.* FROM json.get_compressed_user_notifications($1, $2, $3) n;
ELSE
RETURN QUERY SELECT n.* FROM json.get_user_notifications($1, $2, $3) n;
END IF;
END
$BODY$
LANGUAGE plpgsql STABLE
COST 100
ROWS 200;
类似的东西(当然它不起作用):
WITH
notifications AS (SELECT n.* FROM json.get_user_notifications($1, $2, $3) n LIMIT 201)
SELECT CASE
WHEN COUNT(*) > 200 THEN
RETURN QUERY SELECT n.* FROM json.get_compressed_user_notifications($1, $2, $3) n;
ELSE
RETURN QUERY SELECT n.* FROM notifications n;
END
FROM notifications;
答案 0 :(得分:0)
当然,像这样运行其他查询
SELECT n.*
FROM json.get_user_notifications($1, $2, $3) AS n
WHERE ( SELECT count(*)>200 FROM json.get_user_notifications )
你可能必须明确。如果返回,则可以返回json.notification
的结果集。如果它没有返回,那么你知道你需要返回压缩的结果集。
答案 1 :(得分:0)
你可能会寻找:
WITH notifications AS (
SELECT n.*
FROM json.get_user_notifications($1, $2, $3) n
LIMIT 201
)
SELECT *
FROM notifications
WHERE (SELECT COUNT(*) <= 200 FROM notifications)
UNION
SELECT *
FROM json.get_compressed_user_notifications($1, $2, $3)
WHERE (SELECT COUNT(*) > 200 FROM notifications)