What is the PostgreSQL equivalent to the TSQL “go” statement?
I have a query to insert a record into a table
--something like this
Insert into employee values(1,'Mike');
GO n;
I want this query to be executed n number of times.
You can write multiple psql commands and SQL statements in one text file (say, named statements. sql ), and then use the command psql congress -af statements. sql to execute them all. Use “ ; ” to signal the end of each SQL statement in the file.
Postgresql supports For loop statements to iterate through a range of integers or results from a sequence query. The For loop is used to iterate over a set of numbers or objects.
It can be used to replace single quotes enclosing string literals (constants) anywhere in SQL scripts. The body of a function happens to be such a string literal. Dollar-quoting is a PostgreSQL-specific substitute for single quotes to avoid escaping of nested single quotes (recursively).
In terms of business transactions, each business transactions is around 30-35 queries hitting the database. We are able to achieve ~ 150 business transactions with 4,500-5,000 QPS ( query per second ).
This is possible without reverting to PL/pgSQL:
Insert into employee (id, name)
select 1,'Mike'
from generate_series(1,3);
Or if you want different IDs for each row:
Insert into employee (id, name)
select id,'Mike'
from generate_series(1,3) as t(id);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With