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 1sZ30q-00G3cU-FJ for pgsql-general@arkaria.postgresql.org; Wed, 31 Jul 2024 06:40:40 +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 1sZ30o-001rcT-VJ for pgsql-general@arkaria.postgresql.org; Wed, 31 Jul 2024 06:40:38 +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 1sZ30o-001rcL-Jr for pgsql-general@lists.postgresql.org; Wed, 31 Jul 2024 06:40:38 +0000 Received: from mail-ej1-x62d.google.com ([2a00:1450:4864:20::62d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sZ30n-002O6O-7c for pgsql-general@lists.postgresql.org; Wed, 31 Jul 2024 06:40:38 +0000 Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-a7aa086b077so467342266b.0 for ; Tue, 30 Jul 2024 23:40:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722408036; x=1723012836; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Cbf59ISg0Sc5Hqb6uDTGlwoWxGdAcZEOLjHZD2II7GI=; b=jgu94WUfNEX59Ad1GSW+QkT59Z6qpqJq67GW+geYwm2znjubVJtihFbp9zffXo34+l eiOkhdD+EOU/GOa+xcLF1XEnqxB0Mxqg/yQLhPect5fUpJ3eAkwIL+CKNq7GkvIB478c UPnB51gk+PAHxWD8B5n+2ioLxopmTBhOOOCxjvDs11ZfZRVYcWuBjDDHIFW+KPSEtBwF 4IeGSHj8tZxvSE7qrEmYH6yZG+Gazn3NPI7EKXYobfFpJB6TJnTb7lhW/v1AoVY2nmwq jxPUDY10r7I4JXG0XQ2lEnXiUefl7M1HJl/u2YBD25Y68El8OhW8Y3C1wpWvW/PhP2Ye HnhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722408036; x=1723012836; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Cbf59ISg0Sc5Hqb6uDTGlwoWxGdAcZEOLjHZD2II7GI=; b=kIpdYPK8VDWU0LnmjR2MVrHTwZBHLlr/xljNixD9v6BXMGcAlb/PIwRcYo1ji0D/bB nbYGTN2xvB27+Ou2y69rvLgb4XDEoyax+1NKDdI8c9T+FruKEein0ryYyNdP3tf6GYtg kKROPV7cIuKzBc5gzQRyhdjhpx8agbXZSYt/g/rbs5+fPfD8hyATTdl0f7WP8qD5W+f+ 3GUcxDRjt2k6izJ1+UlPWmPgp2Ul4xnOmsloTh2PmUwNdFH9/uf61UjAV7oUSwX1KyqP hsJ206iAwpA+nWk7yrLgnQflm+GnDbLHQgvusYUE15gMa2Lr1gA7vBS/IdirXRdDRuzy Optw== X-Gm-Message-State: AOJu0Ywb7DoC13V0+IvKstID1P2hQZJeV2e1jb/K4Amt6ziFgx5jKPqO pIIg1MzSgOoIrPhUJLp//d2Gyzy+fJA6OBPeYJYuEfo4mqx2iH+9 X-Google-Smtp-Source: AGHT+IEJ93w4g+iVpmsG4jhbXmbUA4p9LR1UnJI47uxCHnpaRaJdGJ2uK9srEkR/zrVadx/Si1EZ5g== X-Received: by 2002:a17:907:96a0:b0:a72:80b8:ba64 with SMTP id a640c23a62f3a-a7d3ffae4c1mr1144297266b.25.1722408035962; Tue, 30 Jul 2024 23:40:35 -0700 (PDT) Received: from smtpclient.apple ([188.212.112.125]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a7acab535b1sm731746366b.64.2024.07.30.23.40.34 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 30 Jul 2024 23:40:35 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.600.62\)) Subject: Re: Trigger usecase From: Alban Hertroys In-Reply-To: Date: Wed, 31 Jul 2024 08:40:24 +0200 Cc: pgsql-general Content-Transfer-Encoding: quoted-printable Message-Id: References: To: sud X-Mailer: Apple Mail (2.3774.600.62) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 30 Jul 2024, at 17:16, sud wrote: >=20 > Hello,=20 >=20 > We have a streaming application (using apache flink and kafka) which = populates data in the tables of a postgres database version 15.4. >=20 > Now while loading transactions data we also get some reference data = information from source (for example customer information) and for these = , we dont want to modify or override the existing customer data but want = to keep the old data with a flag as inactive and the new record should = get inserted with flag as active. So for such use case , should we cater = this inside the apache flink application code or should we handle this = using trigger on the table level which will execute on each INSERT and = execute this logic?=20 >=20 > I understand trigger is difficult to debug and monitor stuff. But here = in this case , team mates is saying , we shouldn't put such code logic = into a streaming application code so should rather handle through = trigger.=20 Is your data consistent if this operation doesn=E2=80=99t happen = correctly? Is it okay to have no, or multiple, records where the flag is = active for the same application transaction? The benefit of doing this in a trigger is that the operations happen in = a single database transaction, guaranteeing that there is only ever a = single row that has the active flag set for every application = transaction. There are other ways to guarantee that, using exclusion constraints = (which you should probably have on this table anyway), which would allow = to handle such in the application. Such constraints can raise exceptions = in your code, that need handling. So I say, at least put an exclusion constraint on that table if you = didn=E2=80=99t already, and then decide what approach suits you best. Alban Hertroys -- Als je de draak wilt steken met iemand, dan helpt het, als die een punthoofd heeft.