Understanding VARHDRSZ - Mailing list pgsql-general

From Redefined Horizons
Subject Understanding VARHDRSZ
Date
Msg-id e24752a10607261555s188e982ct16eec10f188cbb9b@mail.gmail.com
Whole thread Raw
Responses Re: Understanding VARHDRSZ  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
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

pgsql-general by date:

Previous
From: aurora
Date:
Subject: Database corruption with Postgre 7.4.2 on FreeBSD 6.1?
Next
From: "Shoaib Mir"
Date:
Subject: Re: Database corruption with Postgre 7.4.2 on FreeBSD 6.1?