Skip to content

Instantly share code, notes, and snippets.

@bagus2x
Created January 25, 2023 20:57
Show Gist options
  • Save bagus2x/d4cd0d912543c6845c8177ff26aeb003 to your computer and use it in GitHub Desktop.
Save bagus2x/d4cd0d912543c6845c8177ff26aeb003 to your computer and use it in GitHub Desktop.
a postgres function to generate materialized path (ltree)
create or replace function create_path(parent_id_param bigint) returns ltree
language plpgsql
as
$$
declare parent_path ltree;
declare current_id BIGINT;
BEGIN
SELECT currval('"comment_id_seq"') into current_id;
IF parent_id_param is NOT NULL THEN
SELECT path into parent_path FROM comment WHERE id = parent_id_param;
return parent_path || current_id::text::ltree;
end if;
return current_id::text::ltree;
end;
$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment