Trouble with C-Language Functions

From: Sergei Koveshnikov <acrobat(at)eurocom(dot)od(dot)ua>
To: pgsql-bugs(at)postgresql(dot)org
Subject: Trouble with C-Language Functions
Date: 2004-06-24 18:13:38
Message-ID: 200406242113.38719.acrobat@eurocom.od.ua
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

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.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Sean McCune 2004-06-24 20:44:15 Problem with now() on 7.4.1-3 under cygwin?
Previous Message Alvaro Herrera 2004-06-24 17:02:18 Re: Dump/Restore of cvs regression database gives invalid timestamp syntax error