【问题标题】:postgresql function timestamp parameterpostgresql 函数时间戳参数
【发布时间】:2017-10-17 04:06:25
【问题描述】:

这是我的第一个功能。使用固定常量手动执行选择查询时效果很好。但是在我的功能范围内,参数被错误地解释了。

基本上,我正在尝试用函数参数替换时间戳。

-- this my mananual query.  it works fine.
select  sum(least(end_time,'2017-05-16 11:30:00')- greatest(start_time,'2017-05-16 10:30:00')) as duration,
    from c_trunk 
    where (start_time < '2017-05-16 11:30:00') and (end_time > '2017-05-16 10:30:00')
    and trunk < 20

-- this is my bogus function with the select that calls it at the end. 
create function trunk_trafficD (p1 timestamp, p2 timestamp)
    returns boolean as $$
begin
select  sum(least(end_time,p2)- greatest(start_time,p1)) as duration,
    count(call_id), device as "system", trunk as "trunk" 
    from c_trunk 
    where (start_time < p2) and (end_time > p1)
    and trunk < 20
    and (device = 1 or device = 4 or device = 3)
return true;
end; $$
language PLPGSQL;

select trunk_trafficD('2017-05-16 10:30:00','2017-05-16 11:30:00');

【问题讨论】:

标签: postgresql function parameters timestamp


【解决方案1】:

你没有显示错误信息,但是在 PL/pgSQL 中你必须使用

SELECT expr1, expr2, ...
  INTO var1, var2, ...
FROM ...

或者如果你想丢弃结果,你可以使用PERFORM 而不是SELECT

【讨论】:

  • 您可以添加,现在当您可能想要返回数据集时,OP 函数总是返回 TRUE
  • 没有错误。手动命令在 10 秒内执行,而该函数运行在我的 4400 万条记录上。我认为“where”子句在函数中不能正常工作,因为参数没有被正确解释。
  • 使用auto_explain 模块与auto_explain.log_min_duration = 0auto_explain.log_nested_statements = on 将函数中SQL 语句的计划写入PostgreSQL 日志。您可以将这些计划添加到问题中,这将使案例更清楚。
【解决方案2】:

您需要为参数使用 to_timestamp() 函数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    相关资源
    最近更新 更多