overriding current_timestamp

Lists: pgsql-hackers
From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: overriding current_timestamp
Date: 2012-02-23 11:08:51
Message-ID: 1329995331.6474.14.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

For (unit) testing, I have often had the need to override the current
timestamp in the database system. For example, a column default,
function, or views would make use of the current timestamp in some way,
and to test the behavior, it's sometimes useful to tweak the current
timestamp.

What might be a good way to do that?

Just overwrite xactStartTimestamp? Is that safe? If it weren't static,
a user-loaded function could do it.

Overwrite pg_catalog.now() in the test database?

Other ideas?

Some semi-official support for this sort of thing would be good.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: overriding current_timestamp
Date: 2012-02-23 14:41:42
Message-ID: 12854.1330008102@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Peter Eisentraut <peter_e(at)gmx(dot)net> writes:
> For (unit) testing, I have often had the need to override the current
> timestamp in the database system. For example, a column default,
> function, or views would make use of the current timestamp in some way,
> and to test the behavior, it's sometimes useful to tweak the current
> timestamp.

> What might be a good way to do that?

> Just overwrite xactStartTimestamp? Is that safe? If it weren't static,
> a user-loaded function could do it.

I think it's safe enough if you can figure out where/when to do it. Do
you need this to happen invisibly, or is it okay to require the test
script to call a set-the-timestamp function in each transaction?
If the former, it'd likely be necessary to hook into the transaction
start/end hooks.

> Overwrite pg_catalog.now() in the test database?

Yeah, that would work too if you'd rather do it at that end.

> Some semi-official support for this sort of thing would be good.

Mumble. It's not hard to think of applications where monkeying with the
system clock would amount to a security breach. So I'm not that excited
about providing a way to do it even semi-officially.

regards, tom lane


From: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: overriding current_timestamp
Date: 2012-02-23 18:54:00
Message-ID: C9C9169B-2464-4B6A-82C8-CE36B2C3337E@justatheory.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Feb 23, 2012, at 3:08 AM, Peter Eisentraut wrote:

> For (unit) testing, I have often had the need to override the current
> timestamp in the database system. For example, a column default,
> function, or views would make use of the current timestamp in some way,
> and to test the behavior, it's sometimes useful to tweak the current
> timestamp.
>
> What might be a good way to do that?
>
> Just overwrite xactStartTimestamp? Is that safe? If it weren't static,
> a user-loaded function could do it.
>
> Overwrite pg_catalog.now() in the test database?
>
> Other ideas?
>
> Some semi-official support for this sort of thing would be good.

I create a "mock" schema, add the function to it, and then put it in the search_path ahead of pg_catalog. See the example starting at slide 48 on http://www.slideshare.net/justatheory/pgtap-best-practices.

Best,

David


From: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: overriding current_timestamp
Date: 2012-02-23 18:55:53
Message-ID: 31380AE0-6818-48AC-8CF4-364D9257B783@justatheory.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Feb 23, 2012, at 10:54 AM, David E. Wheeler wrote:

> I create a "mock" schema, add the function to it, and then put it in the search_path ahead of pg_catalog. See the example starting at slide 48 on http://www.slideshare.net/justatheory/pgtap-best-practices.

Sorry, starting at slide 480.

David


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: overriding current_timestamp
Date: 2012-02-27 12:29:32
Message-ID: 1330345772.7111.25.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On tor, 2012-02-23 at 10:54 -0800, David E. Wheeler wrote:
> I create a "mock" schema, add the function to it, and then put it in the search_path ahead of pg_catalog.

That doesn't work for current_timestamp and similar built-in functions,
because they are always mapped to the pg_catalog schema.


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: overriding current_timestamp
Date: 2012-02-27 12:29:54
Message-ID: 1330345794.7111.26.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On tor, 2012-02-23 at 10:55 -0800, David E. Wheeler wrote:
> On Feb 23, 2012, at 10:54 AM, David E. Wheeler wrote:
>
> > I create a "mock" schema, add the function to it, and then put it in the search_path ahead of pg_catalog. See the example starting at slide 48 on http://www.slideshare.net/justatheory/pgtap-best-practices.
>
> Sorry, starting at slide 480.

That presentation only goes to slide 394.


From: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: overriding current_timestamp
Date: 2012-02-27 16:48:53
Message-ID: 77434680-D54C-4BA5-83A1-B2F1D4B74E1A@justatheory.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Feb 27, 2012, at 4:29 AM, Peter Eisentraut wrote:

>> I create a "mock" schema, add the function to it, and then put it in the search_path ahead of pg_catalog.
>
> That doesn't work for current_timestamp and similar built-in functions,
> because they are always mapped to the pg_catalog schema.

I use it for NOW() all the time.

David


From: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: overriding current_timestamp
Date: 2012-02-27 16:51:20
Message-ID: 0E379B0D-EEFA-45FB-AC60-23F760B8D338@justatheory.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Feb 27, 2012, at 4:29 AM, Peter Eisentraut wrote:

>> Sorry, starting at slide 480.
>
> That presentation only goes to slide 394.

Crimony, sorry, this presentation:

http://www.slideshare.net/justatheory/test-drivern-database-development

Note that I put pg_catalog at the end of the search_path, so that my mocked function will be found before it gets found in pg_catalog. If you don't add it to the end, it's implicitly the first item in the search path.

Best,

David


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: overriding current_timestamp
Date: 2012-02-27 19:39:27
Message-ID: 1330371567.7111.28.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On mån, 2012-02-27 at 08:48 -0800, David E. Wheeler wrote:
> On Feb 27, 2012, at 4:29 AM, Peter Eisentraut wrote:
>
> >> I create a "mock" schema, add the function to it, and then put it in the search_path ahead of pg_catalog.
> >
> > That doesn't work for current_timestamp and similar built-in functions,
> > because they are always mapped to the pg_catalog schema.
>
> I use it for NOW() all the time.

But it won't work for current_timestamp.


From: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: overriding current_timestamp
Date: 2012-02-27 19:40:55
Message-ID: 2B744299-D343-4077-8EAB-063A6EB120F5@justatheory.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Feb 27, 2012, at 11:39 AM, Peter Eisentraut wrote:

>> I use it for NOW() all the time.
>
> But it won't work for current_timestamp.

Why not? Not challenging your assertion here, just curious why it’s different.

David


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: overriding current_timestamp
Date: 2012-02-27 19:43:05
Message-ID: 1330371785.7111.30.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On mån, 2012-02-27 at 11:40 -0800, David E. Wheeler wrote:
> On Feb 27, 2012, at 11:39 AM, Peter Eisentraut wrote:
>
> >> I use it for NOW() all the time.
> >
> > But it won't work for current_timestamp.
>
> Why not? Not challenging your assertion here, just curious why it’s different.

Because it's not actually a function, it's hardcoded in the grammar to
call pg_catalog.now().


From: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: overriding current_timestamp
Date: 2012-02-27 19:44:03
Message-ID: FCAA10CA-E87D-4166-9FA1-16117EEA9AF7@justatheory.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Feb 27, 2012, at 11:43 AM, Peter Eisentraut wrote:

>> Why not? Not challenging your assertion here, just curious why it’s different.
>
> Because it's not actually a function, it's hardcoded in the grammar to
> call pg_catalog.now().

Ah, I see. Pity.

David


From: thehesiod <thehesiod(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: overriding current_timestamp
Date: 2020-11-05 00:39:55
Message-ID: 1604536795380-0.post@n3.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

so I used this method here:
https://gist.github.com/thehesiod/d0314d599c721216f075375c667e2d9a
<https://gist.github.com/thehesiod/d0314d599c721216f075375c667e2d9a> and
indeed does not work for current_timestamp and the like functions, and
there's another major problems, it doesn't seem to work for existing
triggers either, it seems like functions referenced by triggers are bound
when the function is created :(. I created an account to post this tid-bit
for others trying to achieve the same

--
Sent from: https://www.postgresql-archive.org/PostgreSQL-hackers-f1928748.html