Re: String literal doesn't autocast to text type - Mailing list pgsql-general

From David G. Johnston
Subject Re: String literal doesn't autocast to text type
Date
Msg-id CAKFQuwZFPrCh6sWDStfdwu_LaBf_XA8mwcznkS4hsdywiYedyQ@mail.gmail.com
Whole thread Raw
In response to Re: String literal doesn't autocast to text type  (Melvin Davidson <melvin6925@gmail.com>)
Responses Re: String literal doesn't autocast to text type  (Alex Ignatov <a.ignatov@postgrespro.ru>)
List pgsql-general
It would be nice if you'd follow the list convention of bottom-posting.  Not doing that is much more annoying than the omission of version and o/s - especially the later since PostgreSQL purports to be generally o/s agnostic.

On Fri, Mar 4, 2016 at 12:17 PM, Alex Ignatov <a.ignatov@postgrespro.ru> wrote:
Hello!
Why string literal like 'Hello world!' doesnt automagicaly cast to text type?


But why we don't have this type cast by default in Postgres? Is there any fundamental restriction on that or there is some reasons for that?


On Friday, March 4, 2016, Melvin Davidson <melvin6925@gmail.com> wrote:
Probably because pg_typeof() returns the OID of a COLUMN in a table def.. Strings literals do not have oid's.

http://www.postgresql.org/docs/9.4/interactive/functions-info.html

"pg_typeof returns the OID of the data type of the value that is passed to it. This can be helpful for troubleshooting or dynamically constructing SQL queries. The function is declared as returning regtype, which is an OID alias type (see Section 8.18); this means that it is the same as an OID for comparison purposes but displays as a type name. For example:"

While true this doesn't actually answer the question - that being what's the motiviation for not implicitly casting away from unknown.  I suspect that the main reason is that in an extensible system like PostgreSQL it is deemed risky to have too many implicit casts.  Having one from unknown required the system to make decisions that could result in unexpected results that would be hard to catch without careful review of queries and results.  It is an issue of some contention in the community but so far no one has convinced the committees to change how this works.
 
Please in the future, ALWAYS specify your PostgreSQL version and O/S, regardless of whether or not you thinnk it is pertinent.

But don't feel too bad if you forget...
 
David J.
 

pgsql-general by date:

Previous
From: Alex Ignatov
Date:
Subject: Re: String literal doesn't autocast to text type
Next
From: Francisco Olarte
Date:
Subject: Re: multiple UNIQUE indices for FK