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 1rrcpv-001iPi-JL for pgsql-general@arkaria.postgresql.org; Tue, 02 Apr 2024 12:01:56 +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 1rrcpu-007sKK-Ox for pgsql-general@arkaria.postgresql.org; Tue, 02 Apr 2024 12:01:54 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rrcpu-007sKA-Ac for pgsql-general@lists.postgresql.org; Tue, 02 Apr 2024 12:01:54 +0000 Received: from mout-u-204.mailbox.org ([80.241.59.204]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rrcpq-000AIq-Fk for pgsql-general@lists.postgresql.org; Tue, 02 Apr 2024 12:01:53 +0000 Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:b231:465::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-204.mailbox.org (Postfix) with ESMTPS id 4V861Q2gvwz9sWV; Tue, 2 Apr 2024 14:01:46 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ewie.name; s=MBO0001; t=1712059306; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Qdy6fuzggwxr5BOK1d9NPgwAFoHClGdQKoAqn9WJJic=; b=xqmzhBJeTUTX/jRohu1ssS1epTmESmXbfaLZZ3JxriIDdJz9tcwkf9rf+0O5+J1ABQFEJg RMfR5zjeg+cev0EjXvzSdDHetQps+pxR1ACMsaNTEixDI6QLx+MjqQS+6DB050hczmdMv7 ykT+7a1+YduHhIGDjztHwABKGHlVXchnxXv40l3EGeXqhZZHtVF/Rw+7u+3fwqY6lNcgDa cJYH3dkG87I0JP5S+1SM+FXeVc+4wQ0zRQooDJ+BxFmZxUXWhNFvOZ0w2KC/tRc85BwJYf gnQD5qS7YgNUuGrKOtx5G+K9kMsl0uOv4Uziz7RcGg/pwgSG32Z8+eBCA909BA== Date: Tue, 2 Apr 2024 14:01:44 +0200 From: Erik Wienhold To: sud Cc: pgsql-general Subject: Re: Timestamp conversion Error in dynamic sql script Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: 4V861Q2gvwz9sWV List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2024-04-02 07:38 +0200, sud wrote: > I am trying to create a block which will create a few partitions > dynamically and also insert ~1million rows into each of those partitions. > Not able to figure out why it's giving below error during timezone > conversion while defining the partitions even though I used the typecast? > > CREATE TABLE parent_table ( > id Numeric, > col1 TEXT, > col2 TEXT, > partition_key TIMESTAMP, > primary key (partition_key, id) > ) > PARTITION BY RANGE (partition_key); > > ********** > > DO $$ > DECLARE > start_date TIMESTAMP := '2022-01-01'; > begin > FOR i IN 0..10 LOOP > > EXECUTE format(' > CREATE TABLE parent_table_%s ( > CHECK (partition_key >= DATE ''%s'' AND partition_key < DATE ''%s'' > ) > ) INHERITS (parent_table);', > TO_CHAR(start_date + i, 'YYYY_MM_DD'), > > TO_CHAR(start_date + i, 'YYYY-MM-DD')::timestamp , > TO_CHAR(start_date + i + INTERVAL '1 day', 'YYYY-MM-DD')::timestamp > ); > EXECUTE format(' > ALTER TABLE parent_table ATTACH PARTITION parent_table_%s > FOR VALUES FROM (''%s'') TO (''%s'');', > TO_CHAR(start_date + i, 'YYYY_MM_DD'), > TO_CHAR(start_date + i, 'YYYY-MM-DD')::timestamp, > TO_CHAR(start_date + i + INTERVAL '1 day', 'YYYY-MM-DD') ::timestamp > ); > > END LOOP; > > FOR i IN 0..10 LOOP > EXECUTE format(' > INSERT INTO parent_table_%s (id,col1, col2, partition_key) > SELECT > generate_series(1, 1000000), > md5(random()::text), > md5(random()::text), > TIMESTAMP ''%s'' + INTERVAL ''%s days'' > FROM generate_series(1, 1000000);', > TO_CHAR(start_date + i, 'YYYY_MM_DD'), > start_date, > i > ); > END LOOP; > END $$; > > *********** > > > > > *SQL Error [42883]: ERROR: operator does not exist: timestamp without time > zone + integerHint: No operator matches the given name and argument types. > You might need to add explicit type casts.Where: PL/pgSQL function > inline_code_block line 7 at EXECUTEError position:* Two ways to fix it: 1. Declare start_date as DATE when you want to add days with date + int 2. Keep TIMESTAMP and use start_date + make_interval(days => i) -- Erik