Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mSp6r-0002fX-Jq for pgsql-general@arkaria.postgresql.org; Tue, 21 Sep 2021 23:23:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mSp6p-0001yx-NO for pgsql-general@arkaria.postgresql.org; Tue, 21 Sep 2021 23:23:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mSp6p-0001yp-AA for pgsql-general@lists.postgresql.org; Tue, 21 Sep 2021 23:23:31 +0000 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mSp6k-0002Gs-Ly for pgsql-general@lists.postgresql.org; Tue, 21 Sep 2021 23:23:30 +0000 Received: by mail-pj1-x1030.google.com with SMTP id v19so724058pjh.2 for ; Tue, 21 Sep 2021 16:23:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; h=from:mime-version:subject:message-id:date:to; bh=QxMzWP++n95m+y6aTV7vcg5Q0ntUY+kyLkdXjNwJbx8=; b=ie0c7aY7ML3vEI4S9RO0LVCfGmsyLAmT5Su4UoCI7CoIP+JaS0aLphDpmx3QzhltDL 8dz01CDGTlSF4xQiTBzk0MQfP2xmKx9iZrEjRJu54h2tmZ+cvMjEYnOkoPOxP1yASovg UxVGhqUyPllLgu9SyDHauTrWh7eOoXZ0HMX1o= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:mime-version:subject:message-id:date:to; bh=QxMzWP++n95m+y6aTV7vcg5Q0ntUY+kyLkdXjNwJbx8=; b=oIIlozkEQMBQ7+yf2PBgrHs78lichYkVZEf7dOxz4xJpfKtDv4M5Ddny7kXhecS/Xn jJl5KL6GoM/oLbOqt2YnXqomwZecE8R5Pjvn2XHu1GDm0zipDKjw7eew6MJGEFWWBJ1i eZZS5sb4vHHj4iPu20Tmbfpxws+S6ry3lLiNuTq0EEpK9ihTYIXYuqftKjN3xLmCOHOR e6KcL6z3Ns39GHaGliNvH3M0wVtXxQf8eTLyOH9nhEoWdp+YWhugY9VSXw16KiV9r7tu wJ7F5LmoWImfWNphzFQQwWx+DzBANhmBgMkWWUFahPJc1DAT1Q3EZnbp/MufYrCLBHkm YCGQ== X-Gm-Message-State: AOAM531CUjPQtOXi4ynFkJgoGD76RsjKhW67U8XcMZrvATI6kWm76+1M S5/Kz5CHGluyuznlyd7vklr9YdmCseAouA== X-Google-Smtp-Source: ABdhPJxcO2uPiq1JthZ/YgppGH6LCP7psG2sQR1yMlL+IAVKcgDk+K7zIbx5HF3dKxsMwfu53xOw2Q== X-Received: by 2002:a17:90b:4b47:: with SMTP id mi7mr8059956pjb.198.1632266603816; Tue, 21 Sep 2021 16:23:23 -0700 (PDT) Received: from smtpclient.apple ([2601:646:101:7fda:7c94:a956:d32b:90ea]) by smtp.gmail.com with ESMTPSA id x19sm204839pfa.104.2021.09.21.16.23.22 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Sep 2021 16:23:23 -0700 (PDT) From: Bryn Llewellyn Content-Type: multipart/alternative; boundary="Apple-Mail=_D225E347-206F-4A7B-8920-566BFDEB43C2" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: Surprising results from tests intended to show the difference in semantics between transaction_timestamp(), statement_timestamp(), and clock_timestamp() Message-Id: <6FCF660B-F151-4AA0-9AE5-C579591EF134@yugabyte.com> Date: Tue, 21 Sep 2021 16:23:22 -0700 To: pgsql-general list X-Mailer: Apple Mail (2.3654.120.0.1.13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_D225E347-206F-4A7B-8920-566BFDEB43C2 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 I'm surprised by the results that I describe below. Please help me = interpret them. Briefly, why does transaction_timestamp() report a later value than = statement_timestamp() when they're both invoked in the very first = statement after "start transaction". (They report identical values in an = implicitly started txn.) =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 = =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 = =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 = =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 *Introduction* I'm presently documenting (in the YugabyteDB YSQL section) the various = PG date-time functions that report the "current" moment. There are two = degrees of freedom: the return data type; and the kind of moment. This = would suggest that there's eighteen differently-named functions = (considering also timetz=E2=80=94which the PG doc recommends against = using). There are, in fact, far fewer. Never mind, timestamptz is almost = always the best choice for the data type of the moment; and the three = functions that I name in my "subject" return that data type. I thought that this demo would be simple and convincing: -- SINGLE STATEMENT TEST (IMPLICIT TXN) select transaction_timestamp() as txn_11, statement_timestamp() as stm_11, clock_timestamp() as clk_11, pg_sleep(:slp_1) as dummy, transaction_timestamp() as txn_12, statement_timestamp() as stm_12, clock_timestamp() as clk_12; -- TWO STATEMENT TEST (EXPLICIT TXN) start transaction; select transaction_timestamp() as txn_11, statement_timestamp() as stm_11, clock_timestamp() as clk_11, pg_sleep(:slp_1) as dummy, transaction_timestamp() as txn_12, statement_timestamp() as stm_12, clock_timestamp() as clk_12; select pg_sleep(:slp_2); select transaction_timestamp() as txn_2, statement_timestamp() as stm_2, clock_timestamp() as clk_2; commit; The output from this takes a huge mental effort to comprehend. So I = wrote a naive reporting function: function expected_tstz_diff_error( caption in text, t_finish in timestamptz, t_start in timestamptz, expected_diff in double precision) returns text I test what I get like this: -- SINGLE STATEMENT TEST (IMPLICIT TXN) select expected_tstz_diff_error('txn_12 - txn_11 =3D 0', = :s1_txn_12, :s1_txn_12, :zero); select expected_tstz_diff_error('stm_12 - stm_11 =3D 0', = :s1_stm_12, :s1_stm_12, :zero); select expected_tstz_diff_error('txn_11 - stm_11 =3D 0', = :s1_txn_11, :s1_stm_11, :zero); select expected_tstz_diff_error('clk_12 - clk_11 =3D slp_1', = :s1_clk_12, :s1_clk_11, :slp_1); and this: -- TWO STATEMENT TEST (EXPLICIT TXN) -- Within the first "select". select expected_tstz_diff_error('txn_12 - txn_11 =3D 0', = :s1_txn_12, :s1_txn_12, :zero); select expected_tstz_diff_error('stm_12 - stm_11 =3D 0', = :s1_stm_12, :s1_stm_12, :zero); select expected_tstz_diff_error('txn_11 - stm_11 =3D 0', = :s1_txn_11, :s1_stm_11, :zero); select expected_tstz_diff_error('clk_12 - clk_11 =3D slp_1', = :s1_clk_12, :s1_clk_11, :slp_1); -- Within the trasaction but across the first and the second "select". select expected_tstz_diff_error('txn_2 - txn_11 =3D 0', = :s2_txn_2, :s1_txn_11, :zero); select expected_tstz_diff_error('stm_2 - stm_11 =3D slp_1 + slp_2', = :s2_stm_2, :s1_stm_11, (:slp_1 + :slp_2)); select expected_tstz_diff_error('clk_2 - clk_11 =3D slp_1 + slp_2', = :s2_clk_2, :s1_clk_11, (:slp_1 + :slp_2)); The function reports either "exact" or the error in milliseconds. The runnable code is copied at the very end, after my account of the = problem. You can guess that there's a malarkey of quoting with \gset to = populate all those psql "substitution variables". But it all works. The = design concept is informed by these ideas: =E2=80=94 I can't use a PL/pgSQL encapsulation because, with this, it's = all regarded as a single top-level statement and so I can't easily = demonstrate the difference in semantics between transaction_timestamp() = and statement_timestamp(). =E2=80=94 I want to get my results back with the minimum of = time-consuming fuss to get the most reliable timings. So I do the = reporting after I have all the results to hand in psql "substitution = variables". *Results* Here's what I get with my PG Version 13.4 installation on my MacBook. (I = removed blank lines to improve the readability.) SINGLE STATEMENT TEST (IMPLICIT TXN) txn_12 - txn_11 =3D 0 : exact stm_12 - stm_11 =3D 0 : exact txn_11 - stm_11 =3D 0 : exact clk_12 - clk_11 =3D slp_1 : error: 0.1 ms TWO STATEMENT TEST (EXPLICIT TXN) txn_12 - txn_11 =3D 0 : exact stm_12 - stm_11 =3D 0 : exact txn_11 - stm_11 =3D 0 : error: -0.1 ms clk_12 - clk_11 =3D slp_1 : error: 0.6 ms txn_2 - txn_11 =3D 0 : exact stm_2 - stm_11 =3D slp_1 + slp_2 : error: 1.6 ms clk_2 - clk_11 =3D slp_1 + slp_2 : error: 1.6 ms *Discussion* The stand-alone =E2=80=9Cselect" that implicitly starts the txn gives = the results that I'd expect. I expected that, using the explicitly started txn, the values returned = by transaction_timestamp() and statement_timestamp() in the first = statement would be identical (just as they are with the implicitly = started txn) because (as I believe) it's the first substantive SQL = statement, and not "set transaction", that actually starts the txn. = Else, if "set transaction" really does start the txn, then = statement_timestamp() should return a later value than = transaction_timestamp(). In fact, it's the other way round. *Any thoughts on why?* If I can explain why, with the explicitly started txn, the values from = transaction_timestamp() and statement_timestamp() differ in the first = "select", then, I suppose, there are no more mysteries in the results = from the second "select" because "commit" is bound to take time and I'm = not using an estimate for this in my value for "expected_diff". =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 = =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 = =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 = =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 create function expected_tstz_diff_error( caption in text, t_finish in timestamptz, t_start in timestamptz, expected_diff in double precision) returns text language plpgsql as $body$ declare dp_zero constant double precision not null :=3D 0; dp_1000 constant double precision not null :=3D 1000; err constant double precision not null :=3D ( (extract(epoch from t_finish) - extract(epoch from = t_start)) - expected_diff )*dp_1000; x constant text not null :=3D rpad(caption, 30)||' : '; r text not null :=3D x||'exact'; begin if err <> dp_zero then r :=3D x||'error:'||to_char(err, '90.9')||' ms'; end if; return r; end; $body$; -- self-doc \set AUTOCOMMIT 'on' \set zero 0 \set slp_1 1 \set slp_2 2 \t on \o pg-local.txt -- =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D select 'SINGLE STATEMENT TEST (IMPLICIT TXN)'; ---------------------------------------------------------------------- select ''''''''||transaction_timestamp() ::text||'''''''' as txn_11, ''''''''||statement_timestamp() ::text||'''''''' as stm_11, ''''''''||clock_timestamp() ::text||'''''''' as clk_11, ''''''''||pg_sleep(:slp_1) ::text||'''''''' as dummy, ''''''''||transaction_timestamp() ::text||'''''''' as txn_12, ''''''''||statement_timestamp() ::text||'''''''' as stm_12, ''''''''||clock_timestamp() ::text||'''''''' as clk_12 \gset s1_ select expected_tstz_diff_error('txn_12 - txn_11 =3D 0', = :s1_txn_12, :s1_txn_12, :zero); select expected_tstz_diff_error('stm_12 - stm_11 =3D 0', = :s1_stm_12, :s1_stm_12, :zero); select expected_tstz_diff_error('txn_11 - stm_11 =3D 0', = :s1_txn_11, :s1_stm_11, :zero); select expected_tstz_diff_error('clk_12 - clk_11 =3D slp_1', = :s1_clk_12, :s1_clk_11, :slp_1); -- =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D select 'TWO STATEMENT TEST (EXPLICIT TXN)'; ---------------------------------------------------------------------- start transaction; select ''''''''||transaction_timestamp() ::text||'''''''' as txn_11, ''''''''||statement_timestamp() ::text||'''''''' as stm_11, ''''''''||clock_timestamp() ::text||'''''''' as clk_11, ''''''''||pg_sleep(:slp_1) ::text||'''''''' as dummy, ''''''''||transaction_timestamp() ::text||'''''''' as txn_12, ''''''''||statement_timestamp() ::text||'''''''' as stm_12, ''''''''||clock_timestamp() ::text||'''''''' as clk_12 \gset s1_ select pg_sleep(:slp_2); select ''''''''||transaction_timestamp() ::text||'''''''' as txn_2, ''''''''||statement_timestamp() ::text||'''''''' as stm_2, ''''''''||clock_timestamp() ::text||'''''''' as clk_2 \gset s2_ commit; -- Within the first "select". select expected_tstz_diff_error('txn_12 - txn_11 =3D 0', = :s1_txn_12, :s1_txn_12, :zero); select expected_tstz_diff_error('stm_12 - stm_11 =3D 0', = :s1_stm_12, :s1_stm_12, :zero); select expected_tstz_diff_error('txn_11 - stm_11 =3D 0', = :s1_txn_11, :s1_stm_11, :zero); select expected_tstz_diff_error('clk_12 - clk_11 =3D slp_1', = :s1_clk_12, :s1_clk_11, :slp_1); -- Within the trasaction but across the first and the second "select". select expected_tstz_diff_error('txn_2 - txn_11 =3D 0', = :s2_txn_2, :s1_txn_11, :zero); select expected_tstz_diff_error('stm_2 - stm_11 =3D slp_1 + slp_2', = :s2_stm_2, :s1_stm_11, (:slp_1 + :slp_2)); select expected_tstz_diff_error('clk_2 - clk_11 =3D slp_1 + slp_2', = :s2_clk_2, :s1_clk_11, (:slp_1 + :slp_2)); \o \t off --Apple-Mail=_D225E347-206F-4A7B-8920-566BFDEB43C2 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
I'm surprised by the = results that I describe below. Please help me interpret them.

Briefly, why = does transaction_timestamp() report a later value = than statement_timestamp() when they're both invoked in the very = first statement after "start transaction". (They report identical values = in an implicitly started txn.)
=E2=80=94 =E2=80=94 =E2=80=94= =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 = =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80= =94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80= =94 =E2=80=94 =E2=80=94

*Introduction*

I'm presently = documenting (in the YugabyteDB YSQL section) the various PG date-time = functions that report the "current" moment. There are two degrees of = freedom: the return data type; and the kind of moment. This would = suggest that there's eighteen differently-named = functions (considering also timetz=E2=80=94which the PG doc = recommends against using). There are, in fact, far fewer. Never mind, = timestamptz is almost always the best choice for the data type of = the moment; and the three functions that I name in my "subject" = return that data type.

I thought that this = demo would be simple and convincing:

-- SINGLE STATEMENT TEST (IMPLICIT = TXN)

select
  transaction_timestamp() as txn_11,
  statement_timestamp()   as = stm_11,
  clock_timestamp()     =   as clk_11,
  pg_sleep(:slp_1)  =       as dummy,
  transaction_timestamp() as txn_12,
  statement_timestamp()   as = stm_12,
  clock_timestamp()     =   as clk_12;

-- TWO STATEMENT = TEST (EXPLICIT TXN)

start transaction;

select
  transaction_timestamp() as txn_11,
  statement_timestamp()   as = stm_11,
  clock_timestamp()     =   as clk_11,
  pg_sleep(:slp_1)  =       as dummy,
  transaction_timestamp() as txn_12,
  statement_timestamp()   as = stm_12,
  clock_timestamp()     =   as clk_12;

select = pg_sleep(:slp_2);

select
  transaction_timestamp() as txn_2,
  statement_timestamp()   as = stm_2,
  clock_timestamp()     =   as clk_2;

commit;

The output from this takes a = huge mental effort to comprehend. So I wrote a naive reporting = function:

function expected_tstz_diff_error(
  caption        in = text,
  t_finish     =   in timestamptz,
  t_start  =       in timestamptz,
  expected_diff  in double precision)
  returns         =   text

I test = what I get like this:

-- SINGLE STATEMENT TEST (IMPLICIT TXN)
select expected_tstz_diff_error('txn_12 - txn_11 =3D = 0',            :s1_txn_12, = :s1_txn_12, :zero);
select = expected_tstz_diff_error('stm_12 - stm_11 =3D 0',      =       :s1_stm_12, :s1_stm_12, :zero);
select expected_tstz_diff_error('txn_11 - stm_11 =3D = 0',            :s1_txn_11, = :s1_stm_11, :zero);
select = expected_tstz_diff_error('clk_12 - clk_11 =3D slp_1',    =     :s1_clk_12, :s1_clk_11, :slp_1);

and this:

-- TWO STATEMENT = TEST (EXPLICIT TXN)

-- Within the first = "select".
select expected_tstz_diff_error('txn_12 - txn_11 = =3D 0',            :s1_txn_12, = :s1_txn_12, :zero);
select = expected_tstz_diff_error('stm_12 - stm_11 =3D 0',      =       :s1_stm_12, :s1_stm_12, :zero);
select expected_tstz_diff_error('txn_11 - stm_11 =3D = 0',            :s1_txn_11, = :s1_stm_11, :zero);
select = expected_tstz_diff_error('clk_12 - clk_11 =3D slp_1',    =     :s1_clk_12, :s1_clk_11, :slp_1);

-- Within the trasaction but across the first and the second = "select".
select expected_tstz_diff_error('txn_2 - txn_11 = =3D 0',           =   :s2_txn_2,  :s1_txn_11, :zero);
select= expected_tstz_diff_error('stm_2 - stm_11 =3D slp_1 + slp_2', = :s2_stm_2,  :s1_stm_11, (:slp_1 + :slp_2));
select= expected_tstz_diff_error('clk_2 - clk_11 =3D slp_1 + slp_2', = :s2_clk_2,  :s1_clk_11, (:slp_1 + :slp_2));

The function reports either = "exact" or the error in milliseconds.

The = runnable code is copied at the very end, after my account of the = problem. You can guess that there's a malarkey of quoting = with \gset to populate all those psql "substitution variables". But = it all works. The design concept is informed by these ideas:

=E2=80=94 I can't use a PL/pgSQL encapsulation = because, with this, it's all regarded as a single top-level statement = and so I can't easily demonstrate the difference in semantics = between transaction_timestamp() and statement_timestamp().

=E2=80=94 I want to get my results back with = the minimum of time-consuming fuss to get the most reliable timings. So = I do the reporting after I have all the results to hand in psql = "substitution variables".

*Results*

Here's what I get = with my PG Version 13.4 installation on my MacBook. (I removed blank = lines to improve the readability.)

SINGLE STATEMENT TEST (IMPLICIT = TXN)

txn_12 - txn_11 =3D 0    =         : exact
stm_12 - stm_11 =3D= 0            : exact
txn_11 - stm_11 =3D 0          =   : exact
clk_12 - clk_11 =3D slp_1    =     : error:  0.1 ms

TWO STATEMENT TEST (EXPLICIT TXN)

txn_12 - txn_11 =3D 0          =   : exact
stm_12 - stm_11 =3D 0    =         : exact
txn_11 - stm_11 =3D= 0            : error: -0.1 ms
clk_12 - clk_11 =3D slp_1        : = error:  0.6 ms

txn_2 - txn_11 =3D = 0             : exact
stm_2 - stm_11 =3D slp_1 + slp_2 : error:  1.6 = ms
clk_2 - clk_11 =3D slp_1 + slp_2 : = error:  1.6 ms

*Discussion*

The = stand-alone =E2=80=9Cselect" that implicitly starts the txn gives the = results that I'd expect.

I expected that, = using the explicitly started txn, the values returned by = transaction_timestamp() and statement_timestamp() in the first = statement would be identical (just as they are with the implicitly = started txn) because (as I believe) it's the first substantive SQL = statement, and not "set transaction", that actually starts the txn. = Else, if "set transaction" really does start the txn, then = statement_timestamp() should return a later value than = transaction_timestamp(). In fact, it's the other way round.

*Any thoughts on = why?*

If I can explain why, with the = explicitly started txn, the values from transaction_timestamp() and = statement_timestamp() differ in the first "select", then, I = suppose, there are no more mysteries in the results from the second = "select" because "commit" is bound to take time and I'm not using = an estimate for this in my value for "expected_diff".
=E2=80= =94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80= =94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 = =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80= =94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94

create function expected_tstz_diff_error(
  caption        in = text,
  t_finish     =   in timestamptz,
  t_start  =       in timestamptz,
  expected_diff  in double precision)
  returns         =   text
  language plpgsql
as $body$
declare
  dp_zero  constant double precision not = null :=3D 0;
  dp_1000  constant = double precision not null :=3D 1000;
  err  =     constant double precision not null :=3D
             ( = (extract(epoch from t_finish) - extract(epoch from t_start)) - = expected_diff )*dp_1000;
  x      =   constant text not null :=3D rpad(caption, 30)||' : ';
  r             =     text not null :=3D x||'exact';
begin
  if err <> dp_zero then
  =   r :=3D x||'error:'||to_char(err, '90.9')||' ms';
  end if;
  return r;
end;
$body$;

-- self-doc
\set AUTOCOMMIT = 'on'

\set zero  0
\set slp_1 1
\set slp_2 2

\t on
\o pg-local.txt

-- = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
select = 'SINGLE STATEMENT TEST (IMPLICIT TXN)';
---------------------------------------------------------------= -------

select
  ''''''''||transaction_timestamp() = ::text||'''''''' as txn_11,
  ''''''''||statement_timestamp()   ::= text||'''''''' as stm_11,
  ''''''''||clock_timestamp()     =   ::text||'''''''' as clk_11,
  ''''''''||pg_sleep(:slp_1)      =   ::text||'''''''' as dummy,
  ''''''''||transaction_timestamp() = ::text||'''''''' as txn_12,
  ''''''''||statement_timestamp()   ::= text||'''''''' as stm_12,
  ''''''''||clock_timestamp()     =   ::text||'''''''' as clk_12
\gset s1_

select expected_tstz_diff_error('txn_12 - = txn_11 =3D 0',            :s1_txn_12, = :s1_txn_12, :zero);
select = expected_tstz_diff_error('stm_12 - stm_11 =3D 0',      =       :s1_stm_12, :s1_stm_12, :zero);
select expected_tstz_diff_error('txn_11 - stm_11 =3D = 0',            :s1_txn_11, = :s1_stm_11, :zero);
select = expected_tstz_diff_error('clk_12 - clk_11 =3D slp_1',    =     :s1_clk_12, :s1_clk_11, :slp_1);

-- = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
select = 'TWO STATEMENT TEST (EXPLICIT TXN)';
---------------------------------------------------------------= -------

start transaction;

select
  ''''''''||transaction_timestamp() = ::text||'''''''' as txn_11,
  ''''''''||statement_timestamp()   ::= text||'''''''' as stm_11,
  ''''''''||clock_timestamp()     =   ::text||'''''''' as clk_11,
  ''''''''||pg_sleep(:slp_1)      =   ::text||'''''''' as dummy,
  ''''''''||transaction_timestamp() = ::text||'''''''' as txn_12,
  ''''''''||statement_timestamp()   ::= text||'''''''' as stm_12,
  ''''''''||clock_timestamp()     =   ::text||'''''''' as clk_12
\gset s1_

select pg_sleep(:slp_2);

select
  ''''''''||transaction_timestamp() = ::text||'''''''' as txn_2,
  ''''''''||statement_timestamp()   ::= text||'''''''' as stm_2,
  ''''''''||clock_timestamp()     =   ::text||'''''''' as clk_2
\gset s2_

commit;

-- Within = the first "select".
select = expected_tstz_diff_error('txn_12 - txn_11 =3D 0',      =       :s1_txn_12, :s1_txn_12, :zero);
select expected_tstz_diff_error('stm_12 - stm_11 =3D = 0',            :s1_stm_12, = :s1_stm_12, :zero);
select = expected_tstz_diff_error('txn_11 - stm_11 =3D 0',      =       :s1_txn_11, :s1_stm_11, :zero);
select expected_tstz_diff_error('clk_12 - clk_11 =3D = slp_1',        :s1_clk_12, :s1_clk_11, = :slp_1);

-- Within the trasaction but = across the first and the second "select".
select = expected_tstz_diff_error('txn_2 - txn_11 =3D 0',     =         :s2_txn_2,  :s1_txn_11, = :zero);
select expected_tstz_diff_error('stm_2 - stm_11 =3D = slp_1 + slp_2', :s2_stm_2,  :s1_stm_11, (:slp_1 + :slp_2));
select expected_tstz_diff_error('clk_2 - clk_11 =3D slp_1 + = slp_2', :s2_clk_2,  :s1_clk_11, (:slp_1 + :slp_2));

\o
\t off


= --Apple-Mail=_D225E347-206F-4A7B-8920-566BFDEB43C2--