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 1qCRKW-0005YN-QV for pgsql-admin@arkaria.postgresql.org; Thu, 22 Jun 2023 20:55:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qCRKV-0002ws-Ow for pgsql-admin@arkaria.postgresql.org; Thu, 22 Jun 2023 20:54:59 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qCRKV-0002wj-AL for pgsql-admin@lists.postgresql.org; Thu, 22 Jun 2023 20:54:59 +0000 Received: from mail-yw1-x1131.google.com ([2607:f8b0:4864:20::1131]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qCRKS-003gTr-W4 for pgsql-admin@lists.postgresql.org; Thu, 22 Jun 2023 20:54:58 +0000 Received: by mail-yw1-x1131.google.com with SMTP id 00721157ae682-57059626276so83063317b3.3 for ; Thu, 22 Jun 2023 13:54:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687467296; x=1690059296; h=in-reply-to:references:to:from:content-language:subject:user-agent :mime-version:date:message-id:from:to:cc:subject:date:message-id :reply-to; bh=97WaWg9P1o1oytDsRK8gLspOZ8VGa717cEQnm55Lnxk=; b=XgD0DAPJKhkND9yysPW08HA3Uni7m7mKc3N7jRgxttxE1S3vQ6+a5KdjVPbTyYp7xP Uy8OQY2r3cJKk9sft98mDgSRYcHcgMLvoOAhqS75cyHrXbLt2pidOBTgHOU/QapwEjac wSH0cfeGQ/+hb3ZEYDPNYlDI/WcrjQAk6f97+Az0TXvbGSXWRQJ/ujtPIMH80oayCuH1 PFOzbRipDzCNtzsxYtsD8AiGcH8K6wjnFTq2zgh9UhJzM6efPOE1J41KB1wi7K2kRdSf Cx/52T8nJNCLTUSLCjJdSavVL1eeFTw2U1IuC+2si9bWPj+q9sxUcIPLu2tGt7xAyxGh a5/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687467296; x=1690059296; h=in-reply-to:references:to:from:content-language:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=97WaWg9P1o1oytDsRK8gLspOZ8VGa717cEQnm55Lnxk=; b=TY50YwWKRRU680gfd/bt/1mSnjIxqaoAtLmOtgtHWasIYyCuTh64i/7ztIu4EARTby Gz4niltNI0TtqoFSPWtEM60oLsL/nJ2XstF2MAdp5Wcb/5GmwDUp/6OdRrH/AQRmTGHw F3JMSVXblFteDofwR8AOX/60fiUVZiFeEnFCm0c1+RgjY8c0T4G18FfILazcsoMcLbH6 WvbEZt3SpD5mnHfsQVu8YUE+ht9P9pruY5C8hannz9qt3BUpSz7rw7P0gy2Ou6RY5xDx g0DN9nPoHR6cMi8nv4SdXoI2CUgc1A0ObHaTe4t2lM3ve2ZwEjbyaqI1nSeiIIZTKD9+ 9xuQ== X-Gm-Message-State: AC+VfDxcPjxegbpVv+9WCBOR7EXM4Z7LsbrxgLWK4XbvCzbB4Jz8KPE/ lzoX0DnYgjF+FZFYhBe5pptxuvV3++I= X-Google-Smtp-Source: ACHHUZ6soit/wKDpKpuEpK7WVqWUWUOTtZ72W2hHrVaWEcKm6dTeQAcowZDiIjMFOdNIjUcwnURztQ== X-Received: by 2002:a25:ab89:0:b0:ba8:7015:36df with SMTP id v9-20020a25ab89000000b00ba8701536dfmr18610419ybi.26.1687467295664; Thu, 22 Jun 2023 13:54:55 -0700 (PDT) Received: from ?IPV6:2600:1700:bd2c:b810:1087:186d:a03b:79fd? ([2600:1700:bd2c:b810:1087:186d:a03b:79fd]) by smtp.googlemail.com with ESMTPSA id w34-20020a25ac22000000b00bc7c81c3cecsm1745555ybi.14.2023.06.22.13.54.55 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 22 Jun 2023 13:54:55 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------YAq8wIDNMN0LDJNS61D9kyeR" Message-ID: <8ed2489b-7198-dbe7-c86f-ef7fee3af2a0@gmail.com> Date: Thu, 22 Jun 2023 15:54:54 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: GENERATE AS Content-Language: en-US From: Ron To: pgsql-admin@lists.postgresql.org References: <7c48174f8ce449af8f3e875bdeca7150@express-scripts.com> <328b8fb1-08e8-2c74-a2b8-878ad5e9f9a4@gmail.com> In-Reply-To: <328b8fb1-08e8-2c74-a2b8-878ad5e9f9a4@gmail.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------YAq8wIDNMN0LDJNS61D9kyeR Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 6/22/23 15:45, Ron wrote: > On 6/22/23 15:00, Wetmore, Matthew (CTR) wrote: >> >> Hi, I have this issue and now I’m just wasting time. Can you tell me what >> I’m doing wrong? >> >> I’d like to subtract a column timestamp hour from current hour to give me >> hours elapased. >> >> How do I do this easily? >> >> I think I’ve tried every combination of types and casting. >> >> Thanks in advance. >> >> --------------------------- >> >> 1. ALTER TABLE matt >> >>                add column matt_time timestamp with time zone default >> current_timestamp; >> > > Besides what Ilya and David mentioned, CURRENT_TIMESTAMP is based on when > the transaction started.  You might have wanted clock_timestamp(). Note the difference between bar_time and bar_time2 in rows 3 and 4. foo=# alter table bar add column bar_time timestamp with time zone default clock_timestamp(); ALTER TABLE foo=# alter table bar add column bar_time2 timestamp with time zone default current_timestamp; ALTER TABLE foo=# begin; BEGIN foo=*# insert into bar (f1, f2) values (3, 'jj'); INSERT 0 1 foo=*# insert into bar (f1, f2) values (4, 'kk'); INSERT 0 1 foo=*# commit; COMMIT foo=# select * from bar;  f1 | f2 |           bar_time            | bar_time2 ----+----+-------------------------------+-------------------------------   1 | gg | 2023-06-22 15:48:43.509351-05 | 2023-06-22 15:50:27.983162-05   2 | hh | 2023-06-22 15:50:38.675741-05 | 2023-06-22 15:50:38.675487-05   3 | jj | 2023-06-22 15:50:57.886325-05 | 2023-06-22 15:50:49.599807-05   4 | kk | 2023-06-22 15:51:10.374681-05 | 2023-06-22 15:50:49.599807-05 (4 rows) -- Born in Arizona, moved to Babylonia. --------------YAq8wIDNMN0LDJNS61D9kyeR Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit On 6/22/23 15:45, Ron wrote:
On 6/22/23 15:00, Wetmore, Matthew (CTR) wrote:

Hi, I have this issue and now I’m just wasting time. Can you tell me what I’m doing wrong?

 

I’d like to subtract a column timestamp hour from current hour to give me hours elapased.

 

How do I do this easily?

 

I think I’ve tried every combination of types and casting.

 

Thanks in advance.

---------------------------

 

1. ALTER TABLE matt

               add column matt_time timestamp with time zone default current_timestamp;


Besides what Ilya and David mentioned, CURRENT_TIMESTAMP is based on when the transaction started.  You might have wanted clock_timestamp().

Note the difference between bar_time and bar_time2 in rows 3 and 4.

foo=# alter table bar add column bar_time timestamp with time zone default clock_timestamp();
ALTER TABLE

foo=# alter table bar add column bar_time2 timestamp with time zone default current_timestamp;
ALTER TABLE

foo=# begin;
BEGIN
foo=*# insert into bar (f1, f2) values (3, 'jj');
INSERT 0 1
foo=*# insert into bar (f1, f2) values (4, 'kk');
INSERT 0 1
foo=*# commit;
COMMIT
foo=# select * from bar;
 f1 | f2 |           bar_time            |           bar_time2           
----+----+-------------------------------+-------------------------------
  1 | gg | 2023-06-22 15:48:43.509351-05 | 2023-06-22 15:50:27.983162-05
  2 | hh | 2023-06-22 15:50:38.675741-05 | 2023-06-22 15:50:38.675487-05
  3 | jj | 2023-06-22 15:50:57.886325-05 | 2023-06-22 15:50:49.599807-05
  4 | kk | 2023-06-22 15:51:10.374681-05 | 2023-06-22 15:50:49.599807-05
(4 rows)


--
Born in Arizona, moved to Babylonia.
--------------YAq8wIDNMN0LDJNS61D9kyeR--