pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kim Bisgaard <kim+pg@alleroedderne.adsl.dk>
To: pgsql-jdbc@postgresql.org
Subject: Synchronisation problems in COPY IN
Date: Mon, 13 Sep 2010 14:13:24 +0200
Message-ID: <4C8E1564.80405@alleroedderne.adsl.dk> (raw)

Hi,

We are occasionally experiencing problems with our JDBC implementation 
of COPY IN.

We have cut our program down to the attached example.

Guessing from the error messages it looks like jdbc is overwriting the 
buffer sent to the server before it is done. We are writing the data in 
CSV format, with integer(serial),date(now),float format, so it is easy 
to see if the buffer is garbled. Here is an example:
INFO: ERROR: extra data after last expected column
   Where: COPY temporary_wind_speed_50_m_3, line 175: 
"60000179,2010-09-07 00:00:00,60001043,2010-09-07 
00:00:00,10.032084733490484"
org.postgresql.util.PSQLException: ERROR: extra data after last expected 
column
   Where: COPY temporary_wind_speed_50_m_3, line 175: 
"60000179,2010-09-07 00:00:00,60001043,2010-09-07 
00:00:00,10.032084733490484"
         at 
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2062)
         at 
org.postgresql.core.v3.QueryExecutorImpl.processCopyResults(QueryExecutorImpl.java:929)
         at 
org.postgresql.core.v3.QueryExecutorImpl.writeToCopy(QueryExecutorImpl.java:838)
         at 
org.postgresql.core.v3.CopyInImpl.writeToCopy(CopyInImpl.java:53)
         at org.postgresql.copy.CopyManager.copyIn(CopyManager.java:179)
         at org.postgresql.copy.CopyManager.copyIn(CopyManager.java:161)
         at dk.dmi.appl.evejrfeeder.EvejrDBCopy.run(Unknown Source)
         at java.lang.Thread.run(Thread.java:595)


It thus looks like a synchronisation problem (locking problem), but we 
are using Piped(Input/Output)Stream, which must be properly 
synchronised, and CopyManager.CopyIn, where there is nowhere to add 
synchronisation.

To us it looks like a problem in CopyManager.CopyIn, or alternatively we 
are not doing something we where expected to do?

We use PostgreSQL 8.4, and JDBC 8.4-702.

Thanks in advance!

Kim

Attachments:

EvejrDBCopy.java (text/x-java, 3.4K) download import java.io.*; import java.sql.*; import java.util.ArrayList; import org.postgresql.Driver; import org.postgresql.PGConnection; import org.postgresql.copy.*; import org.postgresql.core.*; public class EvejrDBCopy implements Runnable { private PipedOutputStream pipe; private PGConnection connection; private String tableNameAndContents; // e.g. "TABLE(index, value)" public EvejrDBCopy(Connection conn, String tableNameAndContents) throws SQLException { this.connection = (PGConnection)conn; this.tableNameAndContents = tableNameAndContents; pipe = new PipedOutputStream(); } public void run() { try { //InputStream from = new PipedInputStream(pipe); PipedInputStream from = new PipedInputStream(pipe); CopyManager cm = new CopyManager((BaseConnection) connection); String copyString = "copy "+tableNameAndContents+" from STDIN with csv"; System.out.println(copyString); cm.copyIn(copyString, from); } catch (IOException e) { System.err.println(e.getMessage()); System.err.println(e.getLocalizedMessage()); e.printStackTrace(); } catch (SQLException e) { System.err.println(e.getMessage()); System.err.println(e.getLocalizedMessage()); e.printStackTrace(); } } public PipedOutputStream pipe() { return pipe; } public static void main(String[] args) { ArrayList<String> tableNames = new ArrayList<String>(); for (int i=1; i<11; i++) { tableNames.add("nkptest"+i); } long ROWCOUNT = 100000; double VALUE = 3.14; Date validTime = new Date(System.currentTimeMillis()); String validTimeString = validTime.toString(); try { String url = "jdbc:postgresql://griddb3:5432/grid"; String user = "gridadm"; String pass = "XXXXXX"; Driver driver = new Driver(); DriverManager.registerDriver(driver); Connection connection = DriverManager.getConnection(url, user, pass); for (String tableName : tableNames) { tableName = "temporary_"+tableName; String fullContentDefinition = "( gridpoint_id integer NOT NULL, valid_at timestamp without time zone NOT NULL, parameter_value real NOT NULL )"; String contentDefinition = "( gridpoint_id, valid_at, parameter_value )"; Statement tableStm = connection.createStatement(); tableStm.execute("DROP TABLE IF EXISTS "+tableName); tableStm.execute("CREATE TABLE "+tableName+" "+fullContentDefinition); EvejrDBCopy evCopy = new EvejrDBCopy(connection, tableName + contentDefinition); Thread t = new Thread(evCopy); t.start(); PrintWriter dataStream = new PrintWriter(evCopy.pipe(), true); try { for (int i = 0; i<ROWCOUNT; i++) { { String insert = i + ",\"" + validTimeString + "\"," + VALUE; dataStream.println(insert); } } } finally { dataStream.close(); } if (t.isAlive()) {} } connection.close(); } catch (Exception e) { e.printStackTrace(); } } }

view thread (6+ messages)  latest in thread

Message-ID: <4C8E1564.80405@alleroedderne.adsl.dk>
Permalink:  ../4C8E1564.80405@alleroedderne.adsl.dk/
Also on:    postgresql.org/message-id/4C8E1564.80405@alleroedderne.adsl.dk

 · 

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-jdbc@postgresql.org
  Cc: kim+pg@alleroedderne.adsl.dk
  Subject: Re: Synchronisation problems in COPY IN
  In-Reply-To: <4C8E1564.80405@alleroedderne.adsl.dk>
  References: <4C8E1564.80405@alleroedderne.adsl.dk>

* 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