Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1caUjG-0000YZ-00 for pgsql-bugs@arkaria.postgresql.org; Sun, 05 Feb 2017 21:52:14 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1caUjF-0002RA-EM for pgsql-bugs@arkaria.postgresql.org; Sun, 05 Feb 2017 21:52:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1caUjE-0002Qm-NT for pgsql-bugs@postgresql.org; Sun, 05 Feb 2017 21:52:12 +0000 Received: from smtp5.hushmail.com ([65.39.178.142]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1caUj5-00036o-LV for pgsql-bugs@postgresql.org; Sun, 05 Feb 2017 21:52:11 +0000 Received: from smtp5.hushmail.com (localhost [127.0.0.1]) by smtp5.hushmail.com (Postfix) with SMTP id 1599D20201 for ; Sun, 5 Feb 2017 21:51:59 +0000 (UTC) X-hush-tls-connected: 1 Received: from smtp.hushmail.com (w2.hushmail.com [65.39.178.46]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp5.hushmail.com (Postfix) with ESMTPS for ; Sun, 5 Feb 2017 21:51:58 +0000 (UTC) Received: by smtp.hushmail.com (Postfix, from userid 99) id AD118E06FA; Sun, 5 Feb 2017 21:51:58 +0000 (UTC) MIME-Version: 1.0 Date: Sun, 05 Feb 2017 16:51:58 -0500 To: pgsql-bugs@postgresql.org Subject: plpython bug From: joel.traf@magwerks.com Content-Type: multipart/alternative; boundary="=_2c908f23e6a4c0432fbe35665fd6c6ac" Message-Id: <20170205215158.AD118E06FA@smtp.hushmail.com> X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --=_2c908f23e6a4c0432fbe35665fd6c6ac Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="UTF-8" Hello Developers found an interesting bug that causes a complete crash and resetting of all connections to PG 9.5.0 compiled Visual C++ 1800, 64 bit log states server process (PID 2720) was terminated by exception 0XC0000005 failed process was running: select upsert_items2(false) Hint: see C include file ntstatus.h for a description of hexadecimal value (this error is a memory access error) log: Terminating any other active server processes Upsert_items2:: Is a function that copies/updates data from ERP database to a Website database. It just iterates over all the items, figures out how to group items together based on class, decides if the item should be seen on the website based on rules, Nothing complicated. As these are two different PG databases, the plpython connects to the erp database via psycopg2 copying the data into local plpython tuple result set. I then just executed the plpy.execute("insert on conflict do update") for each item. processing time was around 5 minutes to go through 12,000 items updating 5 tables on the website database. The users want this to be faster. The only solution i could think of was to cut down on all context switching and data type mapping that happens when calling plpy.execute() for each item. So decided to just build a really big SQL update command then at the end plpython function call plpy.execute(). That worked and processing time was reduced from 5 minutes to 8 seconds. As this is work in process I started adding in more rules that change what gets updated and i had a couple of typo's in the SQL statements. That's when i crashed the PG server hard. Its taken me a few hours to figure out were my mistake was, as this crash will only occur if a SQL error is very deep in the command sent via plpy.execute(). If there is an error in the first few lines it returns plpython SPI error. How deep into the SQL command an error has to be appear to cause this access error i do not know. The length of the command sent via plpy.execute() is 1,454,561 characters long Any suggestions on how to narrow down at what point a bad command causes this error?? FYI only The first write of this function was written in plpgsql using DbLink which took 15 minutes to process the same data-set, second write used FDW processing time was still 15 minutes, in both cases it spent all the time getting data from the remote PG database doing executing Fetchs. Thanks --=_2c908f23e6a4c0432fbe35665fd6c6ac Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="UTF-8" Hello Developers
found an interesting bug that causes a complete crash and resetting of al= l connections to PG 9.5.0 compiled Visual C++ 1800, 64 bit
log states&nb= sp; server process (PID 2720) was terminated by exception 0XC0000005
fa= iled process was running: select upsert_items2(false)
Hint: see C includ= e file ntstatus.h for a description of hexadecimal value  (this error = is a memory access error)
log: Terminating any other active server proce= sses

Upsert_items2::  Is a function that copies/updates = data from ERP database to a Website database. It just iterates over all the= items, figures out how to group items together based on class, decides if = the item should be seen on the website based on rules,  Nothing compli= cated.

As these are two different PG  databases, the plpython c= onnects to the erp database via psycopg2 copying the data into local plpyth= on tuple result set.   I then just executed the plpy.execute("ins= ert on conflict do update") for each item.  processing time was around= 5 minutes to go through 12,000 items updating 5 tables on the website data= base.

The users want this to be faster. The only solution i could th= ink of was to cut down on all context switching and data type mapping that = happens when calling plpy.execute() for each item.  So decided to just= build a really big SQL update command  then at the end plpython funct= ion call plpy.execute().

That worked and processing time was reduced= from 5 minutes to 8 seconds.  As this is work in process I started ad= ding in more rules that change what gets updated  and i had a couple o= f typo's  in the SQL statements.

That's when i crashed the PG s= erver hard.  Its taken me a few hours to figure out were my mistake wa= s, as this crash will only occur if a SQL error is very deep in the command= sent via plpy.execute().  If there is an error in the first few lines= it returns plpython SPI error.   How deep into the SQL command an err= or has to be appear to cause this access error i do not know.

The l= ength of the command sent via plpy.execute()  is 1,454,561  chara= cters long

Any suggestions on how to narrow down at what point a ba= d command causes this error??


FYI only
The first write of th= is function was written in plpgsql  using DbLink which took 15 minutes= to process the same data-set,  second write used FDW processing time = was still 15 minutes, in both cases it spent all the time getting data from= the remote PG database doing executing Fetchs. 


Thanks
--=_2c908f23e6a4c0432fbe35665fd6c6ac--