Subject: Re: Backend crashes--is it me?

From: [email-masked] (John Campbell)
Date: Thu, 17 Dec 92 11:32:05 -0800

This is another follow up to my previous bug report/question.
On Dec 11 at  4:08pm, John Campbell writes:
 >From [email-masked] Fri Dec 11 20:32:21 1992
 >Date: Fri, 11 Dec 92 16:08:07 -0800
 >Message-Id: <[email-masked]>
 >From: [email-masked] (John Campbell)

 >Platform:  SS2 running SunOS 4.1.2
 >Postgres: 4.0.1
 >
In the previous message I had included a script file and a short
'C' program that made the backend either hang or crash.  I was
worried that the 'C' program was causing the problem but I now
believe it is not the culprit.

1) I reduced the 'C' program to the following and it still
   crashed the backend:

      #include "tmp/postgres.h"
      text *ip_text (tval)
      text *tval;
      {
         return tval;  /* Useless and trivial-- echo */
      }

2) I even removed the 'C' program all together and the following
   script by itself caused the backend to hang:

/* Learn by playing with a small test set. */

destroy VENDOR \p\g
destroy COMPUTER \p\g
remove function vendor \p \g
remove function ip_text \p \g
remove function ip \p \g

create VENDOR (name = text, phone=text) \p\g
create COMPUTER (name = text, model=text, _ven=text) \p\g

define function vendor
 (language = "postquel", returntype = VENDOR)
 arg is (COMPUTER)
 as "retrieve (VENDOR.all) where VENDOR.name = $1._ven" \p\g

/* populate with 2 VENDORs and 3 COMPUTERs */
append VENDOR (name = "SUN", phone="1234") \p\g
append VENDOR (name = "DEC", phone="5678") \p\g

append COMPUTER (name = "sunset.cse.nau.edu", model="SS2", _ven="SUN") \p\g
append COMPUTER (name = "slate.cse.nau.edu", model="SS2", _ven="SUN") \p\g
append COMPUTER (name = "naucse.cse.nau.edu", model="MVII", _ven="DEC") \p\g

/* Now define a different function. */
define function ip
   (language="postquel", returntype = text)
   arg is (COMPUTER)
   as "retrieve ($1.name)" \p\g

retrieve (COMPUTER.ip, COMPUTER.name) where COMPUTER.name = "naucse.cse.nau.edu" \p\g

retrieve (COMPUTER.name, COMPUTER.ip) where 1=1 \p\g

/* Now we hang, I believe because of the previous query */
retrieve (COMPUTER.name, COMPUTER.ip) \p\g

-- 
	John Campbell               [email-masked]
        [email-masked]      [email-masked]

---

From: <[email-masked]>
Date: Thu, 17 Dec 92 11:37:48 PST

>          return tval;  /* Useless and trivial-- echo */

the return value from a dynamically-loaded user function, if that value
is pass-by-reference, must be allocated by palloc(), because it's
immediately freed by the backend when it's no longer needed.  this no-op
routine causes postgres to free memory that it believes *you* have
allocated, but which has actually been allocated by *it*, and which
it still needs.  this is causing the core dump.

> retrieve (COMPUTER.name, COMPUTER.ip) \p\g

this one will take a little more investigation; we'll file it in the
list of bugs we'll look at for the next release.

					mike