PG_EXECUTE(n)
β¨ PG_EXECUTE β¨
okay so this is the pg_execute page from the PostgreSQL Tcl Interface Documentation and honestly? it's a glow-up of a function. bestie, let's get into it π
πΈ SYNOPSIS πΈ
pg_execute [-array arrayVar] [-oid oidVar] conn commandString [procedure]
that's it. that's the whole vibe. two optional flags, a connection, a query, and maybe a callback if you're feeling fancy π
π DESCRIPTION π
so pg_execute submits a command to the PostgreSQL server. that's the whole job. main character energy fr fr π₯
if the command is not a SELECT statement, what comes back is the number of rows affected by the command. and ooh, special case: if the command is an INSERT statement and a single row is inserted, then the OID of the inserted row gets stored in the variable oidVar β but only if you passed the optional -oid argument. no flag, no OID, that's the deal π
now if the command is a SELECT statement? this is where it gets good. for each row in the result:
- the row values get stored in the
arrayVarvariable (if you supplied it), using the column names as the array indices β cute, indexed, organized π - otherwise they go into variables named by the column names (old school, still slaying)
- and then the optional
proceduregets executed, if you supplied one β¨
side note bestie: omitting the procedure probably makes sense only if the query will return a single row. like, do the math. multiple rows + no procedure = you're just overwriting variables over and over π
the number of rows selected is what gets returned. iconic.
the procedure can use the Tcl commands break, continue, and return with the expected behavior β all the classics, working exactly how you'd expect π
β οΈ but watch out: if the procedure executesreturn, thenpg_executedoes not return the number of affected rows. the early exit ate the return value, no cap π
pg_execute is a newer function which provides a superset of the features of pg_select and can replace pg_exec in many cases where access to the result handle is not needed. so like, it's the upgraded version. the remake that actually slapped π
now, errors β because we gotta talk about errors:
for server-handled errors,pg_executewill throw a Tcl error and return a two-element list. first element = an error code, such asPGRES_FATAL_ERROR. second element = the server error text. two for one π«Ά
for more serious errors, such as failure to communicate with the server, pg_execute will throw a Tcl error and return just the error message text. no code, no list, just the raw text. that's the scary one π
π¦ ARGUMENTS π¦
-arrayarrayVar β specifies the name of an array variable where result rows are stored, indexed by the column names. π this gets ignored ifcommandStringis not aSELECTstatement β so don't come crying when nothing shows up.-oidoidVar β specifies the name of a variable into which the OID from anINSERTstatement will be stored. very niche, very valid π- conn β the handle of the connection on which to execute the command. your connection era π
- commandString β the SQL command to execute. the actual query, the star of the show β
- procedure β optional procedure to execute for each result row of a
SELECTstatement. run it per row, bestie, live your truth π§
π RETURN VALUE π
the number of rows affected or returned by the command. one number, very demure, very mindful π
π EXAMPLES π
in the following examples, error checking with catch has been omitted for clarity. (yes, unguarded. we're living dangerously π)
insert a row and save the OID in result_oid:
pg_execute -oid result_oid $pgconn "INSERT INTO mytable VALUES (1);"
print the columns item and value from each row:
pg_execute -array d $pgconn "SELECT item, value FROM mytable;" {
puts "Item=$d(item) Value=$d(value)"
}
find the maximum and minimum values and store them in $s(max) and $s(min):
pg_execute -array s $pgconn "SELECT max(value) AS max, min(value) AS min FROM mytable;"
find the maximum and minimum values and store them in $max and $min:
pg_execute $pgconn "SELECT max(value) AS max, min(value) AS min FROM mytable;"
see the difference? no -array, no array β the column names become plain variables. it's giving "same query, different storage" and honestly both ate πΈ
that's the whole page, bestie β go query something iconic πβ¨
π«Ά sign-off time: this lil masterpiece is dated 17 September 2020, from the PostgreSQL Tcl Interface Documentation. screenshot it, cite it, cite me, whatever β iykyk π
π¦ where this page comes from
- alpine 3.22: pgtcl-doc 3.1.0-r0 Β· BSD-3-Clause
- alpine 3.23: pgtcl-doc 3.1.1-r0 Β· BSD-3-Clause
- alpine 3.24: pgtcl-doc 3.1.1-r0 Β· BSD-3-Clause
- nixpkgs unstable: pgtcl 3.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms