X-Original-To: pgsql-jdbc-postgresql.org@postgresql.org Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id 7957F9FB2A6 for ; Tue, 29 Aug 2006 20:14:20 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 97496-04-5 for ; Tue, 29 Aug 2006 23:14:11 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 59BCA9FB43F for ; Tue, 29 Aug 2006 19:02:08 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from exch01smtp10.hdi.tvcabo (smtp1.netcabo.pt [212.113.174.28]) by svr4.postgresql.org (Postfix) with ESMTP id 08B895AF044 for ; Tue, 29 Aug 2006 22:02:04 +0000 (GMT) Received: from neti02mx.hdi.tvcabo ([10.137.34.34]) by exch01smtp10.hdi.tvcabo with Microsoft SMTPSVC(6.0.3790.1830); Tue, 29 Aug 2006 23:01:59 +0100 Received: from a83-132-224-44.cpe.netcabo.pt (HELO [192.168.1.2]) ([83.132.224.44]) by neti02mx.hdi.tvcabo with ESMTP; 29 Aug 2006 23:01:58 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AQAAAGdU9EQNgmiIOgE In-Reply-To: References: <6E13F8E6-8D37-4D2D-833D-AFFD87BF22B8@fastcrypt.com> Mime-Version: 1.0 (Apple Message framework v752.2) X-Priority: 3 Content-Type: multipart/alternative; boundary=Apple-Mail-1-340611760 Message-Id: <0D2330AE-008F-47DD-858F-491CB18652F1@evolute.pt> From: Luis Vilar Flores Subject: Re: outOfMemoryError Date: Tue, 29 Aug 2006 23:02:34 +0100 To: pgsql-jdbc@postgresql.org X-Mailer: Apple Mail (2.752.2) X-OriginalArrivalTime: 29 Aug 2006 22:01:59.0230 (UTC) FILETIME=[BFFF3DE0:01C6CBB6] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.136 tagged_above=0 required=5 tests=FORGED_RCVD_HELO, HTML_MESSAGE X-Spam-Level: X-Archive-Number: 200608/161 X-Sequence-Number: 15688 --Apple-Mail-1-340611760 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=ISO-8859-1; delsp=yes; format=flowed I wrote a program for moving full databases between different db =20 engines, the main loop for inserting is: The main difference is that I use batch insert on prepared statements. public class Move { public static final int MAX_BATCH_ROWS =3D 4096; =09 public static move( Connection CON_SRC, Connection CON_DEST, = String =20 table ) throws Exception { Statement stm =3D CON_SRC.createStatement( = ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY ); ResultSet rs =3D statement.executeQuery( "SELECT * FROM = " + table ); StringBuffer buff =3D new StringBuffer( "INSERT INTO " = ); StringBuffer args =3D new StringBuffer(); buff.append( table ); buff.append( " ( " ); ResultSetMetaData rsmd =3D rs.getMetaData(); for( int j =3D 1; j <=3D rsmd.getColumnCount(); ++j ) { if( j !=3D 1 ) { buff.append( ", " ); args.append( ", " ); } buff.append( rsmd.getColumnName( j ) ); args.append( "?" ); } buff.append( " ) VALUES ( " ); buff.append( args ); buff.append( " )" ); String insert =3D buff.toString(); PreparedStatement pstm =3D CON_DEST.prepareStatement( = insert ); System.out.println( "I: " + i + " " + TABLES[ i ] ); int typesCache[] =3D new int[ rsmd.getColumnCount() + 1 = ]; for( int j =3D 1; j <=3D rsmd.getColumnCount(); ++j ) { typesCache[ j ] =3D rsmd.getColumnType( j ); } =09 int rows =3D 0; while( rs.next() ) { for( int j =3D 1; j < typesCache.length; ++j ) { Object data =3D rs.getObject( j ); pstm.setObject( j, rs.getObject( j ), = typesCache[ j ] ); } ++rows; if( ( rows % MAX_BATCH_ROWS ) =3D=3D 0 ) { pstm.executeBatch(); } } rs.close(); pstm.close(); } =09 } Please have attention, this code was copy/paste from 2 classes (my =20 program is multi-threaded for performance - async read/write and =20 multiple tables at once) This method works on at least 5 million rows. Luis Flores Analista de Sistemas Evolute - Consultoria Inform=E1tica Email: lflores@evolute.pt Tel: (+351) 212949689 AVISO DE CONFIDENCIALIDADE Esta mensagem de correio electr=F3nico e eventuais ficheiros anexos s=E3o = =20 confidenciais e destinados apenas =E0(s) pessoa(s) ou entidade(s) acima =20= referida(s), podendo conter informa=E7=E3o privilegiada e confidencial, = a =20 qual n=E3o poder=E1 ser divulgada, copiada, gravada ou distribu=EDda nos = =20 termos da lei vigente. Caso n=E3o seja o destinat=E1rio da mensagem, ou =20= se ela lhe foi enviada por engano, agradecemos que n=E3o fa=E7a uso ou =20= divulga=E7=E3o da mesma. A distribui=E7=E3o ou utiliza=E7=E3o da = informa=E7=E3o nela =20 contida =E9 interdita. Se recebeu esta mensagem por engano, por favor =20= notifique o remetente e apague este e-mail do seu sistema. Obrigado. CONFIDENTIALITY NOTICE This e-mail transmission and eventual attached files are intended =20 only for the use of the individual(s) or entity(ies) named above and =20 may contain information that is both privileged and confidential and =20 is exempt from disclosure under applicable law. If you are not the =20 intended recipient, you are hereby notified that any disclosure, =20 copying, distribution or use of any of the information contained in =20 this transmission is strictly restricted. If by any means you have =20 received this transmission in error, please immediately notify the =20 sender and delete this e-mail from your system. Thank you. --Apple-Mail-1-340611760 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=ISO-8859-1
I wrote a = program for moving full databases between different db engines, the main = loop for inserting is:=A0

The main difference is that I use batch insert on = prepared statements.

public class Move
public static final int = MAX_BATCH_ROWS =3D 4096;


public = static move( Connection CON_SRC, Connection CON_DEST, String table = )
throws Exception
{
Statement = stm =3D CON_SRC.createStatement( ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY = );
ResultSet rs =3D = statement.executeQuery( "SELECT * FROM " + table );
StringBuffer buff =3D new = StringBuffer( "INSERT INTO " );
= StringBuffer args =3D new StringBuffer();
buff.append( table );
buff.append( " ( " );
ResultSetMetaData rsmd =3D = rs.getMetaData();
for( int j =3D 1; j <=3D = rsmd.getColumnCount(); ++j )
= {
if( j !=3D 1 )
{
= buff.append( ", " );
= args.append( ", " );
= }
buff.append( rsmd.getColumnName( = j ) );
args.append( "?" );
}
= buff.append( " ) VALUES ( " );
= buff.append( args );
= buff.append( " )" );
String = insert =3D buff.toString();
= PreparedStatement pstm =3D CON_DEST.prepareStatement( insert = );
System.out.println( "I: " + i + " = " + TABLES[ i ] );
int = typesCache[] =3D new int[ rsmd.getColumnCount() + 1 ];
for( int j =3D 1; j <=3D = rsmd.getColumnCount(); ++j )
= {
typesCache[ j ] =3D = rsmd.getColumnType( j );
}


int rows = =3D 0;
while( rs.next() )
{
for( int = j =3D 1; j < typesCache.length; ++j )
= {
Object data =3D rs.getObject( j = );
pstm.setObject( j, rs.getObject( = j ), typesCache[ j ] );
= }
++rows;
if( ( rows % MAX_BATCH_ROWS ) =3D=3D= 0 )
{
= pstm.executeBatch();
= }
}
= rs.close();
= pstm.close();
}


}

Please have = attention, this code was copy/paste from 2 classes (my program is = multi-threaded for performance - async read/write and multiple tables at = once)
This method works on at least 5 = million rows.


Luis Flores

Analista de = Sistemas

Evolute - Consultoria Inform=E1tica

Email: = lflores@evolute.pt

Tel: (+351) = 212949689


AVISO DE = CONFIDENCIALIDADE
Esta mensagem de correio electr=F3nico e = eventuais ficheiros anexos s=E3o confidenciais e destinados apenas =E0(s) = pessoa(s) ou entidade(s) acima referida(s), podendo conter informa=E7=E3o = privilegiada e confidencial, a qual n=E3o poder=E1 ser divulgada, = copiada, gravada ou distribu=EDda nos termos da lei vigente. Caso n=E3o = seja o destinat=E1rio da mensagem, ou se ela lhe foi enviada por engano, = agradecemos que n=E3o fa=E7a uso ou divulga=E7=E3o da mesma. A = distribui=E7=E3o ou utiliza=E7=E3o da informa=E7=E3o nela contida =E9 = interdita. Se recebeu esta mensagem por engano, por favor notifique o = remetente e apague este e-mail do seu sistema. = Obrigado.
This = e-mail transmission and eventual attached files are intended only for = the use of the individual(s) or entity(ies) named above and may contain = information that is both privileged and confidential and is exempt from = disclosure under applicable law. If you are not the intended recipient, = you are hereby notified that any disclosure, copying, distribution or = use of any of the information contained in this transmission is strictly = restricted. If by any means you have received this transmission in = error, please immediately notify the sender and delete this e-mail from = your system. Thank you.

=

= --Apple-Mail-1-340611760--