首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >跨多个过程调用缓存getdate()时的红移问题

跨多个过程调用缓存getdate()时的红移问题
EN

Stack Overflow用户
提问于 2021-03-09 21:34:55
回答 1查看 59关注 0票数 0

我希望在一个过程的执行过程中有时间戳,我使用的是GETDATE(),但是调用过程的第二次,第三次等等,总是返回相同的时间戳,似乎红移在GETDATE()上做了某种缓存?

OK - getdate()在过程之外,正确地保持返回不同的时间戳:

代码语言:javascript
运行
复制
select getdate();
2021-03-09 13:03:18.0
select getdate();
2021-03-09 13:03:26.0

NOK - now将创建测试过程,捕获开始和结束时间戳(和大FOR循环进行暂停),并调用过程超过1次,我看到在第二次、第三次执行时返回相同的开始/结束值等。

代码语言:javascript
运行
复制
create or replace procedure tests.test_getdate_cache()
as $$ 
declare
    ts  timestamp;
    f   float;
begin
--    execute 'SET enable_result_cache_for_session TO OFF';
    
    ts:= getdate();
    raise info 'START - getdate()=[%]', ts;

    FOR i IN 1..10000000 LOOP
--        ts:= getdate();
--        raise info '**loop[%]  getdate()=[%]', i, ts;
        f:= i * 1234.4234;
    END LOOP;
    
    ts:= getdate();
    raise info 'END - getdate()=[%]', ts;

--    execute 'SET enable_result_cache_for_session TO ON';
end;
$$ LANGUAGE plpgsql;
/

call tests.test_getdate_cache();
START - getdate()=[2021-03-09 13:15:47]
END - getdate()=[2021-03-09 13:16:00

START - getdate()=[2021-03-09 13:15:47]       <---- DUPLICATED VALUE
END - getdate()=[2021-03-09 13:16:00]         <---- DUPLICATED VALUE

--cache results off
START - getdate()=[2021-03-09 13:23:22]
END - getdate()=[2021-03-09 13:23:34]

START - getdate()=[2021-03-09 13:23:22]       <---- DUPLICATED VALUE
END - getdate()=[2021-03-09 13:23:34]         <---- DUPLICATED VALUE


SELECT * FROM svl_stored_proc_messages WHERE 1=1 ORDER BY recordtime desc limit 4
userid | session_userid | pid   | xid     | query | recordtime          | loglevel | loglevel_text | message                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | linenum | querytxt                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             | label                                                                                                                                                                                                                                                                                                                            | aborted
-------+----------------+-------+---------+-------+---------------------+----------+---------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------
   101 |            101 | 31870 | 1540358 |  4332 | 2021-03-09 13:16:32 |       30 | INFO          | END - getdate()=[2021-03-09 13:16:00]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |      16 | call tests.test_getdate_cache()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | default                                                                                                                                                                                                                                                                                                                          |       0
   101 |            101 | 31870 | 1540358 |  4332 | 2021-03-09 13:16:19 |       30 | INFO          | START - getdate()=[2021-03-09 13:15:47]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |       7 | call tests.test_getdate_cache()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | default                                                                                                                                                                                                                                                                                                                          |       0

   101 |            101 | 31870 | 1540334 |  4325 | 2021-03-09 13:16:00 |       30 | INFO          | END - getdate()=[2021-03-09 13:16:00]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |      16 | call tests.test_getdate_cache()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | default                                                                                                                                                                                                                                                                                                                          |       0
   101 |            101 | 31870 | 1540334 |  4325 | 2021-03-09 13:15:47 |       30 | INFO          | START - getdate()=[2021-03-09 13:15:47]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |       7 | call tests.test_getdate_cache()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | default                                                                                                                                                                                                                                                                                                                          |       0
\

我甚至试图关闭结果缓存,尽管这可能只适用于当getdate()是函数时的SELECTs,但不管怎样,我尝试了一下,但同样的结果,时间戳总是出现相同的结果。只需取消上面几行的注释--执行'SET enable_result_cache_for_session TO OFF';--执行'SET enable_result_cache_for_session TO ON';

我知道如何在过程的早期获得新的时间戳以及何时退出吗?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2021-03-10 00:12:39

Getdate()返回当前的语句时间,由于存储过程调用是单个语句,因此在整个过程中它将返回相同的时间。请参阅:https://docs.aws.amazon.com/redshift/latest/dg/Date_functions_header.html

我不认为你正在尝试做的事情能(可靠地)工作。请记住,Redshift是一群联网的计算机,虽然它们的时钟是同步的,但这并不完美。每个节点的时钟略有不同,相互通信的网络时间也会影响比较。您的过程不是在一个地方运行,而是在多个地方运行。通过使用python UDF,您可能可以获得每个节点的实时信息,但是您必须从不同的结果中理清所看到的是什么。对于对UDF的单个调用,这可能是可行的,但当您将该过程应用于跨多个节点存储的实际数据时,您可能会看到许多冲突的答案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66547910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档