X-Original-To: pgsql-hackers-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 07A5FD1C91D for ; Fri, 25 Jul 2003 17:28:39 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 57092-03 for ; Fri, 25 Jul 2003 14:27:50 -0300 (ADT) Received: from varlena.com (adsl-63-207-12-154.dsl.snfc21.pacbell.net [63.207.12.154]) by svr1.postgresql.org (Postfix) with ESMTP id AD6FAD1C921 for ; Fri, 25 Jul 2003 14:28:36 -0300 (ADT) Received: by varlena.com (Postfix on SuSE Linux 7.3 (i386), from userid 500) id 8535F394BA; Fri, 25 Jul 2003 10:24:08 -0700 (PDT) Date: Fri, 25 Jul 2003 10:24:08 -0700 From: elein To: Tom Lane Cc: Joe Conway , "Hackers (PostgreSQL)" Subject: Re: best way to determine start of new statement within a function? (resend) Message-ID: <20030725102408.C3643@cookie> References: <3F206EB3.9010904@joeconway.com> <5630.1059108791@sss.pgh.pa.us> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <5630.1059108791@sss.pgh.pa.us> User-Agent: Mutt/1.3.22.1i X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/796 X-Sequence-Number: 41401 The semantics of this conceptually is not too bad. The implementation could be tricky. For any given DML or sub-DML (select, eg) the scope should be for that DML. The DML is the "parent" of the function. The DML is the statement context and the function is the function context. statement --> function | + --> statement --> function | + --> statement statement --> function1, function2 | | | +--> statement -> function3 | | | +--> statement | | | + --> statement --> function4 | + --> statement For example: select myfunc() from foo; -- 1 statement select myfunc() from (select myfunc2() from foo); -- subselect ( select ): both actually have the same memory duration, but have different statement contexts and different function contexts. -- the scope of each matches its select select myfunc() from foo; --where myfunc invokes select myfunc2() from bar; The scope of myfunc is the parent select. The scope of myfunc2 resets at each iteration of myfunc(), but is available for each iteration of itself. Note that these things can nest very deeply. select myfunc() from foo where myfunc3() = 't'; -- both myfunc() and myfunc3() have the same statement parent. -- if myfunc3() invoked select * from myfunc4() then each iteration of myfunc3() resets the statement context of that select. The new statement corresponds to a single invocation of one DML. Memory should last through the whole invocation loop. Am I making sense here? Where does the function context info memory get available for allocation and where does it get deallocated? (It does get deallocated via garbage collection, right?) I think the scope of this is correct. We implemented this at informix and got a lot of stuff wrong. Memory pool hell, but we had to deal with a multi-threaded server. Illustra did it right so I think postgres already has the handles to the hook the memory into the right scope. Do you want me to try to write this up into more formal definitions? elein On Fri, Jul 25, 2003 at 12:53:11AM -0400, Tom Lane wrote: > Joe Conway writes: > > Specifically I'd like to reset a variable within my PL/R interpreter > > each time a new SQL statement begins. > > Define "new SQL statement". In particular, what of a PL function > executing multiple SQL statements inside an outer SQL statement that > invoked the function? Unless you've got a clear idea of the semantics > you want, it's not going to be very profitable to discuss > implementations ... > > regards, tom lane > > ---------------------------(end of broadcast)--------------------------- > TIP 5: Have you checked our extensive FAQ? > > http://www.postgresql.org/docs/faqs/FAQ.html >