From 5e8674dc83926f52516f847f1a77e8d38e94e143 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 4 Jan 2024 15:24:15 -0500 Subject: In plpgsql, allow %TYPE and %ROWTYPE to be followed by array decoration. This provides the useful ability to declare a variable that is an array of the type of some other variable or some table column. Quan Zongliang, Pavel Stehule Discussion: https://postgr.es/m/ec4523e1-9e7e-f3ef-f9ce-bafd680ad6f6@yeah.net --- doc/src/sgml/plpgsql.sgml | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml index 5977534a627..c2b9c6adb01 100644 --- a/doc/src/sgml/plpgsql.sgml +++ b/doc/src/sgml/plpgsql.sgml @@ -675,12 +675,14 @@ DECLARE Copying Types -variable%TYPE +name table.column%TYPE +name variable%TYPE - %TYPE provides the data type of a variable or - table column. You can use this to declare variables that will hold + %TYPE provides the data type of a table column + or a previously-declared PL/pgSQL + variable. You can use this to declare variables that will hold database values. For example, let's say you have a column named user_id in your users table. To declare a variable with the same data type as @@ -690,6 +692,21 @@ user_id users.user_id%TYPE; + + It is also possible to write array decoration + after %TYPE, thereby creating a variable that holds + an array of the referenced type: + +user_ids users.user_id%TYPE[]; +user_ids users.user_id%TYPE ARRAY[4]; -- equivalent to the above + + Just as when declaring table columns that are arrays, it doesn't + matter whether you write multiple bracket pairs or specific array + dimensions: PostgreSQL treats all arrays of + a given element type as the same type, regardless of dimensionality. + (See .) + + By using %TYPE you don't need to know the data type of the structure you are referencing, and most importantly, @@ -739,6 +756,12 @@ user_id users.user_id%TYPE; %ROWTYPE is more portable.) + + As with %TYPE, %ROWTYPE can be + followed by array decoration to declare a variable that holds an array + of the referenced composite type. + + Parameters to a function can be composite types (complete table rows). In that case, the -- cgit v1.2.3