示例:https ://dbfiddle.uk/bCSwVpd9
数据库:
CREATE TABLE entities (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY
);
CREATE TYPE entity_id AS (
id bigint
);
功能:
CREATE FUNCTION get_entities (
pagination_limit bigint DEFAULT 25,
pagination_offset bigint DEFAULT 0,
entity_ids entity_id DEFAULT NULL
)
RETURNS TABLE (
id bigint
)
LANGUAGE SQL
AS $BODY$
WITH input_entities AS (
SELECT
id
FROM
entities
WHERE
-- filter by id list if provided
entity_ids IS NULL OR id IN (
SELECT
id
FROM
entity_ids
)
ORDER BY
id ASC
LIMIT pagination_limit
OFFSET pagination_offset
)
SELECT
id
FROM
input_entities
ORDER BY
id
$BODY$;
关键是我想编写一个分页多选函数,它可以从分页信息和一组 ID 中工作。它上面的函数的问题崩溃了:
ERROR: relation "entity_ids" does not exist
LINE 22: entity_ids
对这个问题也有类似的反应:first,second。但是,它们围绕作为标识符字符串的参数,而不是复合记录类型和 use plpgsql
,这可能不重要,也可能不重要。