postgres=# \df generate_series
List of functions
Schema | Name | Result data type | Argument data types | Type
------------+-----------------+-----------------------------------+--------------------------------------------------------------------+--------
pg_catalog | generate_series | SETOF bigint | bigint, bigint | normal
pg_catalog | generate_series | SETOF bigint | bigint, bigint, bigint | normal
pg_catalog | generate_series | SETOF integer | integer, integer | normal
pg_catalog | generate_series | SETOF integer | integer, integer, integer | normal
pg_catalog | generate_series | SETOF numeric | numeric, numeric | normal
pg_catalog | generate_series | SETOF numeric | numeric, numeric, numeric | normal
pg_catalog | generate_series | SETOF timestamp without time zone | timestamp without time zone, timestamp without time zone, interval | normal
pg_catalog | generate_series | SETOF timestamp with time zone | timestamp with time zone, timestamp with time zone, interval | normal
(8 rows)
int 类型
1 不写步进时默认为1
test=# select generate_series(1, 10);
generate_series
-----------------
1
2
3
4
5
6
7
8
9
10
(10 rows)
设置步长
test=# select generate_series(1, 9,2 );
generate_series
-----------------
1
3
5
7
9
(5 rows)
如果step 是正数,而start 大于stop,那么返回零行。相反,如果step 是负数,start 小于stop,则返回零行。如果是NULL 输入,也产生零行。step 为零则是一个错误。
test=# select generate_series(5,1);
generate_series
-----------------
(0 rows)
test=# select generate_series(5,-1);
generate_series
-----------------
(0 rows)
start 大于stop,step 是负数
test=# select generate_series(5,1,-1);
generate_series
-----------------
5
4
3
2
1
(5 rows)
时间类型
test=# select generate_series(now(), now() + '7 days', '1 day');
generate_series
-------------------------------
2019-03-30 11:32:13.770565+08
2019-03-31 11:32:13.770565+08
2019-04-01 11:32:13.770565+08
2019-04-02 11:32:13.770565+08
2019-04-03 11:32:13.770565+08
2019-04-04 11:32:13.770565+08
2019-04-05 11:32:13.770565+08
2019-04-06 11:32:13.770565+08
(8 rows)
test=# select generate_series(to_date('20130403','yyyymmdd'), to_date('20130404','yyyymmdd'), '3 hours');
generate_series
------------------------
2013-04-03 00:00:00+08
2013-04-03 03:00:00+08
2013-04-03 06:00:00+08
2013-04-03 09:00:00+08
2013-04-03 12:00:00+08
2013-04-03 15:00:00+08
2013-04-03 18:00:00+08
2013-04-03 21:00:00+08
2013-04-04 00:00:00+08
(9 rows)
IP类型
test=# create table test(id int, ip_start inet, ip_stop inet);
CREATE TABLE
test=# insert into test values (1, '192.168.1.6', '192.168.1.10');
INSERT 0 1
test=# insert into test values (2, '192.168.2.16', '192.168.2.20');
INSERT 0 1
test=# insert into test values (3, '192.168.3.116', '192.168.3.120');
INSERT 0 1
test=# select * From test;
id | ip_start | ip_stop
----+---------------+---------------
1 | 192.168.1.6 | 192.168.1.10
2 | 192.168.2.16 | 192.168.2.20
3 | 192.168.3.116 | 192.168.3.120
(3 rows)
test=# select id, generate_series(0, ip_stop-ip_start)+ip_start as ip_new from test;
id | ip_new
----+---------------
1 | 192.168.1.6
1 | 192.168.1.7
1 | 192.168.1.8
1 | 192.168.1.9
1 | 192.168.1.10
2 | 192.168.2.16
2 | 192.168.2.17
2 | 192.168.2.18
2 | 192.168.2.19
2 | 192.168.2.20
3 | 192.168.3.116
3 | 192.168.3.117
3 | 192.168.3.118
3 | 192.168.3.119
3 | 192.168.3.120
(15 rows)