I want to create a psql-C function with an undefined length list of integers (GIDs for example).

An erratic example using V0 function definition that works is like:

int mytest4(int *x, int i){
  int *a  = &(x[6]);
  int j;
  for (j = 0; j < i; j++)
     printf("Passed on %d", a[j]); // sent to logfile
  return a[i];

My question concerns the V1 functions to read an int array. I don't understand how to do

Datum mytest5(PG_FUNCTION_ARGS){
int *x = PG_GETARG_???(0);
int i = PG_GETARG_INT32(1);

Does anyone know how to write the same function, please??


1 Answer 1


Okay that was a tricky one, you just have to force cast using

int *x = (int *) PG_GETARG_POINTER(1);

and that should do the job...

I still don't get everything, but /usr/include/postgresql/9.1/server/utils/array.h says :

  • A standard varlena array has the following internal structure:
    • - standard varlena header word
    • - number of dimensions of the array
    • - offset to stored data, or 0 if no nulls bitmap
    • - element type OID
    • - length of each array axis (C array of int)
    • - lower boundary of each dimension (C array of int)
    • - bitmap showing locations of nulls (OPTIONAL)
    • - whatever is the stored data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.