Skip to content

Instantly share code, notes, and snippets.

@saerdnaer
Last active December 12, 2022 22:52
Show Gist options
  • Save saerdnaer/a38f57bf50861f69676a81d181ec0f3f to your computer and use it in GitHub Desktop.
Save saerdnaer/a38f57bf50861f69676a81d181ec0f3f to your computer and use it in GitHub Desktop.
CREATE INDEX "items_alternate_ids_idx" ON "items" USING GIN ("alternate_ids");
CREATE OR REPLACE FUNCTION items_by_ids(ids text[], is_published boolean DEFAULT NULL)
RETURNS SETOF items
AS $$
SELECT items.*
FROM unnest($1) WITH ORDINALITY t(value, ord), items
WHERE (
items.core_id = t.value
OR items.id = CASE WHEN isnumeric(t.value) THEN t.value::integer ELSE null END
OR items.alternate_ids @> ARRAY[t.value]
)
AND ($2 IS NULL OR items.is_published = $2)
LIMIT 500
$$
LANGUAGE sql
STABLE;
COMMENT ON FUNCTION items_by_ids(text[], boolean) IS E'@simpleCollections both';
@saerdnaer
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment