Thread: Problem with PG_GETARG_CSTRING

Problem with PG_GETARG_CSTRING

From
"Brett Maton"
Date:

I'm probably just being a numpty, but I can't seem to get arguments with PG_GETARG_CSTRING.
I would much appreciate it if someone could point me in the right direction!

  The following code is simply test stuff and doesn't actually do anything other than raise notices

Thanks,
Brett

#include <strings.h>

#include "postgres.h"
#include "fmgr.h"
#include "executor/spi.h"

//PG_MODULE_MAGIC;

Datum bm_test(PG_FUNCTION_ARGS);

PG_FUNCTION_INFO_V1(bm_test);

Datum
bm_test(PG_FUNCTION_ARGS)
{
HeapTuple     tuple = NULL;
TupleDesc     tupdesc;
SPITupleTable *tuptable;
char          *szHostName = NULL;
int           i, ret;

//  palloc
  char *szPolicy = PG_GETARG_CSTRING(0);

elog(NOTICE, "szPolicy len [%d]'%s'\n", strlen(szPolicy), szPolicy);

  /*  Connect to the SPI Manager */
  if ((ret = SPI_connect()) < 0)
    /* internal error */
    elog(ERROR, "check_primary_key: SPI_connect returned %d", ret);

  /* Execute SELECT */
  if((ret = SPI_exec("SELECT sv_HostName FROM servers", 0)) != SPI_OK_SELECT) {
    /* CASE Error results ?, external function ? */
    elog(ERROR, "SELECT Failed, SP_exec returned %d.", ret);
  }

  while (SPI_tuptable->vals[i] != NULL)
  {
    tupdesc  = SPI_tuptable->tupdesc;
    tuptable = SPI_tuptable;
    tuple    = SPI_tuptable->vals[i];
    i++;

    szHostName = SPI_getvalue(tuple, tupdesc, 1);
    elog(NOTICE, "HostName %s", szHostName);
  }

  SPI_finish();
  PG_RETURN_NULL();
}

Compiled with:

gcc -Wall -Wmissing-prototypes -fpic -I/usr/include/pgsql/server/ -c bkptime.c
gcc -shared -o bkptime.so bkptime.o

Function created with:

create function bm_test(CSTRING) returns TEXT as '/home/brettm/BkpTime/bkptime.so' LANGUAGE 'c';

Executing the function returns the following:

ServerStats=# select * from bm_test('PRODAPP');
NOTICE:  szPolicy len [1]'
                          '

NOTICE:  HostName .......


-----------------------------------------------------------------------------------------------
This email, and the contents contained within, are private
and confidential between the sender and the individual(s)
to whom it is addressed.
The contents do not necessarily represent the views of Simplyhealth Group
Limited.

In the event of misdirection, the recipient is prohibited from 
using, copying or disseminating it or any information contained in it.
If you have received this email in error please notify Simplyhealth
immediately by telephone on 0845 075 2020, or by email to 
network.administrator@simplyhealth.co.uk

Simplyhealth Group Limited is registered and incorporated in England and Wales  
as a company limited by guarantee. Its registered office is at Hambleden House, 
Waterloo Court, Andover, Hampshire, SP10 1LQ, registered no. 5445654. We 
may record or monitor telephone calls to help improve our service and protect our 
members.
-----------------------------------------------------------------------------------------------

Re: Problem with PG_GETARG_CSTRING

From
Tom Lane
Date:
"Brett Maton" <Brett.Maton@simplyhealth.co.uk> writes:
> I'm probably just being a numpty, but I can't seem to get arguments with PG=
> _GETARG_CSTRING.

The code looks all right as far as I can see --- I suspect procedural
error, like you aren't loading the right .so file, or changed the code
and didn't re-LOAD the .so, or something like that.

            regards, tom lane

Re: Problem with PG_GETARG_CSTRING

From
"Brett Maton"
Date:

Ok, that means it's not something blindingly obvious then :-)

  I brought the code home and have tried it on one of my machines here.

  First time the function has been created so no picking the wrong .so
  Differences are that here I'm running Postgres 8.1.10-1 on Fedora Core 6

  Unfortunately when I call the function with:

  select * from bm_test('PRODAPP');

  I get the same result:

  NOTICE:  szPolicy len [1]'
                          '

  Followed by the expected list of Notices for each hostname in the servers table.
  My C is a bit rusty I'll be the first to admit but I'm really not getting where the line break is coming from.

  For info in my pg_log file it's reporting:

  NOTICE:  szPolicy len [1]'^K'

  Not sure if ^K means anything to anyone, would have expected to see ^J (10) or ^M (13)...

Brett

-----Original Message-----
From: Tom Lane [mailto:tgl@sss.pgh.pa.us]
Sent: Fri 12/10/2007 16:10
To: Brett Maton
Cc: pgsql-novice@postgresql.org
Subject: Re: [NOVICE] Problem with PG_GETARG_CSTRING

"Brett Maton" <Brett.Maton@simplyhealth.co.uk> writes:
> I'm probably just being a numpty, but I can't seem to get arguments with PG=
> _GETARG_CSTRING.

The code looks all right as far as I can see --- I suspect procedural
error, like you aren't loading the right .so file, or changed the code
and didn't re-LOAD the .so, or something like that.

                        regards, tom lane




-----------------------------------------------------------------------------------------------
This email, and the contents contained within, are private
and confidential between the sender and the individual(s)
to whom it is addressed.
The contents do not necessarily represent the views of Simplyhealth Group
Limited.

In the event of misdirection, the recipient is prohibited from 
using, copying or disseminating it or any information contained in it.
If you have received this email in error please notify Simplyhealth
immediately by telephone on 0845 075 2020, or by email to 
network.administrator@simplyhealth.co.uk

Simplyhealth Group Limited is registered and incorporated in England and Wales  
as a company limited by guarantee. Its registered office is at Hambleden House, 
Waterloo Court, Andover, Hampshire, SP10 1LQ, registered no. 5445654. We 
may record or monitor telephone calls to help improve our service and protect our 
members.
-----------------------------------------------------------------------------------------------

Re: Problem with PG_GETARG_CSTRING

From
"Brett Maton"
Date:

I am a numpty :-D

  Thanks Tom, your little insights sent me digging.

  The problem was that my little function was defined twice in the database as:

  bm_test(CSTRING)
  bm_test(varchar)

  I wouldn't have been looking for that!

Thanks again.

Brett


-----Original Message-----
From: pgsql-novice-owner@postgresql.org on behalf of Brett Maton
Sent: Fri 12/10/2007 16:49
To: Tom Lane
Cc: pgsql-novice@postgresql.org
Subject: Re: [NOVICE] Problem with PG_GETARG_CSTRING

Ok, that means it's not something blindingly obvious then :-)

  I brought the code home and have tried it on one of my machines here.

  First time the function has been created so no picking the wrong .so
  Differences are that here I'm running Postgres 8.1.10-1 on Fedora Core 6

  Unfortunately when I call the function with:

  select * from bm_test('PRODAPP');

  I get the same result:

  NOTICE:  szPolicy len [1]'
                          '

  Followed by the expected list of Notices for each hostname in the servers table.
  My C is a bit rusty I'll be the first to admit but I'm really not getting where the line break is coming from.

  For info in my pg_log file it's reporting:

  NOTICE:  szPolicy len [1]'^K'

  Not sure if ^K means anything to anyone, would have expected to see ^J (10) or ^M (13)...

Brett

-----Original Message-----
From: Tom Lane [mailto:tgl@sss.pgh.pa.us]
Sent: Fri 12/10/2007 16:10
To: Brett Maton
Cc: pgsql-novice@postgresql.org
Subject: Re: [NOVICE] Problem with PG_GETARG_CSTRING

"Brett Maton" <Brett.Maton@simplyhealth.co.uk> writes:
> I'm probably just being a numpty, but I can't seem to get arguments with PG=
> _GETARG_CSTRING.

The code looks all right as far as I can see --- I suspect procedural
error, like you aren't loading the right .so file, or changed the code
and didn't re-LOAD the .so, or something like that.

                        regards, tom lane

-----------------------------------------------------------------------------------------------
This email, and the contents contained within, are private
and confidential between the sender and the individual(s)
to whom it is addressed.
The contents do not necessarily represent the views of Simplyhealth Group
Limited.

In the event of misdirection, the recipient is prohibited from
using, copying or disseminating it or any information contained in it.
If you have received this email in error please notify Simplyhealth
immediately by telephone on 0845 075 2020, or by email to
network.administrator@simplyhealth.co.uk

Simplyhealth Group Limited is registered and incorporated in England and Wales 
as a company limited by guarantee. Its registered office is at Hambleden House,
Waterloo Court, Andover, Hampshire, SP10 1LQ, registered no. 5445654. We
may record or monitor telephone calls to help improve our service and protect our
members.
-----------------------------------------------------------------------------------------------




-----------------------------------------------------------------------------------------------
This email, and the contents contained within, are private
and confidential between the sender and the individual(s)
to whom it is addressed.
The contents do not necessarily represent the views of Simplyhealth Group
Limited.

In the event of misdirection, the recipient is prohibited from 
using, copying or disseminating it or any information contained in it.
If you have received this email in error please notify Simplyhealth
immediately by telephone on 0845 075 2020, or by email to 
network.administrator@simplyhealth.co.uk

Simplyhealth Group Limited is registered and incorporated in England and Wales  
as a company limited by guarantee. Its registered office is at Hambleden House, 
Waterloo Court, Andover, Hampshire, SP10 1LQ, registered no. 5445654. We 
may record or monitor telephone calls to help improve our service and protect our 
members.
-----------------------------------------------------------------------------------------------