Обсуждение: Understanding VARHDRSZ

Поиск
Список
Период
Сортировка

Understanding VARHDRSZ

От
"Redefined Horizons"
Дата:
Page 524 of the PostgreSQL 8.1 Manual, PDF Edition, has a code snippit
that makes use of the VARHDRSZ macro. I'm trying to understand the
purpose of this macro.

Here is the code from the manual:

#include "postgres.h"
...
char buffer[40]; /* our source data */
...
text *destination = (text *) palloc(VARHDRSZ + 40);
destination->length = VARHDRSZ + 40;
memcpy(destination->data, buffer, 40);
...

The manual also says, just below the code snippit:

"VARHDRSZ is the same as sizeof(int4), but it's considered good style
to use the macro VARHDRSZ to refer to the size of the overhead for a
variable-length type."

Does this mean that we are simply allocating 44 bytes for the data
type with these calls to the VARHDRSX macro?

Previous to the code snippit the manual says this:

"All variable-length types must begin with a length field of exactly 4
bytes, and all data to be stored within that type must be located in
the memory immediately following that length field."

If this is the case, why even have the VARHDRSZ macro? Why not just
add 4 bytes to the length of the data when you use palloc?

I know I could just take this on faith, but I'm trying to understand
the underlying concepts of what is being done in this code. Is the
macro used becuase of different operating system implementations of
the C programming language, or is the Macro used to perserve backward
compatibility if the "length" indicator is increased to more than 4
bytes in the future?

Thanks,

Scott Huey

Re: Understanding VARHDRSZ

От
Tom Lane
Дата:
"Redefined Horizons" <redefined.horizons@gmail.com> writes:
> Page 524 of the PostgreSQL 8.1 Manual, PDF Edition, has a code snippit
> that makes use of the VARHDRSZ macro. I'm trying to understand the
> purpose of this macro.

It's just a symbol for the size of the length word at the start of the
stored value.

> If this is the case, why even have the VARHDRSZ macro?

Someday, we might want to change the size of the length word (eg,
move to int8, when no one uses mere 32-bit machines anymore).  That
will be a painful undertaking in any case, but everyplace that codes
the overhead space as "VARHDRSZ" rather than "4" will be one less
place to fix.

            regards, tom lane