2 9.26. Set Returning Functions #
4 This section describes functions that possibly return more than one
5 row. The most widely used functions in this class are series generating
6 functions, as detailed in Table 9.69 and Table 9.70. Other, more
7 specialized set-returning functions are described elsewhere in this
8 manual. See Section 7.2.1.4 for ways to combine multiple set-returning
11 Table 9.69. Series Generating Functions
17 generate_series ( start integer, stop integer [, step integer ] ) →
20 generate_series ( start bigint, stop bigint [, step bigint ] ) → setof
23 generate_series ( start numeric, stop numeric [, step numeric ] ) →
26 Generates a series of values from start to stop, with a step size of
27 step. step defaults to 1.
29 generate_series ( start timestamp, stop timestamp, step interval ) →
32 generate_series ( start timestamp with time zone, stop timestamp with
33 time zone, step interval [, timezone text ] ) → setof timestamp with
36 Generates a series of values from start to stop, with a step size of
37 step. In the timezone-aware form, times of day and daylight-savings
38 adjustments are computed according to the time zone named by the
39 timezone argument, or the current TimeZone setting if that is omitted.
41 When step is positive, zero rows are returned if start is greater than
42 stop. Conversely, when step is negative, zero rows are returned if
43 start is less than stop. Zero rows are also returned if any input is
44 NULL. It is an error for step to be zero. Some examples follow:
45 SELECT * FROM generate_series(2,4);
53 SELECT * FROM generate_series(5,1,-2);
61 SELECT * FROM generate_series(4,3);
66 SELECT generate_series(1.1, 4, 1.3);
74 -- this example relies on the date-plus-integer operator:
75 SELECT current_date + s.a AS dates FROM generate_series(0,14,7) AS s(a);
83 SELECT * FROM generate_series('2008-03-01 00:00'::timestamp,
84 '2008-03-04 12:00', '10 hours');
98 -- this example assumes that TimeZone is set to UTC; note the DST transition:
99 SELECT * FROM generate_series('2001-10-22 00:00 -04:00'::timestamptz,
100 '2001-11-01 00:00 -05:00'::timestamptz,
101 '1 day'::interval, 'America/New_York');
103 ------------------------
104 2001-10-22 04:00:00+00
105 2001-10-23 04:00:00+00
106 2001-10-24 04:00:00+00
107 2001-10-25 04:00:00+00
108 2001-10-26 04:00:00+00
109 2001-10-27 04:00:00+00
110 2001-10-28 04:00:00+00
111 2001-10-29 05:00:00+00
112 2001-10-30 05:00:00+00
113 2001-10-31 05:00:00+00
114 2001-11-01 05:00:00+00
117 Table 9.70. Subscript Generating Functions
123 generate_subscripts ( array anyarray, dim integer ) → setof integer
125 Generates a series comprising the valid subscripts of the dim'th
126 dimension of the given array.
128 generate_subscripts ( array anyarray, dim integer, reverse boolean ) →
131 Generates a series comprising the valid subscripts of the dim'th
132 dimension of the given array. When reverse is true, returns the series
135 generate_subscripts is a convenience function that generates the set of
136 valid subscripts for the specified dimension of the given array. Zero
137 rows are returned for arrays that do not have the requested dimension,
138 or if any input is NULL. Some examples follow:
140 SELECT generate_subscripts('{NULL,1,NULL,2}'::int[], 1) AS s;
149 -- presenting an array, the subscript and the subscripted
150 -- value requires a subquery:
151 SELECT * FROM arrays;
158 SELECT a AS array, s AS subscript, a[s] AS value
159 FROM (SELECT generate_subscripts(a, 1) AS s, a FROM arrays) foo;
160 array | subscript | value
161 ---------------+-----------+-------
164 {100,200,300} | 1 | 100
165 {100,200,300} | 2 | 200
166 {100,200,300} | 3 | 300
169 -- unnest a 2D array:
170 CREATE OR REPLACE FUNCTION unnest2(anyarray)
171 RETURNS SETOF anyelement AS $$
173 from generate_subscripts($1,1) g1(i),
174 generate_subscripts($1,2) g2(j);
175 $$ LANGUAGE sql IMMUTABLE;
177 SELECT * FROM unnest2(ARRAY[[1,2],[3,4]]);
186 When a function in the FROM clause is suffixed by WITH ORDINALITY, a
187 bigint column is appended to the function's output column(s), which
188 starts from 1 and increments by 1 for each row of the function's
189 output. This is most useful in the case of set returning functions such
191 -- set returning function WITH ORDINALITY:
192 SELECT * FROM pg_ls_dir('.') WITH ORDINALITY AS t(ls,n);
194 -----------------+----