Skip to content
Advertisement

How to delete a particular label from ltree in Postgres table?

How to delete a particular label from ltree in Postgres table? I have a table?

Test table:

I want to pass a userid to a query, to remove it from every path in the table. For example, if I pass 101, then 123.101.103 should update as 123.103.

Is it possible to do this directly? Or shall I update path using path replace functions?

I tried the following select query from PHP, but it returns the error below. The same query works properly in phpPgAdmin!?

Query:

Error:

Advertisement

Answer

Fix error

To use the data type ltree and associated functions you need the additional module ltree installed.

By default, additional modules are installed to the schema public. If your current setting for search_path does not include the schema public, ltree operators are not found. Your error messages hints in this direction:

To verify, schema-qualify the ltree operator @:

Does it work now?
If that’s the root of the problem, you can also set your search_path properly. Check with:

Set with:

The manual about search_path.

UPDATE path

If I read your question right and you want to remove a certain item from the path of all rows:

(Note that since path is type ltree, the operator ~ in the expression path ~ '*.101.*' is not a regular expression match operator, but a special ltree match operator, and the right term is taken to be of type lquery. It matches any label path containing the label 101.)

This would leave you with an empty path where you had just 101. You could put this into a trigger AFTER DELETE to purge a certain item from all rows.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement