Custom Base Type in C

Started by Toby Chavezalmost 18 years ago9 messagesgeneral
Jump to latest
#1Toby Chavez
odnamr@gmail.com

I have been trying to created a custom base type in C to use as a state
transition variable for some aggregate functions. By following the
documentation I haven't been able to do much but crash postgres :) I can
compile and run the complex example from the tutorial just fine so I guess
that means I am compiling correctly.

My custom type needs to have one biginteger and one text value... pretty
straight forward. I guess my first question is if there are any examples out
there that do something similar. I have looked extensively through the
contrib directory and can't find anything very helpful. If not, could
someone help me understand what my c struct would look like and what values
I need for INTERNALLENGTH, ALIGNMENT, STORAGE, etc in the CREATE TYPE
command?

Thanks a ton,

Toby

#2David Wilson
david.t.wilson@gmail.com
In reply to: Toby Chavez (#1)
Re: Custom Base Type in C

On Wed, May 7, 2008 at 7:06 PM, Toby Chavez <odnamr@gmail.com> wrote:

My custom type needs to have one biginteger and one text value... pretty
straight forward. I guess my first question is if there are any examples out
there that do something similar. I have looked extensively through the
contrib directory and can't find anything very helpful. If not, could
someone help me understand what my c struct would look like and what values
I need for INTERNALLENGTH, ALIGNMENT, STORAGE, etc in the CREATE TYPE
command?

Is there any particular reason why this needs to be done in C? Why not
just create the type normally as per:

CREATE TYPE my_transition_type AS (a bigint, b text);

And then create your aggregate function using that type?

I can't help you with the C stuff, but you may be making far more work
for yourself than you really need.

--
- David T. Wilson
david.t.wilson@gmail.com

#3Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Toby Chavez (#1)
Re: Custom Base Type in C

Toby Chavez escribi�:

My custom type needs to have one biginteger and one text value... pretty
straight forward. I guess my first question is if there are any examples out
there that do something similar. I have looked extensively through the
contrib directory and can't find anything very helpful. If not, could
someone help me understand what my c struct would look like and what values
I need for INTERNALLENGTH, ALIGNMENT, STORAGE, etc in the CREATE TYPE
command?

Probably what's biting you is that the type needs to be
pass-by-reference and varlena.

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

#4Dan "Heron" Myers
heron@xnapid.com
In reply to: Toby Chavez (#1)
Using a composite SQL type in C

I have a type defined in SQL:

CREATE TYPE text_info AS (str text, id integer);

I'd like to use this type as the aggregate type in an aggregate function:

CREATE FUNCTION foobar(text_info,integer,text,text) RETURNS text_info AS
'$libdir/plugins/mylib.dll', 'foobar' LANGUAGE C;

CREATE AGGREGATE foobar(integer,text,text) (SFUNC = foobar, STYPE =
text_info);

My problem is, how do I access this type in my function (in C), and how
do I create a new object of this type to return from the function? I
need to store both a text and an int, to avoid doing an extra query
around the result of this aggregate (to get the corresponding text
value), which is what I'm currently doing. Any ideas?

Thanks,
Dan

#5Toby Chavez
odnamr@gmail.com
In reply to: David Wilson (#2)
Re: Custom Base Type in C

I need to do the aggregate functions in c not pg/plsql so transition type
needs to be accessible in c. If there is a way to do that without explicitly
defining the type in c then that would be perfect.

Show quoted text

Is there any particular reason why this needs to be done in C? Why not
just create the type normally as per:

CREATE TYPE my_transition_type AS (a bigint, b text);

And then create your aggregate function using that type?

I can't help you with the C stuff, but you may be making far more work
for yourself than you really need.

--
- David T. Wilson
david.t.wilson@gmail.com

#6Toby Chavez
odnamr@gmail.com
In reply to: Alvaro Herrera (#3)
Re: Custom Base Type in C

If I make it pass-by-reference and varlena do I have to worry about TOASTing
and deTOASTing everying in my INPUT and OUTPUT functions?

On Wed, May 7, 2008 at 5:15 PM, Alvaro Herrera <alvherre@commandprompt.com>
wrote:

Show quoted text

Toby Chavez escribió:

My custom type needs to have one biginteger and one text value... pretty
straight forward. I guess my first question is if there are any examples

out

there that do something similar. I have looked extensively through the
contrib directory and can't find anything very helpful. If not, could
someone help me understand what my c struct would look like and what

values

I need for INTERNALLENGTH, ALIGNMENT, STORAGE, etc in the CREATE TYPE
command?

Probably what's biting you is that the type needs to be
pass-by-reference and varlena.

--
Alvaro Herrera
http://www.CommandPrompt.com/ <http://www.commandprompt.com/&gt;
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

#7Dimitri Fontaine
dimitri@2ndQuadrant.fr
In reply to: Toby Chavez (#1)
Re: Custom Base Type in C

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

Le 8 mai 08 à 01:06, Toby Chavez a écrit :

My custom type needs to have one biginteger and one text value...
pretty straight forward. I guess my first question is if there are
any examples out there that do something similar. I have looked
extensively through the contrib directory and can't find anything
very helpful.

You could have a look at the prefix module, which defines a varlena
prefix_range datatype, which is a C struct containing two char and a
char* elements.
http://pgfoundry.org/projects/prefix

But maybe you just don't need to define the type in C, this I can't say.

- --
dim
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Darwin)

iEYEARECAAYFAkgi7BsACgkQlBXRlnbh1bnqcgCfQhuzXrmRIc/k65w4Jb5mCHs6
OBgAn1h6g5eadNPetCBs59nnh5TGs+2Z
=F2OW
-----END PGP SIGNATURE-----

#8Toby Chavez
odnamr@gmail.com
In reply to: Dan "Heron" Myers (#4)
Re: Using a composite SQL type in C

Check the documentation about using composite-type arguments in C
http://www.postgresql.org/docs/8.2/static/xfunc-c.html#AEN37402

You can use GetAttributeByNum() or GetAttributeByName() to get each value
from your custom type. You will need to get the HeapTupleHeader first by
calling PG_GETARG_HEAPTUPLEHEADER(). A quick google search brought up this
tutorial that might help http://linuxgazette.net/142/peterson.html

Cheers

Show quoted text

My problem is, how do I access this type in my function (in C), and how do
I create a new object of this type to return from the function? I need to
store both a text and an int, to avoid doing an extra query around the
result of this aggregate (to get the corresponding text value), which is
what I'm currently doing. Any ideas?

#9Dan "Heron" Myers
heron@xnapid.com
In reply to: Toby Chavez (#8)
Re: Using a composite SQL type in C

Toby Chavez wrote:

Check the documentation about using composite-type arguments in C
http://www.postgresql.org/docs/8.2/static/xfunc-c.html#AEN37402

[...]

A quick google search
brought up this tutorial that might help
http://linuxgazette.net/142/peterson.html

Cheers

I found the documentation... less than enlightening (my attempt at
following it resulted in crashes), but it looks like that tutorial will
help. I'll try it out and come back if I get stuck.

Thanks,
Dan