Hi.

Let me ask you somthing about the magic of palloc()
- at least for me, it looks like magic, and variable-length user-defined
data types.
Hopefully, there is anyone who can help me... I am really struggling to
grasp PostgreSQL.

According to chapter 4 of the Programmer's Guide, user-defined data types
can
have one of these internal formats.

- pass by value, fixed-length
- pass by reference, fixed-length
- pass by reference, variable-length

I am trying to define an user-defined data type corresponding to the third
case,
but let me use the example in the manual.
In the manual, I found this example:

typedef struct {
   int4 length;
   char data[1];
} text;


1) then, my first question is about the example for coding as follows:

char buffer[40];
....
text *destination = (text *) palloc(VARHDRSZ + 40);
....

I cannot understand this mechanism... it looks like magic to me..
If it is like the following, I can see it:

text *destination = (text *) palloc(sizeof(text));
destination->data = (char *) palloc(40);

In this case, there still remains a question, how can I allocate a bunch of
memory
to "char data[1]" - it is an array data type, not pointer...

I tried to see the source code, and found it just call MemoryContextAlloc(),
but
MemoryContextAlloc() just has an empty function body.

Is there anyone who can tell me the magic of the palloc()?
And which header file do I need to include for palloc()?

2) And my sencond question is how to create such variable-lenth data type.

In Chapter 5 of the Programmer's Guide, the example for a fixed-length
user-defined
data types, 'Complex', is:

....
(let's assume input and output function for a new user-defined data type
have been
created.)

CREATE TYPE complex (
   internallength = 16,
   input = complex_in,
   output = complex_out
);

But, I have no idea what I need to set to 'internallength' for
variable-length data types.


3) My last curiosity is about linking problem.

If I want to make a stand-alone program which call internal functions,
especially palloc(),
to which library I need to link my program?
I started this attempt to find an answer for my first question, but now I am
very
curious about it, becuase I realize that I cannot use the client libraries
in ..../pgsql/lib
such as libpgeasy and libpg, instead I suppose I need to link my stand-alone
program
to server libraries. But the problem is server libraries are shared
libraries and I have
no idea about the mechanism.
Which shared library has the reference to MemoryContextAlloc()?
And is there anything on which I need to take care to link my stand-alone
program to such
shared library? Is it perhaps impossible?

Anyway, thank you for reading my long e-mail.
Cheers.

 From someone who is trying to love PostgreSQL.




Wed, 14 Nov 2001 20:23:57 -0000
"Seung Hyun Jeong" <jeongs@cs.man.ac.uk>
about the magic(?) of palloc() and variable-length user-defined data
	type
"Seung Hyun Jeong" <jeongs@cs.man.ac.uk> writes:
> In the manual, I found this example:
> typedef struct {
>    int4 length;
>    char data[1];
> } text;

> 1) then, my first question is about the example for coding as follows:
> char buffer[40];
> ....
> text *destination = (text *) palloc(VARHDRSZ + 40);
> ....

> I cannot understand this mechanism... it looks like magic to me.

No, it's simply relying on the fact that C doesn't check array
subscripts.  Given the stated declaration for struct text, we can
access data[0], or we can (try to) access data[1], data[2], etc etc.
These latter array elements are off the end of the declared structure;
but if we've allocated sufficient memory, it'll work fine.

This is a very standard C programming trick to get around the language's
lack of explicit variable-sized arrays.  If you haven't seen it before,
you may need to spend more time with an introductory C textbook before
you start trying to make sense of the Postgres internals...

> I tried to see the source code, and found it just call MemoryContextAlloc(),
> but MemoryContextAlloc() just has an empty function body.

Not hardly.  Where are you looking?  MemoryContextAlloc is in
src/backend/utils/mmgr/mcxt.c, and the function pointer it invokes
generally points at AllocSetAlloc in src/backend/utils/mmgr/aset.c.

> But, I have no idea what I need to set to 'internallength' for
> variable-length data types.

You say "variable".

> If I want to make a stand-alone program which call internal functions,
> especially palloc(),
> to which library I need to link my program?

You don't.  There is no library built for the backend, only the server
executable.  It's pretty unclear to me what a standalone program would
want with these functions anyway ... they are of no value except to
something that plans to run inside a server backend process.

			regards, tom lane

Wed, 14 Nov 2001 19:26:25 -0500
Tom Lane <tgl@sss.pgh.pa.us>
Re: about the magic(?) of palloc() and variable-length user-defined
	data type