pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
libpq/c function question
3+ messages / 2 participants
[nested] [flat]

* libpq/c function question
@ 2001-01-28 02:48  Joe Conway <joe.conway@mail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Joe Conway @ 2001-01-28 02:48 UTC (permalink / raw)
  To: pgsql-interfaces

Hi,

Apologies if this hits the list twice, but my first email (from yesterday)
seems have to gone to /dev/null instead of the list :(
Also, please be gentle, this is my first foray into C programming :)

Here's what I did/am trying to do:

- First, I created a C function based on the version 1 calling convention
from the 7.1 docs
(http://www.postgresql.org/devel-corner/docs/programmer/xfunc-c.htm). It is
intended to accept two text parameters -- the first is a connection string
for PQconnectdb, and the second is a SQL statement. The code is attached in
jec-repl.c

- Second, I registered the function in PostgeSQL using jec-repl.sql
(attached).

- Third, I created a pgplsql function (sync_data_test) to invoke the C
function (sync_data_test.sql)

- The overall idea was to do a simple form of host-to-host replication using
a trigger based on a pgplsql function similar to sysnc_data_test. I wanted
to keep the C function very specific to the task of connecting to an
arbitrary host and executing a SQL statement (not returning tuples, of
course) and leave most of the replication logic to the pgplsql function.

The problem is that the C function fails to connect consistantly. I found
through trial and error that if I pad the connection string with several
spaces (the exact number seems to constant within one psql session, but
varies from session to session) it is able to connect and execute my insert
statement successfully. Below I'll include some sample output of a psql
session. I'm running RedHat Linux 6.2, and the 7.1 beta 3 tarball.

Any help or guidance is very much appreciated!

Joe Conway

p.s. Note - the function returns 2 on a failure to connect (PQstatus(conn)
== CONNECTION_BAD) and 0 on successful execution of the insert statement.

jec1=# select version();
                                version
------------------------------------------------------------------------
 PostgreSQL 7.1devel on i686-pc-linux-gnu, compiled by GCC egcs-2.91.66
(1 row)


jec1=# select sync_data_test('hostaddr=172.16.1.84 port=5432 dbname=jec2
user=postgres password=mypasswd','    insert into t1(f1) values(987)');
 sync_data_test
----------------
              2
(1 row)

jec1=# select sync_data_test(' hostaddr=172.16.1.84 port=5432 dbname=jec2
user=postgres password=mypasswd','    insert into t1(f1) values(987)');
 sync_data_test
----------------
              2
(1 row)

jec1=# select sync_data_test('  hostaddr=172.16.1.84 port=5432 dbname=jec2
user=postgres password=mypasswd','    insert into t1(f1) values(987)');
 sync_data_test
----------------
              2
(1 row)

jec1=# select sync_data_test('    hostaddr=172.16.1.84 port=5432 dbname=jec2
user=postgres password=mypasswd','    insert into t1(f1) values(987)');
 sync_data_test
----------------
              0
(1 row)

jec1=# select sync_data_test('    hostaddr=172.16.1.84 port=5432 dbname=jec2
user=postgres password=mypasswd','    insert into t1(f1) values(987)');
 sync_data_test
----------------
              0
(1 row)

jec1=# select sync_data_test('hostaddr=172.16.1.84 port=5432 dbname=jec2
user=postgres password=mypasswd'::text,'    insert into t1(f1)
values(987)');
 sync_data_test
----------------
              2
(1 row)

Attachments:

  sync_data_test.sql (application/octet-stream, 282B) download

  jec-repl.sql (application/octet-stream, 183B) download

  jec-repl.c (application/octet-stream, 891B) download

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: libpq/c function question
@ 2001-01-28 03:48  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Joe Conway <joe.conway@mail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2001-01-28 03:48 UTC (permalink / raw)
  To: Joe Conway <joe.conway@mail.com>; +Cc: pgsql-interfaces

"Joe Conway" <joe.conway@mail.com> writes:
> 	optstr = PG_GETARG_TEXT_P(0);
> 	jpgsql = PG_GETARG_TEXT_P(1);

> 	conn = PQconnectdb(VARDATA(optstr));

I think you've missed the fact that a TEXT value's VARDATA is not
null-terminated.  There are numerous examples of transforming a TEXT
into a C string in the backend, see for example text_cmp/varstr_cmp
in src/backend/utils/adt/varlena.c.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: libpq/c function question
@ 2001-01-28 04:08  Joe Conway <joe.conway@mail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Joe Conway @ 2001-01-28 04:08 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-interfaces

> "Joe Conway" <joe.conway@mail.com> writes:
> > optstr = PG_GETARG_TEXT_P(0);
> > jpgsql = PG_GETARG_TEXT_P(1);
> 
> > conn = PQconnectdb(VARDATA(optstr));
> 
> I think you've missed the fact that a TEXT value's VARDATA is not
> null-terminated.  There are numerous examples of transforming a TEXT
> into a C string in the backend, see for example text_cmp/varstr_cmp
> in src/backend/utils/adt/varlena.c.
> 
> regards, tom lane

That did the trick!

Thank you!!

Joe









^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2001-01-28 04:08 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-01-28 02:48 libpq/c function question Joe Conway <joe.conway@mail.com>
2001-01-28 03:48 ` Tom Lane <tgl@sss.pgh.pa.us>
2001-01-28 04:08   ` Joe Conway <joe.conway@mail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox