简体   繁体   English

使用generate_series()修改PL / pgSQL函数

[英]Modifying PL/pgSQL function using generate_series()

I am working on a version of PostgreSQL 8.3 that does not support the variant of the generate_series() functions for date/time series. 我正在研究PostgreSQL 8.3的一个版本,它不支持日期/时间序列的generate_series()函数的变体。 I have this ugly workaround at the moment that calls the function with a third argument like: 我现在有这个丑陋的解决方法,用第三个参数调用函数,如:

select table_union('2012-12-01', '2013-02-20', 79)

I have to compute the last parameter manually to determine the number of days for generate_series() . 我必须手动计算最后一个参数以确定generate_series()的天数。

What is the best way to modify this script so that I only need two arguments in the function call? 修改此脚本的最佳方法是什么,以便在函数调用中只需要两个参数?

Is there a way to modify the below code to work the same way only given two arguments to the function like this? 有没有办法修改下面的代码以同样的方式工作只给这个函数的两个参数?

select table_union('2012-12-01', '2013-02-20')
create or replace function table_union(date_from date, date_to date, numday int)
returns void language plpgsql as $$
declare
    day_1 date;
    _stop_ bigint := (date_from::date - date_to::date)::int; 
begin
    for day_1 in 
        select date_from + s.a as dates from generate_series(0, $3 ) as s(a)
    loop
        execute 'insert into dhcp.dhcp_map select * from dhcp.final_map_'|| trim( leading ' ' from to_char(extract(month from day_1), '09')) ||'_'|| 
        trim( leading ' ' from to_char(extract(day from day_1), '09'));  --to_char introduces a leadin space use trim to remove
    end loop;
end; $$;

Update: I tried modifying my code after the great suggestions in the answer below but still have some errors: 更新:我尝试修改我的代码后,在下面的答案中提出了很好的建议,但仍然有一些错误:


create or replace function 
table_union(date_from date, date_to date) 
returns void language plpgsql 
as $func$ 
declare day_1 date; 
begin 
for day_1 in select date_from
 + s.a as dates from generate_series(0, (date_to - date_from)) 
as s(a) 
loop 
execute 
'insert into dhcp.dhcp_map select * from dhcp.final_map_'||
 array_to_string(ARRAY(SELECT to_char(date_from + generate_series(0, (date_to - date_from)), 'MM_DD')) ) ; 
end loop;
 end; 
$func$;
CREATE OR REPLACE FUNCTION table_union(date_from date, date_to date)
  RETURNS void LANGUAGE plpgsql AS
$func$
BEGIN
   EXECUTE '
INSERT INTO dhcp.dhcp_map
SELECT * FROM dhcp.final_map_'
   || array_to_string(ARRAY(SELECT to_char(date_from
                      + generate_series(0, (date_to - date_from)), 'MM_DD')), '
UNION ALL
SELECT * FROM dhcp.final_map_'
   );
END
$func$;

Call: 呼叫:

SELECT table_union('2012-12-01', '2013-01-10');

Major points 主要观点

  • Most important for performance: instead of one INSERT statement per day I generate and execute a single INSERT statement for everything. 性能最重要的是:而不是一个INSERT 每天声明我生成并执行一个 INSERT语句中的一切。 With RETURNS text and RETURN in place of EXECUTE you can see the generated statement: 使用RETURNS textRETURN代替EXECUTE您可以看到生成的语句:

     INSERT INTO dhcp.dhcp_map SELECT * FROM dhcp.final_map_12_01 UNION ALL SELECT * FROM dhcp.final_map_12_02 UNION ALL SELECT * FROM dhcp.final_map_12_03 ... 

    ->sqlfiddle for Postgres 8.3. - > postlfre的sqlfiddle 8.3。
    As long as the amount of data to be inserted f9its into RAM, this is substantially faster. 只要将数据量插入到RAM中,这就会快得多。

  • If your INSERT should be huge you may want stick with one INSERT per day . 如果您的INSERT应该很大,您可能希望每天坚持使用一个INSERT

CREATE OR REPLACE FUNCTION table_union_huge(date_from date, date_to date)
  RETURNS void LANGUAGE plpgsql AS
$func$
BEGIN
FOR i IN 0 .. (date_to - date_from)
LOOP
   EXECUTE 'INSERT INTO dhcp.dhcp_map
SELECT * FROM dhcp.final_map_'|| (date_from + i)::text;
END LOOP;
END
$func$; 
  • Remove redundant parameter $3 from function call. 从函数调用中删除冗余参数$3 Replace with simple subtraction $2 - $1 . 替换为简单的减法$2 - $1 Returns an integer (difference in days) in Postgres. 返回Postgres中的整数(以天为单位)。

  • Simplified generate_series() call. 简化generate_series()调用。

  • Replaced LOOP with ARRAY constructor and array_to_string() to create a single statement. ARRAY构造函数array_to_string()替换LOOP以创建单个语句。

  • Largely simplified string handling. 大大简化了字符串处理。 Just use the pattern MM_DD with to_char() to extract your string from the date. 只需使用带有to_char()模式MM_DD从日期中提取字符串。

All Links to the manual of Postgres 8.3. 所有链接到Postgres手册8.3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM