Re: Concurrent Inserts

From: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "sri harsha *EXTERN*" <sriharsha9992(at)gmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Concurrent Inserts
Date: 2015-01-21 09:52:02
Message-ID: A737B7A37273E048B164557ADEF4A58B3658E2B5@ntex2010i.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

sri harsha wrote:
> Is there any way to stop concurrent inserts to happen on a single table ??
>
> Query 1 : INSERT INTO TABLE_A SELECT * FROM TABLE1;
> Query 2 : INSERT INTO TABLE_A SELECT * FROM TABLE2;
> Query 3 : SELECT * FROM TABLE_A;
>
> Assume i have the above queries. Query 1 and Query 3 can occur concurrently . If one insert is taking
> place , the other should wait. How do i achieve this ??

Is that a typo and you really mean "query 1 an query 2" above?

Why would you want to prevent concurrent inserts?
Maybe you don't really need to hobble your performance like that.

If you really want that, it is easy with table locks.
Your INSERT could look like that:

BEGIN;
LOCK table_a IN EXCLUSIVE MODE;
INSERT INTO table_a ...
COMMIT;

> How do the concurrent inserts take place in postgres ?? Is data stored temporarily for both queries
> separately and then written into the table ??

No, it is written to the table concurrently.
A table is organized in pages of 8KB each, and there is no problem with writing to
different pages concurrently.

Yours,
Laurenz Albe

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message sri harsha 2015-01-21 11:15:19 Re: Concurrent Inserts
Previous Message sri harsha 2015-01-21 09:36:12 Concurrent Inserts