Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1scX8b-008DgB-Lh for pgsql-general@arkaria.postgresql.org; Fri, 09 Aug 2024 21:27:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1scX8a-006EwU-5A for pgsql-general@arkaria.postgresql.org; Fri, 09 Aug 2024 21:27:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1scX8Y-006EwM-NH for pgsql-general@lists.postgresql.org; Fri, 09 Aug 2024 21:27:03 +0000 Received: from fout3-smtp.messagingengine.com ([103.168.172.146]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1scX8Q-0041zS-Qk for pgsql-general@lists.postgresql.org; Fri, 09 Aug 2024 21:27:02 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailfout.nyi.internal (Postfix) with ESMTP id 29236138CCFC; Fri, 9 Aug 2024 17:26:52 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Fri, 09 Aug 2024 17:26:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm2; t=1723238812; x=1723325212; bh=ak83ynlr92uZ94X8JNFvfAmKdn6C++Oy5MWkt7JHpXc=; b= eRtH+VuhLJqjf+x+SL8wI7MnZCXzyCKfjbdTw5Xw9Z6K6h9gvAySfdDcYJKWDJg5 Ep+Y1FuvH057GbGOLGIxl+rI1Ae55S7Wd29FvM1qSRwKbks9k0Oh1ScA/kn8W+qD AcO042vMPnCXAaXvbEJ4tPZYrSXFhI1v1hkfpf7mGMfilm3v7Sht5F35/X8JlGLo x0bK9np20PSBmz2Trdrir1kkpzLKD7yEhXFOVEwhvpe9lMuxYE5jLOKIfClbnEy2 yJXQ8SZhaL6Ss+eEiVBLHJUYCI8WFQ8nK0SObrU1Ce85sQQ6ILdaB8wUzQYPJnX7 OpQrsd0cRIOab+vM4+kwvw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1723238812; x= 1723325212; bh=ak83ynlr92uZ94X8JNFvfAmKdn6C++Oy5MWkt7JHpXc=; b=a MoPnTpdgf6kcqj8KCDbI0UtL6fRm+xB954E5NHWSaxqQ6DZSZR5gvWXbqUJeUKbl Y0o/EhFnuJ+YVpRT2qswBnACNhtLwTcJBVTaTV6oN59cwodTdJea9cqdHrZDmAg7 5hI7ZBoe0QJwx5snR/J9eUEA7IMtua+Ty6cqwuQj78f+F+zpNnqJ8dbcfkhZbUnX 7iaqPOEAoKPggHDeimsKsaVmEKbB7SjxeIvPrQEu1of7kWg/ny5MWl1B5JQy0cR1 F+euAQhzmMgvlgJQsnME4AFbCE/p00zfDLs7p9Uf9y+tRbJzJi/TOv8b76SKtvHz cjAjnKAtxyK8AhIRM8/xw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrleeggdduheelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefkffggfg fuvfhfhfgjtgfgsehtkeertddtvdejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghr uceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecuggftrfgrth htvghrnhepudeihfeluefgvdfhvedtgefghedvffevleekudethfethedtgefgtdehuddu hffgnecuffhomhgrihhnpegusghfihguughlvgdruhhkpdhpohhsthhgrhgvshhqlhdroh hrghenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegr ughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmpdhnsggprhgtphhtthhope dvpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehlvggrrhhnvghruggrthgrsggr shgvleelsehgmhgrihhlrdgtohhmpdhrtghpthhtohepphhgshhqlhdqghgvnhgvrhgrlh eslhhishhtshdrphhoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 9 Aug 2024 17:26:51 -0400 (EDT) Message-ID: <37e09717-f121-4192-b152-18df17713414@aklaver.com> Date: Fri, 9 Aug 2024 14:26:50 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Insert works but fails for merge To: yudhi s , pgsql-general References: Content-Language: en-US From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 8/9/24 14:13, yudhi s wrote: > Hello, > It's version 15.4 postgres. Where we have an insert working fine, but > then a similar insert with the same 'timestamp' value, when trying to be > executed through merge , it fails stating "You will need to rewrite or > cast the expression.". Why so? > > *Example:-* > https://dbfiddle.uk/j5S7br-q * > * > > CREATE TABLE tab1 ( >     id varchar(100) , >     mid INT, >     txn_timestamp TIMESTAMPTZ NOT NULL, >     cre_ts TIMESTAMPTZ NOT NULL > ) PARTITION BY RANGE (txn_timestamp); > > CREATE TABLE tab1_2024_08_09 PARTITION OF tab1 >     FOR VALUES FROM ('2024-08-09 00:00:00') TO ('2024-08-10 00:00:00'); > > -- Below insert works fine > INSERT INTO tab1 >     (id, mid, txn_timestamp, cre_ts) > VALUES >     ('5efd4c91-ef93-4477-840c-a723ae212d84', 123, > '2024-08-09T11:33:49.402585600Z', '2024-08-09T11:33:49.402585600Z'); > > -- Below merge , which trying to insert similar row but failing > > WITH source_data (id, mid, txn_timestamp, cre_ts) AS ( >     VALUES ('5efd4c91-ef93-4477-840c-a723ae212d66', 123, > '2024-08-09T11:33:49.402585600Z', '2024-08-09T11:33:49.402585600Z') > ) > INSERT INTO tab1 (id, mid, txn_timestamp, cre_ts) > SELECT id, mid, txn_timestamp, cre_ts > FROM source_data > ON CONFLICT (id) DO UPDATE > SET    mid = EXCLUDED.mid, >     txn_timestamp = EXCLUDED.txn_timestamp, >     cre_ts = EXCLUDED.cre_ts; > > ERROR: column "txn_timestamp" is of type timestamp with time zone but > expression is of type text LINE 24: SELECT id, mid, txn_timestamp, > cre_ts ^ HINT: You will need to rewrite or cast the expression. VALUES: https://www.postgresql.org/docs/current/sql-values.html "When VALUES is used in INSERT, the values are all automatically coerced to the data type of the corresponding destination column. When it's used in other contexts, it might be necessary to specify the correct data type. If the entries are all quoted literal constants, coercing the first is sufficient to determine the assumed type for all: SELECT * FROM machines WHERE ip_address IN (VALUES('192.168.0.1'::inet), ('192.168.0.10'), ('192.168.1.43')); " The VALUES is not directly attached to the INSERT, you will need to do explicit casts: VALUES ('5efd4c91-ef93-4477-840c-a723ae212d66', 123, '2024-08-09T11:33:49.402585600Z'::timestamptz, '2024-08-09T11:33:49.402585600Z'::timestamptz) -- Adrian Klaver adrian.klaver@aklaver.com