Thread: how to determine which types take a length argument

how to determine which types take a length argument

From
Robert Treat
Date:
Is there a way to determine which datatypes take a length argument (eg. 
varchar, time, etc...) by looking in the system catalogs? pg_type doesnt seem 
to have the info... or is there a single place in the back end code that 
contains this info?     

-- 
Robert Treat
Build A Brighter LAMP :: Linux Apache {middleware} PostgreSQL


Re: how to determine which types take a length argument

From
Martijn van Oosterhout
Date:
On Wed, Aug 09, 2006 at 10:44:22AM -0400, Robert Treat wrote:
> Is there a way to determine which datatypes take a length argument (eg.
> varchar, time, etc...) by looking in the system catalogs? pg_type doesnt seem
> to have the info... or is there a single place in the back end code that
> contains this info?

Sure, wherever the typlen column in pg_type is -1. In that column,
positive is fixed length, -1 is a verlena type and -2 is null
terminated (for cstring).

In the backend this info is available using get_typlenbyval() or a few
other functions, see utils/cache/lsyscache.c

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.

Re: how to determine which types take a length argument

From
Robert Treat
Date:
On Wednesday 09 August 2006 10:53, Martijn van Oosterhout wrote:
> On Wed, Aug 09, 2006 at 10:44:22AM -0400, Robert Treat wrote:
> > Is there a way to determine which datatypes take a length argument (eg.
> > varchar, time, etc...) by looking in the system catalogs? pg_type doesnt
> > seem to have the info... or is there a single place in the back end code
> > that contains this info?
>
> Sure, wherever the typlen column in pg_type is -1. In that column,
> positive is fixed length, -1 is a verlena type and -2 is null
> terminated (for cstring).
>

pagila=# select now()::varchar(3), now()::time(3);now |     now
-----+--------------200 | 13:19:34.339
(1 row)
pagila=# select typname, typlen from pg_type where typname in 
('varchar', 'time');typname | typlen
---------+--------varchar |     -1time    |      8
(2 rows)

Both time and varchar take an argument, but they have different typlen values.  
I don't think the docs are wrong here, I think they just don't tell me what I 
am looking for. 

-- 
Robert Treat
Build A Brighter LAMP :: Linux Apache {middleware} PostgreSQL


Re: how to determine which types take a length argument

From
Martijn van Oosterhout
Date:
On Wed, Aug 09, 2006 at 01:20:41PM -0400, Robert Treat wrote:
> Both time and varchar take an argument, but they have different typlen values.
> I don't think the docs are wrong here, I think they just don't tell me what I
> am looking for.

Oh, you're referring to typmod values. All those are defined by the
grammer directly, special cased (which is what we want to change). They
should be documented somewhere, but they'd all be expantions of the
ConstTypename in the grammer.

Offhand I can see:

FLOAT, DECIMAL, DEC, NUMERIC, BIT, BIT VARYING and all the char/varchar
types.

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.