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



pg_select -- loop over the result of a SELECT statement


pg_select dbHandle queryString arrayVar queryProcedure



Specifies a valid database handle.


Specifies a valid SQL select query.


Array variable for tuples returned.


Procedure run on each tuple found.




pg_select submits a SELECT query to the PostgreSQL backend, and executes a given chunk of code for each tuple in the result. The queryString must be a SELECT statement. Anything else returns an error. The arrayVar variable is an array name used in the loop. For each tuple, arrayVar is filled in with the tuple field values, using the field names as the array indexes. Then the queryProcedure is executed.

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


A list of the column names returned by the SELECT.


The number of columns returned by the SELECT.


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


This would work if table table has fields control and name (and, perhaps, other fields):

       pg_select $pgconn "SELECT * FROM table" 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