Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nu0Xu-0006T0-AA for pgsql-sql@arkaria.postgresql.org; Wed, 25 May 2022 23:36:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nu0Xs-00055N-Rh for pgsql-sql@arkaria.postgresql.org; Wed, 25 May 2022 23:36:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nu0Xs-00055E-Hg for pgsql-sql@lists.postgresql.org; Wed, 25 May 2022 23:36:04 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nu0Xm-0000Tt-Cv for pgsql-sql@lists.postgresql.org; Wed, 25 May 2022 23:36:04 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Type:MIME-Version:References:Reply-To: Message-ID:Subject:Cc:To:Sender:From:Date:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=nlrMBeH4+QZBozL6+US66aOPUDsB73ynPIgiqiJsX/8=; b=u1b/T9WI4z4MiMyz/rGQsKn4Ne pI3UEzkBkD8zWw6FDm8Unf0OzOI14bO36T1SxpOBeCd1uZkveYdilSdPUtJF4kzS+UG+sBLt/3nIQ 2b9t0aSGrA1KPfaW1x18yfwApILa/qzx/gtaJSsIvWu4yhSJ496C1Sq+6AaInEYCiMbw=; Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by depesz.com with esmtpa (Exim 4.92) (envelope-from ) id 1nu0Xk-0002vV-G1; Thu, 26 May 2022 01:35:56 +0200 Date: Thu, 26 May 2022 01:35:56 +0200 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: Hui Jackson Cc: pgsql-sql@lists.postgresql.org Subject: Re: Using Transaction with if-else in prepared statement Message-ID: <20220525233556.GA9042@depesz.com> Reply-To: depesz@depesz.com References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, May 25, 2022 at 07:12:49PM +0800, Hui Jackson wrote: > I am trying to make transaction in nodejs > The logic will be check if sufficient coin in sender's amount, if > sufficient then do transaction. > I am new to postgres, not sure if this is a right way to do so, if you have > a better solution, please let me know, thank you. I would do i by issuing *only* INSERT INTO coin_history(sender_id, receiver_id, amount) VALUES and then changing coin data with triggers plus adding constraints that app_user.coin can never be < 0. depesz