Re: smallint out of range EXECUTEing prepared statement

From: Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>
To: Justin Pryzby <pryzby(at)telsasoft(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: smallint out of range EXECUTEing prepared statement
Date: 2017-01-19 00:59:24
Message-ID: 87shofub06.fsf@news-spur.riddles.org.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

>>>>> "Justin" == Justin Pryzby <pryzby(at)telsasoft(dot)com> writes:

Justin> Is this expected behavior ?

Justin> ts=# SELECT * FROM t WHERE site_id=32768 LIMIT 1;
Justin> (0 rows)

Justin> ts=# PREPARE x AS SELECT * FROM t WHERE site_id=$1 LIMIT 1;
Justin> PREPARE
Justin> ts=# EXECUTE x(32768);
Justin> ERROR: smallint out of range

If column "site_id" is of type smallint, then parse analysis will deduce
a type of smallint for $1, which is otherwise of unknown type. So the
prepared statement "x" then has one parameter of type smallint.

Passing 32768 for that parameter therefore fails with the expected error.

Justin> ts=# PREPARE y AS SELECT * FROM t WHERE site_id::int=$1 LIMIT 1;
Justin> PREPARE

Now $1 is of type integer, not smallint, because parse analysis sees
(integer = unknown) and deduces the type from that.

(a better way would be WHERE site_id = $1::integer, which would allow
index usage on site_id, unlike your example)

--
Andrew (irc:RhodiumToad)

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Ryan Murphy 2017-01-19 01:13:21 Re: Re: Clarifying "server starting" messaging in pg_ctl start without --wait
Previous Message Ryan Murphy 2017-01-19 00:53:26 Re: Re: Clarifying "server starting" messaging in pg_ctl start without --wait