Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UGQRO-0004aO-Fh for pgsql-odbc@arkaria.postgresql.org; Fri, 15 Mar 2013 08:56:42 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UGQRN-0007JB-26 for pgsql-odbc@arkaria.postgresql.org; Fri, 15 Mar 2013 08:56:41 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UGQRL-0007J5-WB for pgsql-odbc@postgresql.org; Fri, 15 Mar 2013 08:56:40 +0000 Received: from smtp-outbound-2.vmware.com ([208.91.2.13]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UGQRJ-0003eU-7h for pgsql-odbc@postgresql.org; Fri, 15 Mar 2013 08:56:38 +0000 Received: from sc9-mailhost3.vmware.com (sc9-mailhost3.vmware.com [10.113.161.73]) by smtp-outbound-2.vmware.com (Postfix) with ESMTP id E6F39281CD for ; Fri, 15 Mar 2013 01:56:35 -0700 (PDT) Received: from zcs-prod-ext-mta-2.vmware.com (zcs-prod-ext-mta-2.vmware.com [10.113.62.224]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id E46C040081 for ; Fri, 15 Mar 2013 01:56:35 -0700 (PDT) Received: from localhost (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTP id 3C782C1539 for ; Fri, 15 Mar 2013 01:56:34 -0700 (PDT) X-Virus-Scanned: amavisd-new at zcs-prod-ext-mta-2.vmware.com Received: from zcs-prod-ext-mta-2.vmware.com ([127.0.0.1]) by localhost (zcs-prod-ext-mta-2.vmware.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id MybaBoWrnqUW for ; Fri, 15 Mar 2013 01:56:34 -0700 (PDT) Received: from zcs-prod-ext-mta-2.vmware.com (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTP id 5A0D8C1523 for ; Fri, 15 Mar 2013 01:45:35 -0700 (PDT) Received: from [192.168.1.90] (zimbra-prod-ext-proxy-vip.vmware.com [10.113.63.87]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTPSA for ; Fri, 15 Mar 2013 01:45:34 -0700 (PDT) Message-ID: <5142DFAE.1060408@vmware.com> Date: Fri, 15 Mar 2013 10:45:34 +0200 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12) Gecko/20130116 Icedove/10.0.12 MIME-Version: 1.0 To: pgsql-odbc@postgresql.org Subject: QR_Destructor should iterate, not recurse, chained result sets Content-Type: multipart/mixed; boundary="------------040607090408040608060201" X-Pg-Spam-Score: -9.3 (---------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-odbc Precedence: bulk Sender: pgsql-odbc-owner@postgresql.org This is a multi-part message in MIME format. --------------040607090408040608060201 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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 --------------040607090408040608060201 Content-Type: text/x-diff; name="psqlodbc-avoid-recursion-in-SQLFreeHandle-2.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="psqlodbc-avoid-recursion-in-SQLFreeHandle-2.patch" *** 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 --------------040607090408040608060201 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc --------------040607090408040608060201--