Обсуждение: Custom Base Type in C

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

Custom Base Type in C

От
"Toby Chavez"
Дата:
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

Re: Custom Base Type in C

От
"David Wilson"
Дата:
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

Re: Custom Base Type in C

От
Alvaro Herrera
Дата:
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

Using a composite SQL type in C

От
"Dan \"Heron\" Myers"
Дата:
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

Re: Custom Base Type in C

От
"Toby Chavez"
Дата:
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.
 
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

Re: Custom Base Type in C

От
"Toby Chavez"
Дата:
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:
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

Re: Custom Base Type in C

От
Dimitri Fontaine
Дата:
-----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-----

Re: Using a composite SQL type in C

От
"Toby Chavez"
Дата:

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


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?


 

Re: Using a composite SQL type in C

От
Dan Myers
Дата:
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