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 1spV45-001tam-PC for pgsql-general@arkaria.postgresql.org; Sat, 14 Sep 2024 15:52:02 +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 1spV44-00G2A4-V6 for pgsql-general@arkaria.postgresql.org; Sat, 14 Sep 2024 15:52:00 +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 1spV44-00G29w-Jb for pgsql-general@lists.postgresql.org; Sat, 14 Sep 2024 15:52:00 +0000 Received: from mail-oo1-xc32.google.com ([2607:f8b0:4864:20::c32]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1spV41-001AqH-09 for pgsql-general@lists.postgresql.org; Sat, 14 Sep 2024 15:52:00 +0000 Received: by mail-oo1-xc32.google.com with SMTP id 006d021491bc7-5dc93fa5639so940557eaf.1 for ; Sat, 14 Sep 2024 08:51:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726329116; x=1726933916; darn=lists.postgresql.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=3bvBf/3RSnCSZLhtgWwQVy4B5lFjmTfrsX7JpGxv53g=; b=Nc+QdiGaZ7sgr5UhljHN9d3PwJacflC7UkAla1a+2/ptZRWrj5nR99kYaV221Gd5uz YNas2svzx5NX2XGmv+coWBQ4YthaxN43xv363qCy6IKi5Gcvub6hbMvOD5YvUZVvZRR0 raqaUf0gTC4a75i4o0o5pKPaYjtJ/dGF+WyvVrEXMxcg+KvMWPd5p3N0pz1gumsv2dFa OLpZk+3Qx/iKGMULZQWNxiAU4QLJM8aOSxJT6W4T+vKvrUEPZCmnFDZ0NWjn+Xqad9xj sm03XEiCNc+kXpsy7pFtCRrHX51vn0w921qAxadiqvNRoV5c+EXzSMWDZSY7NjdX/qGb +6sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726329116; x=1726933916; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=3bvBf/3RSnCSZLhtgWwQVy4B5lFjmTfrsX7JpGxv53g=; b=lLTRra8/jXeD8Spb53e5mOlzh7Xmk6QP8Y53FZzzMhFu7aYagqvaSBxPaAwzYo6PBP pLagJzbfDQsyliXLPCely31AKyOQuOU2yeJ5ofaXwc2K2MlJwWg3T5AhlaActTpBytH8 UruwPQuVu7+PqZMwrugV4mMCE9kXu4oCq7HgMhj3xki3JSa0lyLgJGT4ryfaiLG5YM1v L/XhwutZ5PTrZUhVIExdjzINoWRTrnbJhLUIBWTyTz3MM6atB2qDisQzVGBB6+y8tLUo aKgN6COiqdOUCZ202e0yNloAPYbr5UuLUmW2P909PNDULTTPpT544MmPrG18gjq3DRet Eyag== X-Gm-Message-State: AOJu0YwTi2ZXhZEcMfpwwO9PUUFgg2xmCm3MD8xZIWtm2pPZwmwkzL+n uHf0cvYC4wn2b486WIs5SfaR0z/0COhumJV06cIImlF2KRes6VCt6fncVGlyD+HQIKThSDrUy/O I2r2kA8FMVmjO7+Qh97TLrtAQQVPO0xV2 X-Google-Smtp-Source: AGHT+IE92+KQiab33WYdDxjkXsaj3sBEoPM18UWB5AYm0aBjo0s39UIGHNP4MfHO5Ih5bH63cdktX4eTcU99DjmY4Co= X-Received: by 2002:a05:6871:5cf:b0:25e:bd07:4743 with SMTP id 586e51a60fabf-27c6812af83mr4627312fac.0.1726329116074; Sat, 14 Sep 2024 08:51:56 -0700 (PDT) MIME-Version: 1.0 References: <67f2a664-b480-40ea-892d-2ab2147ad390@aklaver.com> <20240914104713.jatugdwnmojwa7kf@hjp.at> In-Reply-To: <20240914104713.jatugdwnmojwa7kf@hjp.at> From: yudhi s Date: Sat, 14 Sep 2024 21:21:45 +0530 Message-ID: Subject: Re: Manual query vs trigger during data load To: pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="000000000000baacf0062216514b" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000baacf0062216514b Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sat, Sep 14, 2024 at 4:17=E2=80=AFPM Peter J. Holzer = wrote: > On 2024-09-14 00:54:49 +0530, yudhi s wrote: > > As "thiemo" mentioned , it can be done as below method, but if we have > > multiple lookup tables to be populated for multiple columns , then , ho= w > can > > the INSERT query be tweaked to cater the need here? > > Just use a join: > insert into target(val1, val2, val3, val4) > select :param1, cfgA.substA, :param3, cfgB.substB > from cfgA, cfgB > where cfgA.keyA =3D :param2 and cfgB.keyB =3D :param4 > > Or use a CTE per lookup which might be more readable: > > with cA as ( select substA from cfgA where keyA =3D :param2 ), > cB as ( select substB from cfgB where keyB =3D :param4 ) > insert into target(val1, val2, val3, val4) > select :param1, cA.substA, :param3, cB.substB > from cA, cB > > Thank you. I will try these options. Also we are trying to do something as below , which will separate the tables based on the specific lookup fields for the target tables and thus it will look simple rather than using those reference tables in the From clause which may cause some confusion in reading the code or not sure if it will cause cartesian. Please correct me if I'm wrong. INSERT INTO tab_part1 (column1, column2, column3, column4, column5, part_date) VALUES ( :v_col1, (SELECT lookup_value FROM reference_tab1 WHERE lookup_key =3D :v_col2), :v_col3, :v_col4, :v_col5, CURRENT_DATE ); --000000000000baacf0062216514b Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Sat, Sep 14, 2024 at 4:17=E2=80=AFPM P= eter J. Holzer <hjp-pgsql@hjp.at= > wrote:
On 2= 024-09-14 00:54:49 +0530, yudhi s wrote:
> As "thiemo" mentioned , it can be done as below method, but = if we have
> multiple=C2=A0lookup tables to be populated for multiple columns , the= n , how can
> the INSERT query be tweaked to cater the need here?

Just use a join:
=C2=A0 =C2=A0 insert into target(val1, val2, val3, val4)
=C2=A0 =C2=A0 select :param1, cfgA.substA, :param3, cfgB.substB
=C2=A0 =C2=A0 from cfgA, cfgB
=C2=A0 =C2=A0 where cfgA.keyA =3D :param2 and cfgB.keyB =3D :param4

Or use a CTE per lookup which might be more readable:

=C2=A0 =C2=A0 with cA as ( select substA from cfgA where keyA =3D :param2 )= ,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0cB as ( select substB from cfgB where key= B =3D :param4 )
=C2=A0 =C2=A0 insert into target(val1, val2, val3, val4)
=C2=A0 =C2=A0 select :param1, cA.substA, :param3, cB.substB
=C2=A0 =C2=A0 from cA, cB


Thank you= . I will try these options.=C2=A0
Also we are trying to do someth= ing as below , which will separate the tables based on the specific=C2=A0lo= okup fields for the target tables and thus it will look simple rather than = using those reference tables in the From clause which may cause some confus= ion in reading the code or not sure if it will cause cartesian. Please corr= ect me if I'm wrong.

IN= SERT INTO tab_part1 (column1, column2, column3, column4, column5, part_date= )
VALUES ( :v_col1, (SELECT lookup_value FROM refere= nce_tab1 WHERE lookup_key =3D :v_col2), :v_col3, :v_col4, :v_col5, CURRENT_= DATE );=C2=A0
--000000000000baacf0062216514b--