pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Re: now() in loop statement
4+ messages / 4 participants
[nested] [flat]

* now() in loop statement
@ 2003-09-29 12:58 Kerv <keresztes_m@yahoo.com>
  1970-01-01 00:00 ` Re: now() in loop statement Christoph Haller <ch@rodos.fzk.de>
  2003-09-29 13:14 ` Re: now() in loop statement Tomasz Myrta <jasiek@klaster.net>
  2003-09-29 13:17 ` Re: now() in loop statement Dan Langille <dan@langille.org>
  0 siblings, 3 replies; 4+ messages in thread

From: Kerv @ 2003-09-29 12:58 UTC (permalink / raw)
  To: pgsql-sql

Hello,

What is wrong with this function because the mytime variable contain the 
same value in each iteration:
CREATE OR REPLACE FUNCTION public.time_test()
   RETURNS int4 AS
'
DECLARE
	count int4;
	mytime timestamp;
BEGIN
	count := 0;
	mytime := now();
	while count <= 25 loop
		mytime := now();
		RAISE NOTICE \'Time: %\',mytime;
		count := count + 1;
	end loop;
	RAISE NOTICE \'Count: %\',count;
	RETURN count;
END;
'
   LANGUAGE 'plpgsql' VOLATILE;

the result is:
NOTICE:  Time: 2003-09-29 15:52:06.745317
.
.
.
NOTICE:  Time: 2003-09-29 15:52:06.745317 --same with the first value!!!
NOTICE:  Count: 26

Thanks.




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: now() in loop statement
  2003-09-29 12:58 now() in loop statement Kerv <keresztes_m@yahoo.com>
@ 1970-01-01 00:00 ` Christoph Haller <ch@rodos.fzk.de>
  2 siblings, 0 replies; 4+ messages in thread

From: Christoph Haller @ 1970-01-01 00:00 UTC (permalink / raw)
  To: keresztes_m@yahoo.com; +Cc: pgsql-sql

> 
> Hello,
> 
> What is wrong with this function because the mytime variable contain the 
> same value in each iteration:
> CREATE OR REPLACE FUNCTION public.time_test()
>    RETURNS int4 AS
> '
> DECLARE
> 	count int4;
> 	mytime timestamp;
> BEGIN
> 	count := 0;
> 	mytime := now();
> 	while count <= 25 loop
> 		mytime := now();
> 		RAISE NOTICE \'Time: %\',mytime;
> 		count := count + 1;
> 	end loop;
> 	RAISE NOTICE \'Count: %\',count;
> 	RETURN count;
> END;
> '
>    LANGUAGE 'plpgsql' VOLATILE;
> 
> the result is:
> NOTICE:  Time: 2003-09-29 15:52:06.745317
> .
> .
> .
> NOTICE:  Time: 2003-09-29 15:52:06.745317 --same with the first value!!!
> NOTICE:  Count: 26
> 
> Thanks.
> 
Change 
mytime := now();
to 
mytime := ''now'';
and RTFM for details. 

Regards, Christoph 




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: now() in loop statement
  2003-09-29 12:58 now() in loop statement Kerv <keresztes_m@yahoo.com>
@ 2003-09-29 13:14 ` Tomasz Myrta <jasiek@klaster.net>
  2 siblings, 0 replies; 4+ messages in thread

From: Tomasz Myrta @ 2003-09-29 13:14 UTC (permalink / raw)
  To: Kerv <keresztes_m@yahoo.com>; +Cc: pgsql-sql

> Hello,
> 
> What is wrong with this function because the mytime variable contain the 
> same value in each iteration:

Just like it should be.
now() gives you always the same value inside single transaction. If you 
need time for performance tests - use "timeofday".

Regards,
Tomasz Myrta




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: now() in loop statement
  2003-09-29 12:58 now() in loop statement Kerv <keresztes_m@yahoo.com>
@ 2003-09-29 13:17 ` Dan Langille <dan@langille.org>
  2 siblings, 0 replies; 4+ messages in thread

From: Dan Langille @ 2003-09-29 13:17 UTC (permalink / raw)
  To: Kerv <keresztes_m@yahoo.com>; +Cc: pgsql-sql

On 29 Sep 2003 at 15:58, Kerv wrote:

> What is wrong with this function because the mytime variable contain the 
> same value in each iteration:

From http://www.postgresql.org/docs/7.3/static/functions-
datetime.html:

It is important to realize that CURRENT_TIMESTAMP and related 
functions return the start time of the current transaction; their 
values do not change during the transaction. timeofday() returns the 
wall clock time and does advance during transactions. 
-- 
Dan Langille : http://www.langille.org/




^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2003-09-29 13:17 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-09-29 12:58 now() in loop statement Kerv <keresztes_m@yahoo.com>
1970-01-01 00:00 ` Christoph Haller <ch@rodos.fzk.de>
2003-09-29 13:14 ` Tomasz Myrta <jasiek@klaster.net>
2003-09-29 13:17 ` Dan Langille <dan@langille.org>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox