lil
lil

Reputation: 2657

PostgreSQL Array Structure

What's the layout of postgres array stored in memory? How can I get the real data? For example, for array[0.1, 0.2, 0.3]::float8[], is the real data (0.1, 0.2, 0.3) stored like a standard c array? Could I use memcpy to copy an existing array? Does the pointer we get use ARR_DATA_PTR refer to the real data?

Upvotes: 1

Views: 733

Answers (1)

Pavel Stehule
Pavel Stehule

Reputation: 45900

PostgreSQL uses a mutable C structure - so first n bytes contains a fixed length data and next bytes are used for data. First four bytes holds length, follow number of dimmensions, dataoffset and identification of element's data type - next data should be a bitmap that holds NULLs and after this bitmaps data are serialised.

PostgreSQL array is not compatible with C arrays - resp. in few cases C array is part of PostgreSQL array. ARR_DATA_PTR can or must not to refer to real data. Depends on current state - data should be toasted, detoasted, ...

People usually use a macros and supporting functions when work with pg arrays. There are ways for unpacking to C arrays or iteration over pg array.

Upvotes: 5

Related Questions