snippetsqlMinor
How to execute two transactions concurrently in PostgreSQL?
Viewed 0 times
postgresqltwohowtransactionsexecuteconcurrently
Problem
I want to execute two transactions concurrently in pgAdmin:
Both parts were written in the same SQL Editor. My first problem is that it doesn't compile, I obtain the message:
--The first transaction should modify the age of Paul
BEGIN;
DECLARE newage INTEGER;
--the original age of Paul is 32
newage := Select age FROM COMPANY WHERE name = Paul;
newage := newage+10;
update company set age := Sage where name = Paul;
commit;
--The second Transaction shows all information about Paul including his age
Begin
select * from company where name = 'Paul';
commit;Both parts were written in the same SQL Editor. My first problem is that it doesn't compile, I obtain the message:
ERROR: syntax error at or near "INTEGER"
LINE 2: DECLARE newage INTEGER;Solution
Each pgAdmin query window has it's own session. You can run one transaction at a time in each session. It's not possible to run two transactions concurrently in the same session (at least not in pgAdmin or psql).
You can open and bind more sessions to the same query window with the "connection bar" in the menu of the query window, then switch between multiple open sessions. But it's simpler to just open another query window.
pgAdmin runs everything you execute at once inside a transaction. It's wrapped into
Start a transaction start with
Similar answer with detailed instructions on SO:
As for your code and error message:
You are using PL/pgSQL code, the default procedural language extension in Postgres (similar to PL/SQL in Oracle). It's only valid in a function or anonymous code block with the
A code block can only be executed as a whole.
You can open and bind more sessions to the same query window with the "connection bar" in the menu of the query window, then switch between multiple open sessions. But it's simpler to just open another query window.
pgAdmin runs everything you execute at once inside a transaction. It's wrapped into
BEGIN; ... COMMIT; implicitly - unless you issue explicit transaction control commands.Start a transaction start with
BEGIN; followed by SQL commands and don't commit yet. Switch to the other query window and do the same. Now both transactions are running concurrently. Some effects don't show until you COMMIT; one or both transactions.Similar answer with detailed instructions on SO:
- How to simulate deadlock in PostgreSQL?
As for your code and error message:
You are using PL/pgSQL code, the default procedural language extension in Postgres (similar to PL/SQL in Oracle). It's only valid in a function or anonymous code block with the
DO command.A code block can only be executed as a whole.
Context
StackExchange Database Administrators Q#102343, answer score: 4
Revisions (0)
No revisions yet.