What is the effect of the second argument to generate_subscripts?

I am trying to understand the second argument to the PostgreSQL generate_subscripts function. The documentation states that the second argument is the size of the array over which indexes should be generated:

generate_subscripts is a convenience function that generates a set of valid indexes for a given dimension of a given array.

However, for my two-dimensional array of examples, providing arguments 1 or 2 , the same output is generated.

 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 ) generate the same output:

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

I am afraid that I do not understand the second argument to generate_subscripts . Can anyone with much experience figure out what this argument does?

I am running PostgreSQL 9.1.6.

+4
source share
1 answer

The second parameter is size:

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

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

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

+3
source

Source: https://habr.com/ru/post/1501577/


All Articles