Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a query multiple times in PostgreSQL

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.

like image 392
Sai Dileep Avatar asked Jul 21 '16 14:07

Sai Dileep


People also ask

How do I run multiple queries in PostgreSQL?

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.

Can you do loops in PostgreSQL?

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.

What is $$ in PostgreSQL?

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).

How many queries per second can Postgres handle?

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 ).


1 Answers

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);
like image 108
a_horse_with_no_name Avatar answered Sep 28 '22 14:09

a_horse_with_no_name