Re: Populating array of composite datatype

From: David Johnston <polobo(at)yahoo(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Populating array of composite datatype
Date: 2013-08-07 14:45:33
Message-ID: 1375886733223-5766656.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Sameer Thakur wrote
> insert into test values (ARRAY[abc(1,2)]); but got error

When you use a function form of casting like this you are basically
short-circuiting the type conversion mechanisms built into PostgreSQL by
directly calling the conversion function instead of actually telling the
system to perform a cast where the input is of one type and the output is of
another. While direct function invocation will work in some instances it is
better not to rely on it. If you really do mean to convert from one type to
another you should - almost always - use either:

the SQL standard

CAST('val' AS type)

or the PostgreSQL short-hand

'val'::type

[It seriously sucks that the standard syntax for casting is so verbose that
it is almost unusable unless you must - immediately - have cross-database
compatibility and thus can withstand the pain using it inflicts.]

There are a bunch more comments, rules, and warnings on this topic somewhere
in the documentation if you are really curious but, really, just use the
explicit casting mechanism provided by the database instead of calling the
casting functions directly.

Note I am not positive that abc(1,2) is truly an instance of this behavior
but I'm guessing you tried doing this because similar syntax has worked for
you before due to the behavior I've referenced above.

David J.

--
View this message in context: http://postgresql.1045698.n5.nabble.com/Populating-array-of-composite-datatype-tp5766628p5766656.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David Johnston 2013-08-07 15:06:09 Re: Self referencing composite datatype
Previous Message Igor Neyman 2013-08-07 14:44:17 Re: Self referencing composite datatype