I am having a postgresql table of approx 1500 rows and the model is “Modified Preorder Tree Traversal” structure based on Left and Right field. and i want to insert many rows in between but i am not sure how to calculate the level, rightindex and leftindex columns values for multiple rows when inserting in between of rows.
I believe for a single row we can do achieve this by below code,
SELECT @myRight := rgt FROM tablename
UPDATE tablename SET rgt = rgt + 2 WHERE rgt > @myRight;
UPDATE tablename SET lft = lft + 2 WHERE lft > @myRight;
INSERT INTO tablename(name, lft, rgt)
VALUES('GAME CONSOLES', @myRight + 1, @myRight + 2);
But how can we do this for multiple updates for a large table.
Please let me know if any further information is needed from my end.
An example of a procedure which adds new items from the
NewNodes
table to thetablename
Setup
The procedure adds new nodes after the node with the specified name at the same level.
db<>fiddle