Book Image

PostgreSQL Server Programming

Book Image

PostgreSQL Server Programming

Overview of this book

Table of Contents (21 chapters)
PostgreSQL Server Programming Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Passing and returning arrays


If you pass array types as an argument to the PL/Tcl function, they are passed as a string value, along with the brackets and the commas. Let's take a look at an example:

CREATE OR REPLACE FUNCTION tcl_array_test(integer[]) RETURNS int
AS $$
    set length [string length $1]
    return $length
$$ LANGUAGE pltcl;

testdb=# select tcl_array_test(ARRAY[1,2,3]);
 tcl_array_test 
----------------
       7
    (1 row)

You are probably surprised at the return value of the preceding function. You passed an integer array to the function that is converted to a string value {1,2,3}, the length of which is indeed 7. If you want to process array values independently, you need a bit of string manipulation to extract the list out of the string, do the manipulation, and convert it back to the string format that you received it in.

Let's take a look at an example PL/Tcl function that will reverse an integer array and return the reversed integer array:

CREATE OR REPLACE FUNCTION tcl_reverse_array...