Re: libpq and multi-threading - Mailing list pgsql-general

From Michael J. Baars
Subject Re: libpq and multi-threading
Date
Msg-id CAMHx2Rs76akkVLTQ6Tr+fnVmZvwQfmM=iLJowW5ypePQDgbPDw@mail.gmail.com
Whole thread Raw
In response to Re: libpq and multi-threading  (Laurenz Albe <laurenz.albe@cybertec.at>)
Responses Re: libpq and multi-threading  ("Peter J. Holzer" <hjp-pgsql@hjp.at>)
List pgsql-general
Hello Laurenz,

I don't think it is, but let me shed some more light on it.

After playing around a little with threads and memory, I now know that the PGresult is not read-only, it is read-once. The child can only read that portion of parent memory, that was written before the thread started. Read-only is not strong enough.

Let me correct my first mail. Making libpq use mmap is not good enough either. Shared memory allocated by the child can not be accessed by the parent. I remembered right after pushing the send button. Shared memory needed by the child therefore has to be allocated through the parent.

In conclusion. I have found no way to pass the PGresult around, other than by copying it to shared memory. Rather disappointing. One store too many if you ask me. But passing PGresults around freely between threads, because they are supposingly read-only, is not a finding that I was able to reproduce from here.

On Tue, 2 May 2023, 15:49 Laurenz Albe, <laurenz.albe@cybertec.at> wrote:
On Tue, 2023-05-02 at 11:38 +0200, Michael J. Baars wrote:
> I have a question about libpq and multi-threading.
>
> In the PostgreSQL documentation (https://www.postgresql.org/docs/15/libpq-threading.html)
> it says that results can be passed around freely between threads. However, when I try to read
> the result from the parent thread, the program crashes with a segmentation fault.

That's too little information.

Yours,
Laurenz Albe

pgsql-general by date:

Previous
From: Laurenz Albe
Date:
Subject: Re: libpq and multi-threading
Next
From: "David G. Johnston"
Date:
Subject: Re: libpq and multi-threading