Thread: table creation using backend functions

table creation using backend functions

From
Alice Lottini
Date:
Hi everybody,
We've created a dynamically loadable module which
reads data by directly invoking the access methods;
now we need to create a new table and to write data in
it by calling backend functions, as we cannot use SQL
statements.

Here is our function body:

...

/* DUMMY_TUPLE is a tuple of our newly-defined type
(whose creation is successful) */
tupdesc=RelationNameGetTupleDesc(DUMMY_TUPLE);

/* newtabcreate is our function's name */
namelist=textToQualifiedNameList(relname,"newtabcreate");
rangevar=makeRangeVarFromNameList(namelist);
relnamespace=RangeVarGetCreationNamespace(rangevar);
if (!IsBootstrapProcessingMode())
{
aclresult=pg_namespace_aclcheck(relnamespace,GetUserId(),ACL_CREATE); if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult,get_namespace_name(relnamespace));
}
namestring=NameListToString(namelist);
reloid=heap_create_with_catalog(namestring,relnamespace,tupdesc,RELKIND_RELATION,false,allowSystemTableMods);
CommandCounterIncrement();
result=ObjectIdGetDatum(reloid);

...

As we try and execute the function, some fatal error
occurs and the server terminates and restarts again.
Obviously the table hasn't been created, but the
strange thing is that both relnamespace (the namespace
Oid) and reloid (the relation oid) seem to be valid
(they do not match with the InvalidOid).
We've also seen that there is the function
DefineRelation which maybe could be useful, but we do
not know how to directly fill the CreateStmt structure
(in particular: how do we have to set NodeTag and
TableElts?).
Thanks in advance for your help!

alice and lorena

______________________________________________________________________
Yahoo! Cellulari: loghi, suonerie, picture message per il tuo telefonino
http://it.yahoo.com/mail_it/foot/?http://it.mobile.yahoo.com/index2002.html



Re: table creation using backend functions

From
Tom Lane
Date:
Alice Lottini <alice_lottini@yahoo.it> writes:
> /* DUMMY_TUPLE is a tuple of our newly-defined type
> (whose creation is successful) */
> tupdesc=RelationNameGetTupleDesc(DUMMY_TUPLE);
> ...
> reloid=heap_create_with_catalog(namestring,relnamespace,tupdesc,RELKIND_RELATION,false,allowSystemTableMods);

I have a vague recollection that heap_create_with_catalog modifies the
passed-in tupdesc.  You may need to copy the tupdesc rather than just
passing the one that belongs to the DUMMY_TUPLE relation.
        regards, tom lane