How to avoid implicit type casts in PostgreSQL?
Is this an intentional feature?


Yes, implicit type conversion is an intentional feature. There are pros and cons either way but this is how postgres is designed to work:


  In many cases a user does not need to understand the details of the type conversion mechanism. However, implicit conversions done by PostgreSQL can affect the results of a query. When necessary, these results can be tailored by using explicit type conversion


  Doesn't this violate the assumption that RDBMSs should be typesafe?


No, everything is still typesafe and "SQL is a strongly typed language". Implicit conversions don't change that.

  Is there anything I can do to avoid 'casual' type casts?


Short of messing around with the system catalog, which is usually a very bad idea (and impossible in some cases), there isn't a lot you can do to avoid implicit casting entirely. Even if you include explicit casts everywhere implicit casts might still occur by mistake:




create table t(foo text);
insert into t(foo) values(11::integer);






select * from t;



| foo |
| :-- |
| 11  |



dbfiddle here