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 1lBgj3-00061C-N5 for pgsql-general@arkaria.postgresql.org; Mon, 15 Feb 2021 16:27:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lBgj2-00056n-JI for pgsql-general@arkaria.postgresql.org; Mon, 15 Feb 2021 16:27:52 +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 1lBgj2-00056c-At for pgsql-general@lists.postgresql.org; Mon, 15 Feb 2021 16:27:52 +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 1lBgj0-0007JR-2a for pgsql-general@lists.postgresql.org; Mon, 15 Feb 2021 16:27:51 +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 11FGRngB2254394; Mon, 15 Feb 2021 11:27:49 -0500 From: Tom Lane To: Marc Millas cc: "pgsql-general@lists.postgresql.org" Subject: Re: prepare in a do loop In-reply-to: References: Comments: In-reply-to Marc Millas message dated "Mon, 15 Feb 2021 17:18:48 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2254392.1613406469.1@sss.pgh.pa.us> Date: Mon, 15 Feb 2021 11:27:49 -0500 Message-ID: <2254393.1613406469@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Marc Millas writes: > in psql, with a postgres 12.5 db on a centos 7 intel: > I do create a function named randname() returning a varchar, and a table > matable with a column prenom varchar(50). then > postgres=# prepare moninsert(varchar) as > postgres-# insert into matable(prenoms) values($1); > PREPARE > I test it: > postgres=# execute moninsert(randname()); > INSERT 0 1 > up to now, everything fine. then: > do $$ begin for counter in 1..1000000 loop execute > moninsert(randname());end loop;end;$$; > ERREUR: la fonction moninsert(character varying) n'existe pas > LIGNE 1 : SELECT moninsert(randname()) > someone can explain ? EXECUTE in plpgsql has nothing whatever to do with the SQL EXECUTE command. See the respective documentation. You don't actually need to use SQL PREPARE/EXECUTE in plpgsql. If you just write "insert into ..." as a command in a plpgsql function, it's automatically prepared behind the scenes. Indeed, one of the common uses for plpgsql's EXECUTE is to stop a prepared plan from being used when you don't want that ... so far from being the same thing, they're more nearly opposites. Perhaps a different name should have been chosen, but we're stuck now. regards, tom lane