Home > Archive > PostgreSQL Discussion > March 2005 > plpgsql array initialization, what's the story?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author plpgsql array initialization, what's the story?
Karl O. Pinc

2005-03-31, 8:03 pm

Postgresql 8.0.1

If I write the plpgsql:

declare
y int[];
begin
y[1] := 1;
y[2] := 2;
y[3] := 3;
....

All y[] array elements are NULL, as is array_dims(y).
But if I write:

declare
y int[] := '{}';
begin
y[1] := 1;
y[2] := 2;
y[3] := 3;
....

Then things work as expected.

What's going on? (As in "Gosh, it looks like something's
happening here that I should know about.")

This behavior is not clear from the plpgsql documentation.

Regards,

Karl <kop@meme.com>
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein



---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere
" to majordomo@postgresql
.org)

Michael Fuhr

2005-03-31, 8:03 pm

On Thu, Mar 31, 2005 at 07:13:30PM +0000, Karl O. Pinc wrote:
> Postgresql 8.0.1
>
> If I write the plpgsql:
>
> declare
> y int[];
> begin
> y[1] := 1;
> y[2] := 2;
> y[3] := 3;
> ...
>
> All y[] array elements are NULL, as is array_dims(y).


I think this has been fixed for 8.0.2:

http://archives.postgresql.org/pgsq...02/msg00012.php

Here's a test in 8.0.2beta1:

CREATE FUNCTION foo() RETURNS integer[] AS $$
DECLARE
y integer[];
BEGIN
y[1] := 1;
y[2] := 2;
y[3] := 3;

RETURN y;
END;
$$ LANGUAGE plpgsql;

SELECT foo();
foo
---------
{1,2,3}
(1 row)

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2008 droptable.com