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 1sp0mu-00Fd6e-4G for pgsql-general@arkaria.postgresql.org; Fri, 13 Sep 2024 07:32:17 +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 1sp0mt-00EWyS-On for pgsql-general@arkaria.postgresql.org; Fri, 13 Sep 2024 07:32:15 +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 1sp0mt-00EWyF-DB for pgsql-general@lists.postgresql.org; Fri, 13 Sep 2024 07:32:15 +0000 Received: from mail-oa1-x2a.google.com ([2001:4860:4864:20::2a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sp0mp-000wlR-Sj for pgsql-general@lists.postgresql.org; Fri, 13 Sep 2024 07:32:14 +0000 Received: by mail-oa1-x2a.google.com with SMTP id 586e51a60fabf-27c147db58bso862653fac.1 for ; Fri, 13 Sep 2024 00:32:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726212731; x=1726817531; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=/apmL6j9hnpTUZHT5GXBA0Cm+ed5HiZ2HfNkAqox1CU=; b=IHsqrgixw/sPJSs1pmzAy4KETwBrLKndwiShF595lnM/xzVw184fexKy6N/jr5hvrl 9uOcxwNX+ANeVtZapZM2z7hXRZwdf17rYfLan7vrO1u1y7i8cyf0uS7q7Xz1YjzgbWSV lgwzIzLvsdVxfkufatpd6odZM+Sexb/QcxyF5a3TwQeGu8t5pjNbnTuVniumiSGykJOh ITZ62g7jOf/JTco/vOIaqp7pr3WcQIM4vFZKkHfDGZLw7KOq5yWf73dwzTp5x/vL7wBR 3y3RoadJK0aD5xjYRllR/0YMtjc6r2nE7SH7q5gus77ylxF7aF2fHJG6gOeNGRzSrNWR 5g0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726212731; x=1726817531; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=/apmL6j9hnpTUZHT5GXBA0Cm+ed5HiZ2HfNkAqox1CU=; b=Ig4hWNDI6oYeVGGilGTz2OhZtmb4oYNDM4/kn/x0VLpDazXeD9/kHyRhqwHG8HvQAQ XKUE5/RGMdnerWHOtyK+EKyVpL1aAVmOySU1JsiL/1/ILakYcbJI6K5B/JM8deAxZuxG LbuzPHqivsLDGypisyemRTmhcvpul7sXEJt0Fa99qbrx8f3yisle4Q1iYyQEGub1VBED r+NwejD+FU8TQejEe01yReDG4WDILW8+jDTlm0n9fwu2E+0syUYUqbmaDaTneJp94w25 nSnK2zp4cZnarFKmCru2NdawzAeRapLrvyPIdGOVdGRP3Em6m/fpYwE29Kd6S79gIpdc x1Ng== X-Gm-Message-State: AOJu0YxYfWT7mgnP1w82mTfDHawp3QQHxtdOsyS+OEOiAqrR0UulJNZS EcrQ7GXz5d0WQKsbePjqc1ViztbOwiXZt48SbAecg5UttTo3gODOV7m/u0jjjIrRS+iNJcvRgCm /wBpUx/SvSDYerr1o1+5JrKMLEtPXuggL X-Google-Smtp-Source: AGHT+IHA2IGbDNFA2ySNn1UD6+ROw/HN2WRMaYSLpYp/py5DKHJlgO1mTmq1xwo2Tpfey6iFedPSWa02v4KvPob1Vds= X-Received: by 2002:a05:6870:1718:b0:261:13b6:16de with SMTP id 586e51a60fabf-27c3f2c3ae7mr3665151fac.25.1726212730749; Fri, 13 Sep 2024 00:32:10 -0700 (PDT) MIME-Version: 1.0 From: yudhi s Date: Fri, 13 Sep 2024 13:01:58 +0530 Message-ID: Subject: Manual query vs trigger during data load To: pgsql-general Content-Type: multipart/alternative; boundary="0000000000009f8d790621fb3890" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000009f8d790621fb3890 Content-Type: text/plain; charset="UTF-8" Hello All, We are having a table which is going to be inserted with 100's of millions of rows each day. And we now want to have a requirement in which we need to do some transformation/lookup logic built on top of a few of the input bind values , while inserting the data. So I wanted to understand ,is it possible to do it along with the INSERT query or is it better to have a trigger created for the same? For. e.g Below is the current Insert query used in the Java code. We want to fetch the value for "column2" from a lookup table rather than directly inserting as it's coming from the customer side. So I am thinking of a trigger like below. But at the same time I also want to compare the performance of a normal way of doing the lookup vs having it performed using triggers. So one way i am thinking is first fetching the value of the "column2" from reference_tab1 using a separate "select query" in Java code itself, and then passing that to the below insert query, but i think that will increase the response time as that will be a separate DB call. 1)So, is there a way I can do it directly using the single INSERT query itself without additional SELECT query? And then will try to compare that with the trigger based approach. 2)Additionally , if this decision will impact a batch insert approach. i.e. say , in case of trigger , will the batch insert fail because trigger will force it to make it row by row? INSERT INTO tab_part1 (column1, column2, column3, column4, column5, part_date) VALUES (:v_col1, :v_col2, :v_col3, :v_col3, :v_col4,:v_col5, CURRENT_DATE); CREATE OR REPLACE FUNCTION trg_func_populate_column2() RETURNS TRIGGER AS $$ BEGIN -- Fetch reference value and populate column2 NEW.column2 := (SELECT lookup_key FROM reference_tab1 WHERE lookup_key = old.column2); RETURN NEW; END; $$ LANGUAGE plpgsql; Regards Yudhi --0000000000009f8d790621fb3890 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello All,

We are having a table which is going to= be inserted with 100's of millions of rows each day. And we now want t= o have a requirement in which we need to do some transformation/lookup logi= c built on top of a few of the input bind values , while inserting the data= . So I wanted to understand ,is it possible to do it along with the INSERT = query or is it better to have a trigger created for the same?

For. e= .g Below is the current Insert query used in the Java code. We want to fetc= h the value for "column2" from a lookup table rather than directl= y inserting as it's coming from the customer side. So I am thinking of = a trigger like below. But at the same time I also want to compare the perfo= rmance of a normal way of doing the lookup vs having it performed using tri= ggers.

So one way i am thinking is first fetching the value of the = "column2" from reference_tab1 using a separate "select query= " in Java code itself, =C2=A0and then passing that to the below insert= query, but i think that will increase the response time as that will be a = separate DB call.

1)So,=C2=A0=C2=A0is there a way = I can do it directly using the single INSERT query itself without additiona= l SELECT query? And then will try to compare that with the trigger based ap= proach.
2)Additionally , if this decision will impact a batch insert app= roach. i.e. say , in case of trigger , will the batch insert fail because t= rigger=C2=A0will force it to make it row by row?

INSERT INTO tab_par= t1 (column1, column2, column3, column4, column5, part_date)
=C2=A0 =C2= =A0 =C2=A0 =C2=A0 VALUES (:v_col1, :v_col2, :v_col3, :v_col3, :v_col4,:v_co= l5, CURRENT_DATE);

CREATE OR REPLACE FUNCTION trg_func_populate_colu= mn2() RETURNS TRIGGER AS $$
BEGIN
=C2=A0 =C2=A0 -- Fetch reference va= lue and populate column2
=C2=A0 =C2=A0 NEW.column2 :=3D (SELECT lookup_k= ey FROM reference_tab1 WHERE lookup_key =3D old.column2);
=C2=A0 =C2=A0 = RETURN NEW;
END;
$$ LANGUAGE plpgsql;

Re= gards
Yudhi
--0000000000009f8d790621fb3890--