Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the effect of the second argument to generate_subscripts?

I'm trying to understand the second argument to the PostgreSQL generate_subscripts function. The documentation states that the second argument is the dimension of the array over which subscripts should be generated:

generate_subscripts is a convenience function that generates the set of valid subscripts for the specified dimension of the given array.

However, for my two-dimensional example array, providing arguments 1 or 2 both generate the same output.

WITH data AS (
    select (array[['1','spam','3'], ['4','eggs','6'], ['7','ham','9']]) AS arr
)
SELECT arr[i][2] AS food
FROM data,
generate_subscripts((SELECT arr FROM data), 1) i;

and

WITH data AS (
    select (array[['1','spam','3'], ['4','eggs','6'], ['7','ham','9']]) AS arr
)
SELECT arr[i][2] AS food
FROM data,
generate_subscripts((SELECT arr FROM data), 2) i;

(Note 1 vs. 2) both generate the same output:

 food
------
 spam
 eggs
 ham
(3 rows)

I'm afraid that I don't understand the second argument to generate_subscripts. Can someone with more experience clarify what this argument does?

I'm running PostgreSQL 9.1.6.

like image 305
skyler Avatar asked Oct 03 '22 00:10

skyler


1 Answers

A second parameter is dimension:

postgres=# select * from generate_subscripts(array[[1,2,3],[3,4,5]],1);
 generate_subscripts 
---------------------
                   1
                   2
(2 rows)

postgres=# select * from generate_subscripts(array[[1,2,3],[3,4,5]],2);
 generate_subscripts 
---------------------
                   1
                   2
                   3
(3 rows)

In your example, both dimensions are same size, so generate_subscripts returns same result.

like image 127
Pavel Stehule Avatar answered Oct 09 '22 21:10

Pavel Stehule