1

I'm currently trying to implement at user-defined data type in Postgres in the form of a linked list with each node holding an int and a pointer to the next node, here are my input functions:

 // Defining struct for linked list
 typedef struct intSet *link;

typedef struct intList {
    int num;
    link next;
} intList;

 //  Create a new node
 link newNode(int item) {
     link n = (link) palloc(sizeof(*n));
     n->num = item;
     n->next = NULL;
     return n;
 }

 link insertEnd(link list, link n){
    link curr;
    // Empty list
    if(list == NULL){
        list = n;
        n->next = NULL;
    // If list not empty, iterate to end of list, then append
    } else {
        for(curr = list; curr->next != NULL; curr = curr->next) {
        }
        curr->next = n;
        n->next = NULL;
     }
     return list;
 }    

 PG_FUNCTION_INFO_V1(intList_in);

 Datum
 intList_in(PG_FUNCTION_ARGS)
 {
     char       *str = PG_GETARG_CSTRING(0);
     char       *token;

 // Create an empty linked list
 link newList;
 newList = NULL;

 // Get individual ints from a set e.g. {1, 2, 3, 4}
 token = strtok(str, ",{} ");

 // For each int, create a new node then
 // append to list
   while (token != NULL) {
       link a = NULL;
       a = newNode(atoi(token));
       newList = insertEnd(newList, a);
       token = strtok(NULL, ",{} ");
   }

   PG_RETURN_POINTER(newList);
}

One thing I'm struggling with is that in the set.source file that I'm to include with this set.c file, in the creation of the type:

CREATE TYPE set {
     internallength = ?,
     input = .., output = .., alignment = ..
}

I'm not sure what to put for the internal length. Upon checking the sizes, I found that the struct had a size of 8 bytes, the num member 4 bytes, and the pointer next was 8 bytes. Does this mean that I have to put 'variable' in the internallength field? If so what would the values of the (int4 field in the struct for var length) and alignment be in this case?

Any pointers/tips would be greatly appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.