PostgreSQL has a very useful built-in function, generate_series, which can be used to generate a series of filled data according to different rules.
I. Syntax
generate_series(start,stop) --int or bigintgenerate_series(start,stop,step) --int or bigintgenerate_series(start,stop, step interval) --timestamp or timestamp with time zone
II. Application Example
1. Int type. The default value is 1 postgres = # select generate_series (); generate_series ----------------- 1 2 3 4 5 6 7 8 9 10 (10 rows) postgres = # select generate_series (, 3); generate_series --------------- 1 4 7 10 (4 rows) postgres = # select generate_series (5, 1); generate_series --------------- (0 rows) postgres = # select generate_series (5, 1,-1); generate_series --------------- 5 4 3 2 1 (5 rows) 2. time type postgres = # select generate_series (now (), now () + '7 Day', '1 Day'); generate_series --------------------------- 22:12:40. 915368 + 08 22:12:40. 915368 + 08 22:12:40. 915368 + 08 22:12:40. 915368 + 08 22:12:40. 915368 + 08 22:12:40. 915368 + 08 22:12:40. 915368 + 08 22:12:40. 915368 + 08 (8 rows) postgres = # select generate_series (to_date ('201312', 'yyyymmdd'), to_date ('20140901', 'yyyymmdd '), '3 H '); generate_series ------------------------ 2012-08-27 00:00:00 + 08 2012-08-27 03:00:00 + 08 2012-08-27 06:00:00 + 08 2012-08-27 09:00:00 + 08 2012-08-27 12:00:00 + 08 2012-08-27 15:00:00 + 08 2012-08-27 18:00:00 + 08 2012-08-27 21:00:00 + 08 (9) rows) 3. IP type postgres = # create table t_kenyon (id int, ip_start inet, ip_end inet); CREATE TABLEpostgres = # insert into t_kenyon values (1, '123. 168.1.254 ', '2017. 168.2.5 '); INSERT 0 1 postgres = # insert into t_kenyon values (2, '2017. 168.2.254 ', '2017. 168.3.5 '); INSERT 0 1 postgres = # insert into t_kenyon values (3, '2017. 168.3.254 ', '2017. 168.4.5 '); INSERT 0 1 postgres = # select * from t_kenyon; id | ip_start | ip_end ---- + --------------- + ------------- 1 | 192.168.1.254 | 192.168.2.5 1 | 192.168.2.254 | 192.168.3.5 1 | 192.168.3.254 | 192.168.4.5 (3 rows) postgres = # select id, generate_series (0, ip_end-ip_start) + ip_start as ip_new from t_kenyon; id | ip_new ---- + ------------- 1 | 192.168.1.254 1 | 192.168.1.255 1 | 192.168.2.0 1 | 192.168.2.1 1 | 192.168.2.2 1 | 192.168.2.3 1 | 192.168.2.4 1 | 192.168.2.5 2 | 192.168.2.254 2 | 192.168.2.255 2 | 192.168.3.0 2 | 192.168.3.1 2 | 192.168.3.2 2 | 192.168.3.3 2 | 192.168.3.4 2 | 192.168.3.5 3 | 192.168.3.254 3 | 192.168.3.255 3 | 192.168.4.0 3 | 192.168.4.1 3 | 192.168.4.2 3 | 192.168.4.3 3 | 192.168.4.4 3 | 192.168.4.5 (24 rows)
Iii. Summary
The generate_series function of postgresql has many application scenarios for generating test data and batch updating data of certain rules. Proper use can improve development efficiency. In addition, IP sequence generation is also a highlight of PG.
Data cannot be generated in either of the following situations:
1. The step size is positive and the start value is greater than the end value.
2. The step size is negative and the start value is smaller than the end value.