Subject: Variable length text arrays

From: "Schenkelaars.V.F" <[email-masked]>
Date: Tue, 28 Apr 92 10:57:58 MET DST

Dear Postgres Group,

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|
---------------

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().

Please help me with this problem.

     ____________________
 \  / . _   _  _   _  |
  \/ ( ( \ (_ (-' ( \ |

--------------------------------------------------------------------------------
Vincent F. Schenkelaars				|
Company: FEL-TNO                                | Private:
Email  : [email-masked]	|
address: Oude Waalsdorperweg 63			| Groenhovenstraat 12
         Postbus 96864				|
         2509 JG  Den Haag			| 2311 BT  Leiden
         The Netherlands			| The Netherlands
Phone  : Holland 070-3264221			| Holland 071-125576
Fax    : Holland 070-3280961			|
--------------------------------------------------------------------------------
We apologize for the inconvenience.

---

From: postarch (Postgres Mailing Archive)
Date: Thu, 30 Apr 92 12:16:34 PDT

> Query sent to backend is "retrieve (test.polyline)"

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.

> 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
[email-masked]