Subject: About "Queries over variable sized arrays" too!

From: lmr <[email-masked]>
Date: Thu, 12 May 1994 15:54:03 -0400 (EDT)

I worked on the exact same problem yesterday and thought about to post
yesterday but did not. 

I create a class by:

create manager (name=char16, employee=text[])\g
append manager (name="mike", employee="{"wei", "greg", "jeff"}")\g
append manager (name="tom", employee="{"bill", "joe", "ren", "jeff"}")\g

and want to write rule or C code to find the length of the array. The C code
I used:

int array_length(t)
TUPLE t;
{
  int i;
  char** names;
  bool isnull=FALSE;

  i=0;
  names=(char**)GetAttributeByName(t, "employee", &isnull);

  for(i=0; !isnull&& names[i]!=NULL; i++)
    {
      /*      printf("%s\n", names[i]);*/
    }
  return i;
}

/*
  define function array_length
  (language="c", returntype=int4)
  arg is (manager)
  as "/n/music/0/ren/postgres/array_length.o"\g
*/

It runs but gives incorrect result. 

*  retrieve (x=array_length(m)) from m in manager\g

Query sent to backend is "retrieve (x=array_length(m)) from m in manager"
---------------
| x           |
---------------
| 7           |
---------------
| 9           |
---------------

How does postgres encode the end of a variable array?
-- 
Liming Ren                       |  (614) 292-8989                   |  
Dept. of Mathematics             |  231 West 18th Avenue
The Ohio State University        |  Columbus, Ohio43210

===============================================================================
    To add/remove yourself from the POSTGRES mailing list: send mail with 
    the subject line ADD or DEL to "[email-masked]"

    If this fails, send mail to "[email-masked]" and
    a human will deal with it.  DO NOT post to the "postgres" mailing list.
===============================================================================

---

From: Keith Sklower <[email-masked]>
Date: Fri, 13 May 1994 13:03:54 -0700

I shoulda looked before I leaped.  I spent 3 hours which I didn't
have writing the document .... sorry.

I also now just spent 20 minutes looking at the 4.1 code.
4.1 arrays have to fit in a page, aren't multidimensional, and can't
be backed by large objects.
It appears, from looking at the older version of arrayfuncs.c
that as before the first 4 bytes is a 32 bit integer telling you
the total size of the array (as with any other varlena).
The array elements may be a fixed size of either 1,2,or4 bytes in
which case you can do the calculation easy enough, or it assumed
to be variable length, in which case you have to walk through
the array (as a sequence of varlena's).
I urge you in the strongest possible terms to upgrade to 4.2;
very large numbers of bugs were fixed, and the support for arrays
is much better and more flexible.