pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: will.pearson@digital.cabinet-office.gov.uk
To: pgsql-bugs@postgresql.org
Subject: BUG #14403: Large numbers of CREATE OR UPDATE function causes memory exhaustion
Date: Fri, 28 Oct 2016 16:23:17 +0000
Message-ID: <20161028162317.15991.74004@wrigleys.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

The following bug has been logged on the website:

Bug reference:      14403
Logged by:          Will Pearson
Email address:      will.pearson@digital.cabinet-office.gov.uk
PostgreSQL version: 9.6.1
Operating system:   OSX El Capitan (also in Linux Containers/RDS)
Description:        

Summary:

We had a problem in production where RDS failed over availability zones. We
later found this was due to a memory overflow. Doing more digging it seems
to due to large numbers of CREATE OR UPDATE functions done by sequelize (a
node ORM) during session creation. We've mitigated the problem.

We've now isolated the problem from our code and have created the following
python script doing standard sql.   But we could do with more understanding
of postgresql internals how to dig further (and any tuning possibilities).


----
Python script to replicate the problems

import psycopg2
import sys

def create_or_replace (n, cur, conn):
  print ("Doing action " + str(n)) 
  cur.execute("CREATE OR REPLACE FUNCTION pg_temp.testfunc(OUT response
test, OUT sequelize_caught_exception text) RETURNS RECORD AS $func_a7" +
str(n) + "$ BEGIN INSERT INTO test (\"id\") VALUES (" + str(n) + ")
RETURNING * INTO response; EXCEPTION WHEN unique_violation THEN GET STACKED
DIAGNOSTICS sequelize_caught_exception = PG_EXCEPTION_DETAIL; END $func_a7"
+ str(n) + "$ LANGUAGE plpgsql;")
  cur.execute("SELECT (testfunc.response).*,
testfunc.sequelize_caught_exception FROM pg_temp.testfunc();")
  cur.execute("DROP FUNCTION IF EXISTS pg_temp.testfunc();")
  conn.commit()

def lots_of_create_or_replace(t, cur, conn) :
  print ("Doing lots of actions for t" + str(t)) 
  for n in range(1, 100000000):
     create_or_replace(n+t, cur, conn)
  print "Finished"

conn = psycopg2.connect("dbname =testdb")

cur = conn.cursor()
cur.execute("DELETE FROM test;")
lots_of_create_or_replace(int(sys.argv[1]), cur,conn)

#Usage#
Create the required tables and database then run it with a starting point.
The code is designed so that you can run multiple copies at different
points.

Look at the memory usage go up on the python processes.

Killing the python process doing the updates means the memory is freed. On
some tests against our dev environments it appeared that simply stopping
doing the requests to create sessions allowed the system to recover the
memory.

----
More details:

Seen in RDS (9.4.4)
locally in Mac 9.5.4
On Linux in a container 9.4.4

Sequelize code that shows it in the wild:

https://github.com/sequelize/sequelize/blob/95d3a5713499ab90b19d7a312b8cb878b26eb1b4/lib/dialects/ab...

----

Hypotheses I've looked at - Reason I don't think that is the problem:

Functions aren't being cleaned up properly - \df doesn't show more functions
during the run. Although a colleague points out that these temp functions
are per connection, so wouldn't show up. We're not sure how to see them.
No vacuums happening - There are vacuums happening during memory usage
increase.

Current hypothesis:

The process that frees up memory for temporary functions doesn't have a
chance to act because of the consistent number of inserts and deletions
pre-empting it. It is not a normal vacuum to deal with it. 

Thanks for any help,

 Will Pearson
 WebOps


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs


view thread (3+ messages)  latest in thread

Message-ID: <20161028162317.15991.74004@wrigleys.postgresql.org>
Permalink:  ../20161028162317.15991.74004@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/20161028162317.15991.74004@wrigleys.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: will.pearson@digital.cabinet-office.gov.uk
  Subject: Re: BUG #14403: Large numbers of CREATE OR UPDATE function causes memory exhaustion
  In-Reply-To: <20161028162317.15991.74004@wrigleys.postgresql.org>
  References: <20161028162317.15991.74004@wrigleys.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox