Created
January 25, 2023 20:57
-
-
Save bagus2x/d4cd0d912543c6845c8177ff26aeb003 to your computer and use it in GitHub Desktop.
a postgres function to generate materialized path (ltree)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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