Return-Path: postarch
Received: by postgres.Berkeley.EDU (5.61/1.29)
	id AA05424; Thu, 30 Apr 92 12:17:10 -0700
Message-Id: <9204301917.AA05424@postgres.Berkeley.EDU>
From: postarch (Postgres Mailing Archive)
Subject: Re: Variable length text arrays
To: postgres@postgres.berkeley.edu
Sender: pg_adm@postgres.berkeley.edu
Reply-To: mer@postgres.berkeley.edu
In-Reply-To: Your message of "Tue, 28 Apr 92 10:57:58 +0700."
             <9204280854.AA03104@postgres.Berkeley.EDU> 
Date: Thu, 30 Apr 92 12:16:34 PDT

you write:

> How can I use variable length text as an return type of a function. I've tried
> to call the Polyline2_out function which does function correctly with our
> defined type Polyline2, But when I call this function directly from the
> terminal monitor, The answer is cut off after 16 characters. Ive tried the
> following commands.
> 
> retrieve (test.polyline)          /* polyline is of type POLYLINE2  */
> 
> Query sent to backend is "retrieve (test.polyline)"
> ---------------
> | polyline    |
> ---------------
> | (6:0,0,2,2,3,1,4,-1.01,5,-2,7,0)|
> ---------------
> 
> 
> retrieve (b = Polyline2_out(test.polyline))\g
> 
> Query sent to backend is "retrieve (b = Polyline2_out(test.polyline))"
> ---------------
> | b           |
> ---------------
> | (6:0,0,2,2,3,1,4|
> ---------------

Here is what is happening.  In case one the type of the data being sent to
the front-end is POLYLINE2.  The dbms calls the POLYLINE2 output procedure
to convert from internal to external format.  In case two the type of the
data being sent is char16 (i.e. the output type of Polyline2_out) so the
backend calls char16out() which truncates strings to 16 characters.

> When I changed the output type of the function Polyline2_out to "text" instead
> of "char16", I get the message
> 
> Error: Invalid argument to pg_alloc().

The problem is that there are hardwired assumptions that all output functions
return simple null terminated character strings.  Making Polyline2_out()
return type text will screw up case one, but should work for case two.

If you declare that a function is going to return a particular type then you
have to make sure it formats the return value properly.  I have a feeling
that when you changed the output type for Polyline2_out() you didn't use
the varlena structure as you must do.  This would explain why you got the
strange error message.  Take a look at the textin() function in
src/utils/adt/varlena.c, that shows how text values should be formatted in
memory.

The following works as expected:

	retrieve (b = textin("hi there"::char16))

The solution to your problem is to either stick to case one or define a
separate function that returns a value of type text (properly formatted)
for use in queries like case 2.


Jeff Meredith
mer@postgres.berkeley.edu
