This page in other versions: Unsupported versions: 7.1 / 7.2 / 7.3 / 7.4



pg_select -- loop over the result of a query


pg_select conn commandString arrayVar procedure


pg_select submits a query (SELECT statement) to the PostgreSQL server and executes a given chunk of code for each row in the result. The commandString must be a SELECT statement; anything else returns an error. The arrayVar variable is an array name used in the loop. For each row, arrayVar is filled in with the row values, using the column names as the array indices. Then the procedure is executed.

In addition to the column values, the following special entries are made in the array:


A list of the column names returned by the query.


The number of columns returned by the query.


The current row number, starting at zero and incrementing for each iteration of the loop body.



The handle of the connection on which to execute the query.


The SQL query to execute.


An array variable for returned rows.


The procedure to run for each returned row.

Return Value



This examples assumes that the table table1 has columns control and name (and perhaps others):

pg_select $pgconn "SELECT * FROM table1;" array {
    puts [format "%5d %s" $array(control) $array(name)]

Submit correction

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.

Privacy Policy | About PostgreSQL
Copyright © 1996-2018 The PostgreSQL Global Development Group