Traversal of PostgreSQL arrays

Source: Internet
Author: User
Tags foreach arrays postgresql


PostgreSQL provides an array type. I'll show you how to use it specifically.
Creates a table with an array type field.


Create table test_array(id serial primary key, str1 int[][][]);
 
 
Insert two test data.
Insert into test_array values (1,array[[[,2],[3,4],[5,6]],[[20,30],[40,50],[70,100]]]);
Insert into test_array values (2,array[[[100,200],[300,400],[500,600]],[[2000,3000],[4000,5000],[7000,10000]]]);

Traversing without sharding,


Create or replace function sp_array2table_simple(
Anyarray
)
Returns table (element int) as
$ytt$
Declare array1 alias for $1;
x int;
Begin
   Drop table if exists tmp_1;
   Create temporary table tmp_1 (id int);
  
   <<label1>> foreach x in array array1
   Loop
     Insert into tmp_1 values (x);
   End loop label1;
  
   Return query select * from tmp_1;
End;
$ytt$ language plpgsql;
 
 
T_girl=#select sp_array2table_simple(str1) as array_list from test_array where id = 2;
 
 
  Array_list
------------
         100
         200
         300
         400
         500
         600
        2000
        3000
        4000
        5000
        7000
       10000
(12 lines of records)
 
 
 
 
Time: 7.780 ms


Traversal with fragmentation:


?
Create or replace function sp_array2table(
Anyarray
)
Returns table (element int) as
$ytt$
Declare array1 alias for $1;
x int[];
Nlen int := 0;
i int := 1;
Begin
   Drop table if exists tmp_1;
   Create temporary table tmp_1 (id int);
  
   <<label1>> foreach x slice 1 in array array1
     Loop
       Nlen := array_length(x,1);
       i := 1;
       <<label2>> while i <= nlen loop
         Insert into tmp_1 values (x[i]);
         i := i + 1;
       End loop label2;
   End loop label1;
  
   Return query select * from tmp_1;
End;
$ytt$ language plpgsql;
 
 
T_girl=#select sp_array2table(str1) as array_list from test_array where id = 2;
 
 
  Array_list
------------
         100
         200
         300
         400
         500
         600
        2000
        3000
        4000
        5000
        7000
       10000
(12 lines of records)
 
 
 
 
Time: 20.139 ms


There is a system of several functions, directly to traverse,


T_girl=#select unnest(str1) as array_list from test_array where id = 2;
 
 
  Array_list
------------
         100
         200
         300
         400
         500
         600
        2000
        3000
        4000
        5000
        7000
       10000
(12 lines of records)
 
 
 
 
Time: 1.002 ms


For example, array_to_string and so on.

T_girl=#select regexp_split_to_table(array_to_string(str1,','),',+') as array_list from test_array where id = 2;
 
 
  Array_list
------------
  100
  200
  300
  400
  500
  600
  2000
  3000
  4000
  5000
  7000
  10000
(12 lines of records)
 
 
 
 
Time: 0.850 ms
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.