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 1pmz5U-00011G-JX for pgsql-interfaces@arkaria.postgresql.org; Thu, 13 Apr 2023 15:42:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pmz5T-00036O-Bi for pgsql-interfaces@arkaria.postgresql.org; Thu, 13 Apr 2023 15:42:15 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pmz5T-00036C-4Z for pgsql-interfaces@lists.postgresql.org; Thu, 13 Apr 2023 15:42:15 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pmz5Q-0006M0-VS for pgsql-interfaces@postgresql.org; Thu, 13 Apr 2023 15:42:14 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 33DFgB8W1342581; Thu, 13 Apr 2023 11:42:11 -0400 From: Tom Lane To: pgchem pgchem cc: "pgsql-interfaces@postgresql.org" Subject: Re: C trigger significantly slower than PL/pgSQL? In-reply-to: <1595558642.2519076.1681378287741@webmail.strato.de> References: <1595558642.2519076.1681378287741@webmail.strato.de> Comments: In-reply-to pgchem pgchem message dated "Thu, 13 Apr 2023 11:31:27 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1342579.1681400531.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 13 Apr 2023 11:42:11 -0400 Message-ID: <1342580.1681400531@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk pgchem pgchem writes: > as requested, here is the C code: > ... > ret =3D SPI_execute("SELECT pg_backend_pid() =3D ANY((SELECT pid FROM pg= _stat_activity WHERE backend_type =3D 'logical replication worker'))::bool= ean AS is_replicated", true, 1); Yeah, there's your problem. Each time through the trigger, that query is being parsed, planned, and executed from scratch. plpgsql knows how to cache the planned statement, so it's doing less work per-call. regards, tom lane