Re: Automatic truncation of character values & casting to the type of a column type

From: Justin Bailey <jgbailey(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Automatic truncation of character values & casting to the type of a column type
Date: 2009-12-17 15:15:58
Message-ID: a45dff840912170715t45054cd4n8fb8314019049664@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Dec 16, 2009 at 7:20 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Really?  Works for me, in everything back to 7.3.
>

I must be missing something, because this function fails:

CREATE OR REPLACE FUNCTION insertShort()
RETURNS VOID AS
$BODY$
DECLARE
s Short.shortCol%TYPE;
BEGIN
SELECT longCol INTO s FROM Long
WHERE char_length(longCol) > 20;
INSERT INTO Short (shortCol) VALUES (s);
END;

$BODY$
LANGUAGE 'plpgsql' VOLATILE;

If s was automatically truncated, the insert would succeed, but it
fails with a "value too long" error.

p.s. I used the definitions from my prior email:

CREATE TABLE Long (longCol varchar(40) );
CREATE TABLE Short (shortCol varchar(20) );

INSERT INTO Long VALUES ('FOOBAR'), ('BAZ'),
(CAST('2314J1L234J21LK342JKL32J32KL4J123LK4J13L4' AS VARCHAR(40)));

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2009-12-17 15:24:28 Re: Automatic truncation of character values & casting to the type of a column type
Previous Message Vincenzo Romano 2009-12-17 15:00:47 Re: Table Partitioning Advice Request