Re: Varlena with recursive data structures?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Sam Patterson <katoriasdev(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Varlena with recursive data structures?
Date: 2019-01-16 22:22:33
Message-ID: 26970.1547677353@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Sam Patterson <katoriasdev(at)gmail(dot)com> writes:
> I presume the purpose for using this approach is because all the data in a
> varlena type has to be contiguous, and the moment you start using pointers
> this is no longer possible.

Yup.

> So my question is, given a structure that looks
> something like this,

> typedef struct Node
> {
> char *data;
> Node *left;
> Node *right;
> } Node;

> am I right in saying that I wouldn't be able to store that representation
> on-disk, but instead I'd have to transform it into some binary
> representation and back again when writing/reading respectively, are there
> any alternatives?

Yes, yes, no. Any elementary datatype has to be able to be flattened into
a "blob of bytes" to be stored on disk.

However, you might be able to avoid working with the flattened
representation all the time. There's an API for "expanded datums"
whereby functions can pass around an in-memory data structure that
doesn't have to look like a blob of bytes, and only flatten it out
when it's due to be stored somewhere. I'm not sure how much that'd
help you, but it's possibly worth looking at. See

src/include/utils/expandeddatum.h
src/backend/utils/adt/expandeddatum.c

for the basic APIs and

src/backend/utils/adt/array_expanded.c
src/backend/utils/adt/expandedrecord.c

for two examples of use.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message George Neuner 2019-01-17 03:15:25 Re: Varlena with recursive data structures?
Previous Message Sam Patterson 2019-01-16 22:08:35 Varlena with recursive data structures?