1

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.

Paul White
95.4k30 gold badges440 silver badges689 bronze badges
asked May 15, 2021 at 20:58
2
  • 1
    You're going to have to expand on your use case - recursive CTEs are all about accessing nested levels... perhaps a small fiddle (dbfiddle.uk) with some sample data and your desired output and maybe expand on your train of thought using the sample data as your exemplar? Also, please put the sample material into the question also... Commented May 16, 2021 at 9:50
  • For alternatives, you have to make your use case clear first. Start a new question for that. Commented May 25, 2021 at 19:10

1 Answer 1

4

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?)

answered May 25, 2021 at 19:07
1
  • 1
    NULL value in the field could terminate the recursion Commented Feb 1, 2023 at 8:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.