User defined type in C

From: "Armel HERVE" <ah(dot)pgsql(at)laposte(dot)net>
To: <pgsql-admin(at)postgresql(dot)org>
Subject: User defined type in C
Date: 2004-05-10 10:20:31
Message-ID: 20040510102035.2C5F780038F@mwinf0402.wanadoo.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Hi everybody,

Sorry if it's not the good forum and for my poor English.

I'm trying to create a new type in C for postgreSQL (7.3.4).
This type is a structure of 4 fields including a variable length field.

When I try to insert record ino a table using this type, postgre server
crashes:
LOG: server process (pid 10838) was terminated by signal 11
LOG: terminating any other active server processes
WARNING: Message from PostgreSQL backend:
The Postmaster has informed me that some other backend
died abnormally and possibly corrupted shared memory.
I have rolled back the current transaction and am
going to terminate your database system connection and exit.
Please reconnect to the database system and repeat your query.
WARNING: Message from PostgreSQL backend:
The Postmaster has informed me that some other backend
died abnormally and possibly corrupted shared memory.
I have rolled back the current transaction and am
going to terminate your database system connection and exit.
Please reconnect to the database system and repeat your query.
WARNING: Message from PostgreSQL backend:
The Postmaster has informed me that some other backend
died abnormally and possibly corrupted shared memory.
I have rolled back the current transaction and am
going to terminate your database system connection and exit.
Please reconnect to the database system and repeat your query.
LOG: all server processes terminated; reinitializing shared memory and
semaphores
LOG: database system was interrupted at 2004-05-10 12:25:55 CEST
LOG: checkpoint record is at 0/627BE1DC
LOG: redo record is at 0/627BE1DC; undo record is at 0/0; shutdown TRUE
LOG: next transaction id: 2026530; next oid: 342157
LOG: database system was not properly shut down; automatic recovery in
progress
LOG: redo starts at 0/627BE21C
LOG: ReadRecord: record with zero length at 0/627DD5F4
LOG: redo done at 0/627DD5D0
LOG: database system is ready

Does anybody can help me ???
Thanks for your answers,

Armel HERVE

This is the C code:
#include "postgres.h"

#define KEY_SIZE 31

typedef struct {
int4 globalSize;
int64 fileSize;
char fileKey[KEY_SIZE + 1];
char fileName[1];
} Psfile;

#define PSFILE_BASIC_SIZE (4 + sizeof(int64) + KEY_SIZE + 1)

/**
*
* char *str : string representation of a psfile.
* must be formatted as follow :
* xxx,yyy,zzz
* with xxx : the name of the file
* yyy : the size of the file
* zzz : the key associated with the file.
*/
Psfile *psfile_in(char *str) {
Psfile *retValue = NULL;

int nameSize = 0;

// In first, we look for the size of the name
for(nameSize = 0; str[nameSize] != ',' && str[nameSize] != 0;
nameSize++) {
// Nothing to do in this block...
}

if(str[nameSize] == 0) {
// We are at the end of the string... Something is wrong!!
elog(ERROR, "psfile_in: string representation of a psfile :
name,size,key \"%s\"", str);
return NULL;
}

// Now, we have the size, so we can allocate memory for the global
structure
retValue = (Psfile *)palloc(PSFILE_BASIC_SIZE + nameSize + 1);

char format[15];
sprintf(format, "%%%ds,%%li,%%s", nameSize);

char *fileName = retValue->fileName;
char *fileKey = retValue->fileKey;
int64 *fileSize = &(retValue->fileSize);

if(sscanf(str, format, fileName, fileSize, fileKey) != 3) {
// Something is wrong : the number of parameters found must
be 3
pfree(retValue);
elog(ERROR, "psfile_in: string representation of a psfile :
name,size,key \"%s\"", str);
return NULL;
}

return retValue;
}

char *psfile_out(Psfile *psfile) {
elog(LOG, "psfile_out");
if(psfile == NULL) return NULL;

char *retValue = NULL;

char size[30];
sprintf(size, "%ld", psfile->fileSize);

retValue = (char *)palloc(strlen(psfile->fileName) + 1 +
strlen(size) + 1 + strlen(psfile->fileKey) + 1);

sprintf(retValue, "%s,%s,%s", psfile->fileName, size,
psfile->fileKey);

return retValue;
}

And this is the type declaration :
CREATE FUNCTION psfile_in(cstring)
RETURNS psfile
AS '/usr/include/pgsql/server/pslib/pslib'
LANGUAGE C IMMUTABLE STRICT;

CREATE FUNCTION psfile_out(psfile)
RETURNS cstring
AS '/usr/include/pgsql/server/pslib/pslib'
LANGUAGE C IMMUTABLE STRICT;

CREATE TYPE psfile(
internallength = valiable,
input = psfile_in,
output = psfile_out,
alignment = int4
);

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Tom Lane 2004-05-10 11:50:35 Re: User defined type in C
Previous Message Stephan Szabo 2004-05-08 23:33:51 Re: cast not IMMUTABLE?