dbTalk Databases Forums  

[BUGS] Trouble with C-Language Functions

mailing.database.pgsql-bugs mailing.database.pgsql-bugs


Discuss [BUGS] Trouble with C-Language Functions in the mailing.database.pgsql-bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Sergei Koveshnikov
 
Posts: n/a

Default [BUGS] Trouble with C-Language Functions - 06-24-2004 , 01:22 PM






Hello to everyone!
I've got some troubles with C-Language Functions.

I wrote simple shared library:
/*=========== dbsync.c ======================*/
#include <stdio.h>
#include <syslog.h>
#include "postgres.h"
#include "fmgr.h"
#include "libpq-fe.h"
/*
CREATE OR REPLACE FUNCTION dbsync_delete() RETURNS int
AS '/usr/local/pgsql/lib/dbsync','dbsync_delete'
LANGUAGE C STRICT;
*/

static PGconn* db;

int connect() {
const char *dbinfo;
dbinfo = "hostaddr=127.0.0.1 port=5432 dbname=test user=some_user
password=some_password";
db = PQconnectdb(dbinfo);
if(PQstatus(db) != CONNECTION_OK) {
syslog(LOG_PID|LOG_ERR, "Connection to database '%s' failed.\n",
PQdb(db));
syslog(LOG_PID|LOG_ERR, "%s", PQerrorMessage(db));
return(-1);
}
return(0);
};

void disconnect() {
PQfinish(db);
};

PG_FUNCTION_INFO_V1(dbsync_delete);
Datum
dbsync_delete(PG_FUNCTION_ARGS) {
openlog("dbsync", LOG_PID, LOG_ERR);
if(connect() == -1) {
syslog(LOG_PID|LOG_ERR, "Error during connection to the DB.\n");
PG_RETURN_NULL();
}
PGresult* res;
res = PQexec(db, "BEGIN TRANSACTION");
res = PQexec(db, "DELETE FROM test1 WHERE val=1");
res = PQexec(db, "COMMIT TRANSACTION");
syslog(LOG_PID|LOG_ERR, "OK.\n");
disconnect();
closelog();
PG_RETURN_NULL();
};
/*=================================*/

Then I compiled it:

cd /home/acrobat/dbsync/
make -k
rm -f dbsync.so
gcc -c -fPIC -o dbsync.o dbsync.c \
-I/usr/local/pgsql/include \
-I/usr/local/src/postgresql-7.4.3/src/include/
gcc -shared -fPIC -o dbsync.so dbsync.o -L/usr/local/pgsql/lib -L. -lpq
cp dbsync.so /usr/local/pgsql/lib

Compilation finished at Thu Jun 24 20:57:01

Then I created FUNCTION:
test=# CREATE OR REPLACE FUNCTION dbsync_delete() RETURNS int AS
'/usr/local/pgsql/lib/dbsync','dbsync_delete' LANGUAGE C STRICT;

CREATE FUNCTION

And tried to use it:


test=# SELECT dbsync_delete();
dbsync_delete
---------------

(1 row)

And in the log file I saw:
====================================

Jun 24 21:07:01 relay3 postgres[26968]: [16-1] LOG: statement: SELECT
dbsync_delete();
Jun 24 21:07:01 relay3 dbsync[26968]: Connection to database 'test' failed.
Jun 24 21:07:01 relay3 dbsync[26968]: could not create socket: Too many open
files
Jun 24 21:07:01 relay3 dbsync[26968]: Connection to database 'test' failed.
Jun 24 21:07:01 relay3 dbsync[26968]: could not connect to server: Too many
open files ^IIs the server running on host "127.0.0.1" and accepting ^ITCP/IP
connections on port 5432?
Jun 24 21:07:01 relay3 dbsync[26968]: Connection to database 'test' failed.
Jun 24 21:07:01 relay3 dbsync[26968]: could not connect to server: Too many
open files ^IIs the server running on host "127.0.0.1" and accepting ^ITCP/IP
connections on port 5432?
Jun 24 21:07:01 relay3 dbsync[26968]: Connection to database 'test' failed.
Jun 24 21:07:01 relay3 dbsync[26968]: could not connect to server: Too many
open files ^IIs the server running on host "127.0.0.1" and accepting ^ITCP/IP
connections on port 5432?
....
....
.... A LOT OF SAME STRINGS ...
....
....
Jun 24 21:07:01 relay3 dbsync[26968]: Connection to database 'test' failed.
Jun 24 21:07:01 relay3 dbsync[26968]: could not connect to server: Too many
open files ^IIs the server running on host "127.0.0.1" and accepting ^ITCP/IP
connections on port 5432?
Jun 24 21:07:01 relay3 dbsync[26968]: Error during connection to the DB.
Jun 24 21:07:01 relay3 dbsync[26968]: [17-1] LOG: duration: 231.663 ms
====================================

What's the matter? Where I've been wrong?

I use:
PostgreSQL 7.4.3
Linux MyHost 2.4.24 #1 SMP Wed Feb 11 13:39:32 EET 2004 i686 GNU/Linux
gcc version 3.2.3

Thank you!
--
Best regards,
Sergei Koveshnikov.

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Reply With Quote
  #2  
Old   
Tom Lane
 
Posts: n/a

Default Re: [BUGS] Trouble with C-Language Functions - 06-24-2004 , 04:47 PM






Sergei Koveshnikov <acrobat (AT) eurocom (DOT) od.ua> writes:

Quote:
int connect() {
I'm wondering about link conflicts with the standard system call connect().

Also, if you use syslog logging for Postgres error messages, it's
probably unwise to call openlog/closelog for yourself.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.