Understanding VARHDRSZ

From: "Redefined Horizons" <redefined(dot)horizons(at)gmail(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Understanding VARHDRSZ
Date: 2006-07-26 22:55:51
Message-ID: e24752a10607261555s188e982ct16eec10f188cbb9b@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Shoaib Mir 2006-07-26 23:08:40 Re: Database corruption with Postgre 7.4.2 on FreeBSD 6.1?
Previous Message aurora 2006-07-26 22:27:56 Database corruption with Postgre 7.4.2 on FreeBSD 6.1?