pg.ddx.io  pgsql-odbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Heikki Linnakangas <hlinnakangas@vmware.com>
To: pgsql-odbc@postgresql.org
Subject: QR_Destructor should iterate, not recurse, chained result sets
Date: Fri, 15 Mar 2013 10:45:34 +0200
Message-ID: <5142DFAE.1060408@vmware.com> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-odbc>

QR_Destructor() calls QR_close_result(), which free's all the resources 
associated with the result set. It also calls QR_Destructor() on the 
next result set in the chain. In other words, when you call 
QR_Destructor() on the first result, it recurses to free all the chained 
results.

That recursion is a problem. A chained result set occurs at least if you 
use array binding; there is one chained result for each element in the 
parameter array. ODBC applications typically use parameter arrays to 
bulk insert data, in which case the parameter array can be very large.

I bumped into this in an application that bound over 10000 parameters in 
a statement. That works otherwise, but 10000-level deep recursion 
sometimes causes the application to run out of stack space. The funny 
thing is that the recursion happens when the statement is free'd; other 
operations don't recurse.

It's pretty obvious that QR_destructor/QR_close_result() should iterate, 
not recurse, through the chained results. Patch attached. Aside from 
avoiding running out of stack space, this speeds up freeing the 
statement considerably.

I've also pushed this to my github repository at 
https://github.com/hlinnaka/psqlodbc. I also added a test case for array 
binding parameters to the master-with-testcases branch in the same 
repository.

- Heikki

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

Attachments:

psqlodbc-avoid-recursion-in-SQLFreeHandle-2.patch (text/x-diff, 3.2K) download *** a/qresult.c --- b/qresult.c *************** *** 216,281 **** void QR_close_result(QResultClass *self, BOOL destroy) { ConnectionClass *conn; if (!self) return; mylog("QResult: in QR_close_result\n"); ! /* ! * If conn is defined, then we may have used "backend_tuples", so in ! * case we need to, free it up. Also, close the cursor. ! */ ! if ((conn = QR_get_conn(self)) && conn->sock) { ! if (CC_is_in_trans(conn) || ! QR_is_withhold(self)) { ! if (!QR_close(self)) /* close the cursor if there is one */ { } } - } ! QR_free_memory(self); /* safe to call anyway */ ! /* Should have been freed in the close() but just in case... */ ! QR_set_cursor(self, NULL); ! /* Free up column info */ ! if (destroy && self->fields) ! { ! CI_Destructor(self->fields); ! self->fields = NULL; ! } ! /* Free command info (this is from strdup()) */ ! if (self->command) ! { ! free(self->command); ! self->command = NULL; ! } ! /* Free message info (this is from strdup()) */ ! if (self->message) ! { ! free(self->message); ! self->message = NULL; ! } ! /* Free notice info (this is from strdup()) */ ! if (self->notice) ! { ! free(self->notice); ! self->notice = NULL; } - /* Destruct the result object in the chain */ - QR_Destructor(self->next); - self->next = NULL; mylog("QResult: exit close_result\n"); - if (destroy) - { - free(self); - } } void --- 216,293 ---- QR_close_result(QResultClass *self, BOOL destroy) { ConnectionClass *conn; + QResultClass *next; + BOOL top = TRUE; if (!self) return; mylog("QResult: in QR_close_result\n"); ! while(self) { ! /* ! * If conn is defined, then we may have used "backend_tuples", so in ! * case we need to, free it up. Also, close the cursor. ! */ ! if ((conn = QR_get_conn(self)) && conn->sock) { ! if (CC_is_in_trans(conn) || QR_is_withhold(self)) { + if (!QR_close(self)) /* close the cursor if there is one */ + { + } } } ! QR_free_memory(self); /* safe to call anyway */ ! /* ! * Should have been freed in the close() but just in case... ! * QR_set_cursor clears the cursor name of all the chained results too, ! * so we only need to do this for the first result in the chain. ! */ ! if (top) ! QR_set_cursor(self, NULL); ! /* Free up column info */ ! if (destroy && self->fields) ! { ! CI_Destructor(self->fields); ! self->fields = NULL; ! } ! /* Free command info (this is from strdup()) */ ! if (self->command) ! { ! free(self->command); ! self->command = NULL; ! } ! /* Free message info (this is from strdup()) */ ! if (self->message) ! { ! free(self->message); ! self->message = NULL; ! } ! /* Free notice info (this is from strdup()) */ ! if (self->notice) ! { ! free(self->notice); ! self->notice = NULL; ! } ! /* Destruct the result object in the chain */ ! next = self->next; ! self->next = NULL; ! if (destroy) ! free(self); ! ! /* Repeat for the next result in the chain */ ! self = next; ! destroy = TRUE; /* always destroy chained results */ ! top = FALSE; } mylog("QResult: exit close_result\n"); } void

Message-ID: <5142DFAE.1060408@vmware.com>
Permalink:  ../5142DFAE.1060408@vmware.com/
Also on:    postgresql.org/message-id/5142DFAE.1060408@vmware.com

 ·  · 

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-odbc@postgresql.org
  Cc: hlinnakangas@vmware.com
  Subject: Re: QR_Destructor should iterate, not recurse, chained result sets
  In-Reply-To: <5142DFAE.1060408@vmware.com>
  References: <5142DFAE.1060408@vmware.com>

* 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