Thread: Exceeded maximum lock level
Hello. Freebsd 6.2, Postgresql 8.3 from ports. I'm trying to add some features to threaded application, like sql logs. I've changed application according to examples of libpq usage (simple things - connect, inserts, disconnect), everything works for a little time (under 1 minute), then i get error: Fatal error 'Exceeded maximum lock level' at line 519 in file /usr/src/lib/libpthread/thread/thr_mutex.c (errno = 844913743) This application compiled with -lpthread, and in #postgresql user RhodiumToad told me to use -pthread instead, but after changes in makefiles to use -pthread the same error comes up right after first use of sql functions (in the beginning some amount of data was actually inserted to DB before error comes up). For now i told this application not to use threads (it have command line key switching thread/fork behavior) and it works, but i think threads would be better under heavy load (i think in future there will be heavy load). If i start application with threads but not use sql functions (sql code is there but not used) i see no errors. Is it a problem of libpq? Compilation flags looks like this: CFLAGS=-O2 -fno-strict-aliasing -pipe -DFREEBSD -pthread -export-dynamic -I/usr/local/include -I . -shared -fPIC LIBS = -lpthread -L/usr/local/lib -lpq -- wbr, alexander
alexander lunyov wrote: > Hello. > > Freebsd 6.2, Postgresql 8.3 from ports. > > I'm trying to add some features to threaded application, like sql logs. > I've changed application according to examples of libpq usage (simple > things - connect, inserts, disconnect), everything works for a little > time (under 1 minute), then i get error: > > Fatal error 'Exceeded maximum lock level' at line 519 in file > /usr/src/lib/libpthread/thread/thr_mutex.c (errno = 844913743) This is clearly not a PG problem -- I'd think there's a bug in your own code. -- Alvaro Herrera http://www.CommandPrompt.com/ PostgreSQL Replication, Consulting, Custom Development, 24x7 support
Alvaro Herrera wrote: >> Fatal error 'Exceeded maximum lock level' at line 519 in file >> /usr/src/lib/libpthread/thread/thr_mutex.c (errno = 844913743) > > This is clearly not a PG problem -- I'd think there's a bug in your own > code. This is very helpful :) My code is just this: int SQLLog( PGconn *conn, struct auth *a, struct client *c, struct request *r, struct data *d) { char * str; char log[256]; char *request; unsigned char *esc_bytea; size_t length; PGresult *res; if ((esc_bytea = PQescapeByteaConn(conn,d->data,d->len,&length)) == NULL) { snprintf(log,256, "Error: %s",PQerrorMessage(conn)); logging(log); return 0; } request = malloc ( sizeof(a->user) + sizeof(c->src) + sizeof(r->dst) + sizeof(esc_bytea) + 110); sprintf(request, "INSERT INTO raw (username,from_addr,to_addr,rawdata,direction) VALUES ('%s','%s','%s',E'%s',%d)", a->user,c->src,r->dst,esc_bytea,d->dir); res = PQexec(conn, request); if (PQresultStatus(res) != PGRES_COMMAND_OK) { snprintf(log,256,"failed: %s", PQerrorMessage(conn)); logging(log); } free(request); PQfreemem(esc_bytea); PQclear(res); return 0; } int OpenSQL( PGconn **conn ) { char log[256]; char *conninfo; conninfo = "host=localhost dbname=db user=user password=password"; *conn = PQconnectdb(conninfo); if (PQstatus(*conn) != CONNECTION_OK) { snprintf(log,256, "Connection todatabase failed: %s", PQerrorMessage(*conn)); logging(log); return 1; } else return0; } int CloseSQL( PGconn *conn ) { PQfinish(conn); return 0; } I didn't touch any mutex (i don't even sure about what is mutex). While not calling any of sql functions, application works just fine. In non-threaded mode it is working with sql functions. Where did i go wrong? -- wbr, alexander
alexander lunyov wrote: > Alvaro Herrera wrote: >>> Fatal error 'Exceeded maximum lock level' at line 519 in file >>> /usr/src/lib/libpthread/thread/thr_mutex.c (errno = 844913743) >> >> This is clearly not a PG problem -- I'd think there's a bug in your own >> code. > > This is very helpful :) > > My code is just this: > I didn't touch any mutex (i don't even sure about what is mutex). While > not calling any of sql functions, application works just fine. In > non-threaded mode it is working with sql functions. Where did i go wrong? Well, there are still many things left out, like how many times you call this function before it fails. Even then, I suspect the bug is in the pthread code that comes with your operating system -- or perhaps on the patches that the port applies to Postgres. (For the record, I filled the missing pieces in your program and made SQLLog loop 100000 times, which it did without a hitch. Next time, please submit a complete program, not just pieces of it.) -- Alvaro Herrera http://www.CommandPrompt.com/ The PostgreSQL Company - Command Prompt, Inc.