Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 54080634E34 for ; Mon, 24 Aug 2009 21:45:19 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 54324-01-6 for ; Tue, 25 Aug 2009 00:45:00 +0000 (UTC) X-Greylist: delayed 00:08:05.180056 by SQLgrey-1.7.6 Received: from ey-out-2122.google.com (ey-out-2122.google.com [74.125.78.25]) by mail.postgresql.org (Postfix) with ESMTP id 5F6A96323E7 for ; Mon, 24 Aug 2009 21:44:59 -0300 (ADT) Received: by ey-out-2122.google.com with SMTP id 9so565008eyd.61 for ; Mon, 24 Aug 2009 17:44:57 -0700 (PDT) MIME-Version: 1.0 Received: by 10.210.128.12 with SMTP id a12mr2260722ebd.49.1251160612895; Mon, 24 Aug 2009 17:36:52 -0700 (PDT) Date: Mon, 24 Aug 2009 17:36:52 -0700 Message-ID: <895e58dd0908241736q3575cdc2v461d879c5b5f9dcb@mail.gmail.com> Subject: PGStream synchronization From: Maciek Sakrejda To: pgsql-jdbc@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200908/56 X-Sequence-Number: 19905 We've found some synchronization issues around PGStream in QueryExecutorImpl and ProtocolConnectionImpl. Specifically, while QueryExecutorImpl synchronizes all its uses off PGStream, ProtocolConnectionImpl uses the same PGStream directly when close() is called. This can easily cause protocol-level errors and, theoretically, at least, bad data shoved into a COPY IN query while the actual Connection.close() is ignored. This doesn't just affect COPY, though--anything that causes writes to the QueryExecutorImpl's pgStream could be affected. The only thing I've achieved so far is protocol-level errors, but that's a serious enough issue on its own. It's not immediately clear how to fix this, since we don't want to synchronize PGStream itself. Should ProtocolConnectionImpl just delegate to QueryExecutorImpl on close() perhaps, with QueryExecutorImpl handling the actual writes to PGStream? See sample code to reproduce below: package com.truviso; import java.sql.Connection; import java.sql.DriverManager; import org.postgresql.PGConnection; import org.postgresql.copy.CopyIn; public class Test { public static void main(String[] args) throws Exception { Class.forName("org.postgresql.Driver"); final PGConnection c = (PGConnection) DriverManager.getConnection("jdbc:postgresql://localhost:5432/cqdb", "foo", ""); ((Connection)c).createStatement().execute( "drop table if exists foo; create temporary table foo(a text)"); Thread t1 = new Thread(new Runnable() { public void run() { try { CopyIn copy = c.getCopyAPI().copyInQuery("copy foo from stdin"); String copyData = "abc"; byte[] copyBytes = copyData.getBytes(); copy.writeToCopy(copyBytes, 0, copyBytes.length); Thread.sleep(2000); copy.endCopy(); } catch (Exception e) { e.printStackTrace(); } } }); t1.start(); Thread.sleep(1000); ((Connection)c).close(); t1.join(); } } Thanks, -- Maciek Sakrejda | Software Engineer | Truviso 1065 E. Hillsdale Blvd., Suite 230 Foster City, CA 94404 (650) 242-3500 Main (650) 242-3501 F msakrejda@truviso.com www.truviso.com