Define recursive composite types

All we need is an easy explanation of the problem, so here it is.

Is it possible to define recursive composite types in Postgres 13?

create type "t" as (
  "a" int,
  "b" t
);

I am aware that I can use a table with a parent-child relationship. The only disadvantage is that I will need to query each nested level instead of directly accessing the nested data.

I am also aware that I can use jsonb, but I am not sure if it can access the nested data directly without querying each nested level?

What are the other alternatives using PL/pgSQL only?

I need to access each nested level separately.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

No, "recursive" type definitions are not possible in Postgres.

You cannot reference a type in CREATE TYPE that is not registered, yet.
(What would terminate the recursion anyway?)

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply