On Fri, Jan 14, 2011 at 19:14, Daniel Popowich <danielpopowich@gmail.com> wrote:
[ snip ]
> CREATE FUNCTION pygaps(start_ts timestamp without time zone, end_ts timestamp without time zone, gap_length interval)
RETURNSSETOF timerange
> LANGUAGE plpythonu
> AS $$
>
> # because pg passes date/time to python as strings I'm using pg to
> # recompute values as seconds so I have numbers to do math
>
> gap = plpy.execute("select extract(epoch from '%s'::interval) as sec"
> % gap_length)[0]['sec']
>
> results = plpy.execute("""select ts, extract(epoch from ts) as epoch
> from timeseries
> where ts between '%s' and '%s'"""
> % (start_ts, end_ts))
> if results.nrows() < 2:
> return
>
> # prime the well by setting prev(ious) to the first tic and
> # iterate starting with the second...
> prev = results[0]
> for curr in results[1:]:
FYI if I don't use a slice copy here I can't get it to leak. ( find my
test case at the end ) I don't know enough about python to know if
thats a pl/python issue or python doing what its told-- having never
really wrote any python myself.
---------------
-- leaks big time
CREATE or replace FUNCTION pygaps_leak() RETURNS void
LANGUAGE plpythonu
AS $$
results = plpy.execute("""select generate_series(0, 1000000)""")
prev = results[0]
for curr in results[1:]:
prev = curr
return
-- does not leak
CREATE or replace FUNCTION pygaps_no_leak() RETURNS void
LANGUAGE plpythonu
AS $$
results = plpy.execute("""select generate_series(0, 1000000)""")
prev = results[0]
for curr in range(1, len(results)):
prev = curr
return