Thread: typemode for variable types

typemode for variable types

From
Mohsen SM
Date:
Hello.
I have a new type similar to varchar.
I want to fine how did I can to calculate typemod
and where must I calculate typemod for this type.
thanks.

Re: typemode for variable types

From
Bruce Momjian
Date:
On Sun, Feb 23, 2014 at 07:50:08AM +0330, Mohsen SM wrote:
> Hello.
> I have a new type similar to varchar.
> I want to fine how did I can to calculate typemod
> and where must I calculate typemod for this type.

Well, typmods are type-specific, so there is no official way to
calculate it.  I would look at how an existing type uses typmod and copy
that.

--  Bruce Momjian  <bruce@momjian.us>        http://momjian.us EnterpriseDB
http://enterprisedb.com
 + Everyone has their own god. +



Re: typemode for variable types

From
Alvaro Herrera
Date:
Bruce Momjian escribió:
> On Sun, Feb 23, 2014 at 07:50:08AM +0330, Mohsen SM wrote:
> > Hello.
> > I have a new type similar to varchar.
> > I want to fine how did I can to calculate typemod
> > and where must I calculate typemod for this type.
> 
> Well, typmods are type-specific, so there is no official way to
> calculate it.  I would look at how an existing type uses typmod and copy
> that.

Our system is pretty neat.  See a complex example here:
https://github.com/postgis/postgis/blob/svn-trunk/postgis/gserialized_typmod.c

-- 
Álvaro Herrera                http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services



Re: typemode for variable types

From
Tom Lane
Date:
Alvaro Herrera <alvherre@2ndquadrant.com> writes:
> Bruce Momjian escribi�:
>> Well, typmods are type-specific, so there is no official way to
>> calculate it.  I would look at how an existing type uses typmod and copy
>> that.

> Our system is pretty neat.  See a complex example here:
> https://github.com/postgis/postgis/blob/svn-trunk/postgis/gserialized_typmod.c

One other point is that if you do consult the varchar functions as
an example, be aware that there's an offset of 4 in their definition
of the typmod (eg, for varchar(3) the stored typmod is 7).  This is
entirely for legacy reasons so there's no good reason to duplicate it
in a new custom-made type.  Except for the rule that negative values
mean "unspecified typmod" (which you have to support), you can
define the contents of the typmod value however you want.
        regards, tom lane



Re: typemode for variable types

From
Greg Stark
Date:
<p dir="ltr">Also keep in mind that the system doesn't always retain the typmod. So the datum should be possible to
interpretwithout the typmod.  Incidental effects such as length limits or precision displayed are ok but the meaning
shouldn'tbe changed.<p dir="ltr">-- <br /> greg<div class="gmail_quote">On 24 Feb 2014 20:34, "Tom Lane" <<a
href="mailto:tgl@sss.pgh.pa.us">tgl@sss.pgh.pa.us</a>>wrote:<br type="attribution" /><blockquote class="gmail_quote"
style="margin:00 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> Alvaro Herrera <<a
href="mailto:alvherre@2ndquadrant.com">alvherre@2ndquadrant.com</a>>writes:<br /> > Bruce Momjian escribió:<br />
>>Well, typmods are type-specific, so there is no official way to<br /> >> calculate it.  I would look at
howan existing type uses typmod and copy<br /> >> that.<br /><br /> > Our system is pretty neat.  See a
complexexample here:<br /> > <a
href="https://github.com/postgis/postgis/blob/svn-trunk/postgis/gserialized_typmod.c"
target="_blank">https://github.com/postgis/postgis/blob/svn-trunk/postgis/gserialized_typmod.c</a><br/><br /> One other
pointis that if you do consult the varchar functions as<br /> an example, be aware that there's an offset of 4 in their
definition<br/> of the typmod (eg, for varchar(3) the stored typmod is 7).  This is<br /> entirely for legacy reasons
sothere's no good reason to duplicate it<br /> in a new custom-made type.  Except for the rule that negative values<br
/>mean "unspecified typmod" (which you have to support), you can<br /> define the contents of the typmod value however
youwant.<br /><br />                         regards, tom lane<br /><br /><br /> --<br /> Sent via pgsql-hackers
mailinglist (<a href="mailto:pgsql-hackers@postgresql.org">pgsql-hackers@postgresql.org</a>)<br /> To make changes to
yoursubscription:<br /><a href="http://www.postgresql.org/mailpref/pgsql-hackers"
target="_blank">http://www.postgresql.org/mailpref/pgsql-hackers</a><br/></blockquote></div>