Thread: Threads With Libpq Issue

Threads With Libpq Issue

From
dinesh kumar
Date:
Respected All,

This is my first post in PG-Generals. If it is not the place for these kind of queries, then please guide me where i need to post. 

I have a quick question regarding "pthread" with PostgreSQL 9.0 Libpq. I'm facing a problem with "Pthread" and libpq. Please find the below program behavoiur. 

Connection_To_PG()
{
/* Making a connection to PG 9.0 */
}

void* Independent_Thread1()
{
while(1)
{
sleep(5);
/* Doing 1 Insert Operation on Table A*/
}
}

void* Independent_Thread2()
{
while(1)
{
sleep(5);
/*Doing 1 Insert Operation on Table B*/
}

main()
{
pthread Ind1,Ind2;
Connection_TO_PG();
pthread_create(&Ind1,NULL,&Independent_Thread1,NULL);
pthread_create(&Ind2,NULL,&Independent_Thread2,NULL);
if(pthread_join(Ind1,NULL)<0)
{
printf("Ind1 is completed");
}
if(pthread_join(Ind2,NULL)<0)
{
printf("Ind2 is completed");
}
}


Problem Description:
====================
When i ran the above program, it's running(i.e inserting 2 to 10 records) some time and going to hang state. Some times, it's running more than 15 minutes and some times only 2 to 3 minutes. I enabled the postgresql log level to DEBUG5, and it's also stopped to showing the progress after 2 to 3 minutes span of time. 

Can some advise me, where i'm doing mistake in the above source code.. And would like to know to why it's not functioning properlly .. And i'm also getting the following message when the above libpq program runs..

************* Message **************************
message type 0x31 arrived from server while idle
message type 0x32 arrived from server while idle
message type 0x6e arrived from server while idle
message type 0x43 arrived from server while idle
message type 0x5a arrived from server while idle

However, the below sample thread(i.e without libpq) programg is working fine witout any issues.  

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

void* thread1()
{
        while(1){
                printf("Thread1\n");
        }
}

void* thread2()
{
        while(1){
                printf("Thread2\n");
        }
}

int main()
{
        int status;
        pthread_t tid1,tid2;
        pthread_create(&tid1,NULL,&thread1,NULL);
        pthread_create(&tid2,NULL,&thread2,NULL);
        pthread_join(tid1,NULL);
        pthread_join(tid2,NULL);
        return 0;
}


Thanks in advance ... 

Best Regards,
Dinesh

Re: Threads With Libpq Issue

From
Craig Ringer
Date:
On 08/01/2012 05:19 PM, dinesh kumar wrote:
Respected All,

This is my first post in PG-Generals. If it is not the place for these kind of queries, then please guide me where i need to post. 

I have a quick question regarding "pthread" with PostgreSQL 9.0 Libpq. I'm facing a problem with "Pthread" and libpq. Please find the below program behavoiur.
You can't share a connection between threads unless it's protected by a lock that prevents more than one thread from using it at once.

Use multiple connections, each isolated to a single thread. See:

  http://www.postgresql.org/docs/9.1/static/libpq-threading.html

--
Craig Ringer

Re: Threads With Libpq Issue

From
Craig Ringer
Date:
Argh, disregard. Your post was duplicated, and this copy only just
reached me.

Re: Threads With Libpq Issue

From
dinesh kumar
Date:
Thanks Craig/Reid ..

Yes it was duplicated ... Sorry 

Regards,
Dinesh

On Thu, Aug 2, 2012 at 6:20 AM, Craig Ringer <ringerc@ringerc.id.au> wrote:
Argh, disregard. Your post was duplicated, and this copy only just reached me.