Re: [HACKERS] Threads in PostgreSQL

2015-12-20 Thread Atri Sharma
On Mon, Dec 21, 2015 at 11:51 AM, sri harsha 
wrote:

> Hi,
>
>Is it possible to use threads in Postgresql ?? I am using threads in my
> foreign data wrapper and i get the following error when i use the threads .
>
> *ERROR:  stack depth limit exceeded*
> *HINT:  Increase the configuration parameter "max_stack_depth" (currently
> 2048kB), after ensuring the platform's stack depth limit is adequate.*
>
>

PostgreSQL is a process-per-backend model.

Can you elaborate on what you are using the threads for?


Re: [HACKERS] Threads in PostgreSQL

2015-12-20 Thread Chapman Flack
On 12/21/15 01:24, Atri Sharma wrote:
> On Mon, Dec 21, 2015, sri harsha  wrote:
> 
>> I am using threads in my
>> foreign data wrapper and i get the following error when i use the threads .
>>
>> *ERROR:  stack depth limit exceeded*
>> *HINT:  Increase the configuration parameter "max_stack_depth" (currently
>> 2048kB), after ensuring the platform's stack depth limit is adequate.*

> PostgreSQL is a process-per-backend model.

To elaborate on that, it is a process per backend and most of the code
in that process has not been written for safe execution by multiple
threads. If your code starts other threads that only do other things,
but only one ever touches PostgreSQL backend code, that can be ok.
This happens in PL/Java, but it uses several interrelated precautions
to make sure no thread ever enters PostgreSQL backend code unless every
other thread is known to be out.

Even if you are taking that precaution, if the backend code is entered
by a different thread than last executed there, the stack depth tests
may be made by comparing the last thread's stack base to the current
thread's stack pointer, which will naturally give you a bogus result.
There is some API in miscadmin.h for manipulating the backend's idea
of the stack base, but there be dragons.

I am far short of the knowledgeable voices here, but in case you don't
hear from them right away, that is at least how I understand the matter.

Chapman Flack


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] Threads in PostgreSQL

2015-12-20 Thread Konstantin Knizhnik

Hi,

PostgreSQL is not using threads but it is possible to spawn thread in 
your PostgreSQL extensions.

For example, I have used pool of threads in my IMCS extension.
But you need to build your  extension with -pthread:

CUSTOM_COPT = -pthread

Also, please take in account that many PostgreSQL functions (even in/out 
or comparison functions) are not reentrant: them are storing their state 
in global variables.
So you will get race conditions if you are calling  such functions from 
multiple threads.


Concerning stack overflow, I think that the most probable reason is 
trivial infinite recursion.

Did you inspect stack trace in debugger?






On 21.12.2015 09:21, sri harsha wrote:

Hi,

   Is it possible to use threads in Postgresql ?? I am using threads 
in my foreign data wrapper and i get the following error when i use 
the threads .


*ERROR:  stack depth limit exceeded*
*HINT:  Increase the configuration parameter "max_stack_depth" 
(currently 2048kB), after ensuring the platform's stack depth limit is 
adequate.*


No matter how much i increase the stack size , the error keeps 
occurring . How do i solve this problem ??



Thanks,
Harsha