Sam Patterson <katoriasdev@gmail.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