return rs->sr_err = LDAP_OTHER;
        }
 
-       backsql_BindRowAsStrings( asth, &row );
+       backsql_BindRowAsStrings_x( asth, &row, op->o_tmpmemctx );
        for ( rc = SQLFetch( asth );
                        BACKSQL_SUCCESS( rc );
                        rc = SQLFetch( asth ) )
                                                sth, rc );
 
                                rs->sr_text = "SQL-backend error";
-                               return rs->sr_err = LDAP_OTHER;
+                               rs->sr_err = LDAP_OTHER;
+                               goto done;
                        }
 
                        if ( BACKSQL_IS_DEL( at->bam_expect_return ) ) {
                                        SQLFreeStmt( sth, SQL_DROP );
 
                                        rs->sr_text = "SQL-backend error";
-                                       return rs->sr_err = LDAP_OTHER;
+                                       rs->sr_err = LDAP_OTHER;
+                                       goto done;
                                }
 
                        } else {
                                SQLFreeStmt( sth, SQL_DROP );
 
                                rs->sr_text = "SQL-backend error";
-                               return rs->sr_err = LDAP_OTHER;
+                               rs->sr_err = LDAP_OTHER;
+                               goto done;
                        }
 #ifdef BACKSQL_ARBITRARY_KEY
                        Debug( LDAP_DEBUG_TRACE,
                                SQLFreeStmt( sth, SQL_DROP );
 
                                rs->sr_text = "SQL-backend error";
-                               return rs->sr_err = LDAP_OTHER;
+                               rs->sr_err = LDAP_OTHER;
+                               goto done;
                        }
         
                        Debug( LDAP_DEBUG_TRACE, 
                                }
                                rs->sr_text = op->o_req_dn.bv_val;
                                SQLFreeStmt( sth, SQL_DROP );
-                               return rs->sr_err;
+                               goto done;
                        }
                        SQLFreeStmt( sth, SQL_DROP );
                }
        }
-       backsql_FreeRow( &row );
+
+       rs->sr_err = LDAP_SUCCESS;
+
+done:;
+       backsql_FreeRow_x( &row, op->o_tmpmemctx );
        SQLFreeStmt( asth, SQL_DROP );
 
-       return LDAP_SUCCESS;
+       return rs->sr_err;
 }
 
 int
 
                goto done;
        }
 
-       rs->sr_err = backsql_has_children( bi, dbh, &op->o_req_ndn );
+       rs->sr_err = backsql_has_children( op, dbh, &op->o_req_ndn );
        switch ( rs->sr_err ) {
        case LDAP_COMPARE_FALSE:
                rs->sr_err = LDAP_SUCCESS;
 
                goto done;
        }
 
-       backsql_BindRowAsStrings( sth, &row );
+       backsql_BindRowAsStrings_x( sth, &row, op->o_tmpmemctx );
        rc = SQLFetch( sth );
        if ( BACKSQL_SUCCESS( rc ) ) {
                char    buf[ SLAP_TEXT_BUFLEN ];
                        }
                }
        }
-       backsql_FreeRow( &row );
 
 done:;
+       backsql_FreeRow_x( &row, op->o_tmpmemctx );
+
        Debug( LDAP_DEBUG_TRACE,
                "<==backsql_dn2id(\"%s\"): err=%d\n",
                ndn->bv_val, res, 0 );
 
 int
 backsql_count_children(
-       backsql_info            *bi,
+       Operation               *op,
        SQLHDBC                 dbh,
        struct berval           *dn,
        unsigned long           *nchildren )
 {
+       backsql_info            *bi = (backsql_info *)op->o_bd->be_private;
        SQLHSTMT                sth = SQL_NULL_HSTMT;
        BACKSQL_ROW_NTS         row;
        RETCODE                 rc;
                return LDAP_OTHER;
        }
 
-       backsql_BindRowAsStrings( sth, &row );
+       backsql_BindRowAsStrings_x( sth, &row, op->o_tmpmemctx );
        
        rc = SQLFetch( sth );
        if ( BACKSQL_SUCCESS( rc ) ) {
        } else {
                res = LDAP_OTHER;
        }
-       backsql_FreeRow( &row );
+       backsql_FreeRow_x( &row, op->o_tmpmemctx );
 
        SQLFreeStmt( sth, SQL_DROP );
 
 
 int
 backsql_has_children(
-       backsql_info            *bi,
+       Operation               *op,
        SQLHDBC                 dbh,
        struct berval           *dn )
 {
        unsigned long   nchildren;
        int             rc;
 
-       rc = backsql_count_children( bi, dbh, dn, &nchildren );
+       rc = backsql_count_children( op, dbh, dn, &nchildren );
 
        if ( rc == LDAP_SUCCESS ) {
                return nchildren > 0 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
        BACKSQL_ROW_NTS         row;
        unsigned long           i,
                                k = 0,
-                               oldcount = 0;
+                               oldcount = 0,
+                               res = 0;
 #ifdef BACKSQL_COUNTQUERY
        unsigned long           count,
                                countsize = sizeof( count ),
                return 1;
        }
 
-       backsql_BindRowAsStrings( sth, &row );
+       backsql_BindRowAsStrings_x( sth, &row, bsi->bsi_op->o_tmpmemctx );
 #ifdef BACKSQL_COUNTQUERY
        j = oldcount;
 #endif /* BACKSQL_COUNTQUERY */
                                                "in schema (%d)\n",
                                                bsi->bsi_e->e_name.bv_val,
                                                row.col_names[ i ].bv_val, retval );
-                                       return 1;
+                                       res = 1;
+                                       goto done;
                                }
 
                                if ( ad != at->bam_ad ) {
                                                bsi->bsi_e->e_name.bv_val,
                                                ad->ad_cname.bv_val,
                                                at->bam_ad->ad_cname.bv_val );
-                                       return 1;
+                                       res = 1;
+                                       goto done;
                                }
 #endif /* BACKSQL_TRACE */
 
        }
 #endif /* BACKSQL_COUNTQUERY */
 
-       backsql_FreeRow( &row );
        SQLFreeStmt( sth, SQL_DROP );
        Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 );
 
        if ( at->bam_next ) {
-               return backsql_get_attr_vals( at->bam_next, v_bsi );
+               res = backsql_get_attr_vals( at->bam_next, v_bsi );
+       } else {
+               res = 1;
        }
 
-       return 1;
+#ifdef BACKSQL_TRACE
+done:;
+#endif /* BACKSQL_TRACE */
+       backsql_FreeRow_x( &row, bsi->bsi_op->o_tmpmemctx );
+
+       return res;
 }
 
 int
 
                goto done;
        }
 
-       if ( backsql_has_children( bi, dbh, &op->o_req_ndn ) == LDAP_COMPARE_TRUE ) {
+       if ( backsql_has_children( op, dbh, &op->o_req_ndn ) == LDAP_COMPARE_TRUE ) {
                Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
                        "entry \"%s\" has children\n",
                        op->o_req_dn.bv_val, 0, 0 );
 
                        && !got[ BACKSQL_OP_HASSUBORDINATES ]
                        && attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_hasSubordinates ) == NULL )
        {
-               rc = backsql_has_children( bi, dbh, &rs->sr_entry->e_nname );
+               rc = backsql_has_children( op, dbh, &rs->sr_entry->e_nname );
 
                switch( rc ) {
                case LDAP_COMPARE_TRUE:
 
                int matched, int muck );
 
 /* stores in *nchildren the count of children for an entry */
-int backsql_count_children( backsql_info *bi, SQLHDBC dbh,
+int backsql_count_children( Operation *op, SQLHDBC dbh,
                struct berval *dn, unsigned long *nchildren );
 
 /* returns LDAP_COMPARE_TRUE/LDAP_COMPARE_FALSE if the entry corresponding
  * to DN has/has not children */
-int backsql_has_children( backsql_info *bi, SQLHDBC dbh, struct berval *dn );
+int backsql_has_children( Operation *op, SQLHDBC dbh, struct berval *dn );
 
 /* frees *id and returns next in list */
 backsql_entryID *backsql_free_entryID( Operation *op, backsql_entryID *id,
        backsql_BindParamInt( (sth), (par_ind), (io), (id) )
 #endif /* ! BACKSQL_ARBITRARY_KEY */
 
+RETCODE backsql_BindRowAsStrings_x( SQLHSTMT sth, BACKSQL_ROW_NTS *row, void *ctx );
+
 RETCODE backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row );
 
+RETCODE backsql_FreeRow_x( BACKSQL_ROW_NTS *row, void *ctx );
+
 RETCODE backsql_FreeRow( BACKSQL_ROW_NTS *row );
 
 void backsql_PrintErrors( SQLHENV henv, SQLHDBC hdbc, SQLHSTMT sth, int rc );
 
                return BACKSQL_AVL_CONTINUE;
        }
 
-       backsql_BindRowAsStrings( sth, &row );
+       backsql_BindRowAsStrings_x( sth, &row, bsi->bsi_op->o_tmpmemctx );
        rc = SQLFetch( sth );
        for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
                struct berval           dn, pdn, ndn;
                        break;
                }
        }
-       backsql_FreeRow( &row );
+       backsql_FreeRow_x( &row, bsi->bsi_op->o_tmpmemctx );
        SQLFreeStmt( sth, SQL_DROP );
 
        Debug( LDAP_DEBUG_TRACE, "<==backsql_oc_get_candidates(): %d\n",
                 * filter_has_subordinates()
                 */
                if ( bsi.bsi_flags & BSQL_SF_FILTER_HASSUBORDINATE ) {
-                       rc = backsql_has_children( bi, dbh, &e->e_nname );
+                       rc = backsql_has_children( op, dbh, &e->e_nname );
 
                        switch ( rc ) {
                        case LDAP_COMPARE_TRUE:
 
 }
 
 RETCODE
-backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row )
+backsql_BindRowAsStrings_x( SQLHSTMT sth, BACKSQL_ROW_NTS *row, void *ctx )
 {
        RETCODE         rc;
        SQLCHAR         colname[ 64 ];
                        "ncols=%d\n", (int)row->ncols, 0, 0 );
 #endif /* BACKSQL_TRACE */
 
-               row->col_names = (BerVarray)ch_calloc( row->ncols + 1, 
-                               sizeof( struct berval ) );
-               row->cols = (char **)ch_calloc( row->ncols + 1, 
-                               sizeof( char * ) );
-               row->col_prec = (UDWORD *)ch_calloc( row->ncols,
-                               sizeof( UDWORD ) );
-               row->value_len = (SQLINTEGER *)ch_calloc( row->ncols,
-                               sizeof( SQLINTEGER ) );
+               row->col_names = (BerVarray)ber_memcalloc_x( row->ncols + 1, 
+                               sizeof( struct berval ), ctx );
+               row->cols = (char **)ber_memcalloc_x( row->ncols + 1, 
+                               sizeof( char * ), ctx );
+               row->col_prec = (UDWORD *)ber_memcalloc_x( row->ncols,
+                               sizeof( UDWORD ), ctx );
+               row->value_len = (SQLINTEGER *)ber_memcalloc_x( row->ncols,
+                               sizeof( SQLINTEGER ), ctx );
                for ( i = 1; i <= row->ncols; i++ ) {
                        rc = SQLDescribeCol( sth, (SQLSMALLINT)i, &colname[ 0 ],
                                        (SQLUINTEGER)( sizeof( colname ) - 1 ),
                                        &name_len, &col_type,
                                        &col_prec, &col_scale, &col_null );
-                       ber_str2bv( (char *)colname, 0, 1, &row->col_names[ i - 1 ] );
+                       /* FIXME: test rc? */
+
+                       ber_str2bv_x( (char *)colname, 0, 1,
+                                       &row->col_names[ i - 1 ], ctx );
 #ifdef BACKSQL_TRACE
                        Debug( LDAP_DEBUG_TRACE, "backsql_BindRowAsStrings: "
                                "col_name=%s, col_prec[%d]=%d\n",
                                colname, (int)i, (int)col_prec );
 #endif /* BACKSQL_TRACE */
-                       if ( col_type == SQL_LONGVARCHAR 
-                                       || col_type == SQL_LONGVARBINARY) {
-#if 0
-                               row->cols[ i - 1 ] = NULL;
-                               row->col_prec[ i - 1 ] = -1;
-
-                               /*
-                                * such fields must be handled 
-                                * in some other way since they return 2G 
-                                * as their precision (at least it does so 
-                                * with MS SQL Server w/native driver)
-                                * for now, we just set fixed precision 
-                                * for such fields - dirty hack, but...
-                                * no time to deal with SQLGetData()
-                                */
-#endif
+                       if ( col_type != SQL_CHAR && col_type != SQL_VARCHAR )
+                       {
                                col_prec = MAX_ATTR_LEN;
-                               row->cols[ i - 1 ] = (char *)ch_calloc( col_prec + 1, sizeof( char ) );
-                               row->col_prec[ i - 1 ] = col_prec;
-                               rc = SQLBindCol( sth, (SQLUSMALLINT)i,
-                                               SQL_C_CHAR,
-                                               (SQLPOINTER)row->cols[ i - 1 ],
-                                               col_prec + 1,
-                                               &row->value_len[ i - 1 ] );
-                       } else {
-                               row->cols[ i - 1 ] = (char *)ch_calloc( col_prec + 1, sizeof( char ) );
-                               row->col_prec[ i - 1 ] = col_prec;
-                               rc = SQLBindCol( sth, (SQLUSMALLINT)i,
-                                               SQL_C_CHAR,
-                                               (SQLPOINTER)row->cols[ i - 1 ],
-                                               col_prec + 1,
-                                               &row->value_len[ i - 1 ] );
                        }
+
+                       row->cols[ i - 1 ] = (char *)ber_memcalloc_x( col_prec + 1,
+                                       sizeof( char ), ctx );
+                       row->col_prec[ i - 1 ] = col_prec;
+                       rc = SQLBindCol( sth, (SQLUSMALLINT)i,
+                                        SQL_C_CHAR,
+                                        (SQLPOINTER)row->cols[ i - 1 ],
+                                        col_prec + 1,
+                                        &row->value_len[ i - 1 ] );
+                       /* FIXME: test rc? */
                }
 
-               row->col_names[ i - 1 ].bv_val = NULL;
-               row->col_names[ i - 1 ].bv_len = 0;
+               BER_BVZERO( &row->col_names[ i - 1 ] );
                row->cols[ i - 1 ] = NULL;
        }
 
 }
 
 RETCODE
-backsql_FreeRow( BACKSQL_ROW_NTS *row )
+backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row )
+{
+       return backsql_BindRowAsStrings_x( sth, row, NULL );
+}
+
+RETCODE
+backsql_FreeRow_x( BACKSQL_ROW_NTS *row, void *ctx )
 {
        if ( row->cols == NULL ) {
                return SQL_ERROR;
        }
 
-       ber_bvarray_free( row->col_names );
-       ldap_charray_free( row->cols );
-       free( row->col_prec );
-       free( row->value_len );
+       ber_bvarray_free_x( row->col_names, ctx );
+       ber_memvfree_x( (void **)row->cols, ctx );
+       ber_memfree_x( row->col_prec, ctx );
+       ber_memfree_x( row->value_len, ctx );
 
        return SQL_SUCCESS;
 }
 
+
+RETCODE
+backsql_FreeRow( BACKSQL_ROW_NTS *row )
+{
+       return backsql_FreeRow_x( row, NULL );
+}
+
 static int
 backsql_cmp_connid( const void *v_c1, const void *v_c2 )
 {