Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ghb5v-0000Ue-Qq for pgsql-general@arkaria.postgresql.org; Thu, 10 Jan 2019 14:14:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ghb5u-0006mg-Ik for pgsql-general@arkaria.postgresql.org; Thu, 10 Jan 2019 14:14:02 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ghb5t-0006mY-VK for pgsql-general@lists.postgresql.org; Thu, 10 Jan 2019 14:14:02 +0000 Received: from mail-io1-xd2a.google.com ([2607:f8b0:4864:20::d2a]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ghb5m-0008Ns-BV for pgsql-general@postgresql.org; Thu, 10 Jan 2019 14:14:00 +0000 Received: by mail-io1-xd2a.google.com with SMTP id g8so9037236iok.4 for ; Thu, 10 Jan 2019 06:13:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=YyJNOrQP/ebWHQi7FBiLmSFODGz73wg8AHr8KdkeovI=; b=TRj5GGefFhBWvacsARuNlFYwmhvsm7dgrs06k+hf5KhUP8dcZK8GpLXywBsJ8gYMmW W011ojvoyy4gp3K9CqyjqXuY5dlrBCHU9ZKkGNdpiTf40N7msUU6w4itgV6EvPRL0xCi eUqwKFxhhgmOItMO6kATzvpWiptVqhXPwpFwuibww4wiTdvuakpP8EWpvYYBtSDc9a6Q wh/0RuytoU8xPxLOztBFPDexs6Oi2Hy78N8tb0h/wyQhgfop7zoDafLTBdIY2bBBE1c3 /EvB/WeUnKysJirQ2+gaiM4sWHgwB/MrQEmZHkg6N9ySSzEZtSpkwqE74GsbbNh4qsnu e17A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=YyJNOrQP/ebWHQi7FBiLmSFODGz73wg8AHr8KdkeovI=; b=ERjQAeLruxTDhoPNShzFOQdSr2hy1Dfqgq9P6T4mvKpb4TRPllF7pEmmGop4f3Vp2y AeKwMgqeDh5sWkD/GoivfyzCaQCoI9OdlLJJacz7r7ilvD+Kw3HteiJP7oLnoYEwTJ3G eQxPBDPnt0N4wOadpTeITBexBW1p/92voddtr1Wb3LoSK7r4REujVjIkEVG6906mBjEr SZaDZoX1PftU+AaaYu0+eA5L1Q17ktoKGISekun+UKM60jCsX0E4X5EzVXDQhJe5Ib20 aRt6PXhxiwMpE4RCiXRQi8vkybkfQG74jZvFdGB66tvAqGyVqnTZhbTiCYb+8+bqbRDX E8iw== X-Gm-Message-State: AJcUukcY6Pf04Zik0h/ZGJmJJrO4GdytGcSVxpR8a75YeXQ4NKJDvkJo rQczYswkVgqi7P3VAJ95NU9d33GM X-Google-Smtp-Source: ALg8bN7gcooZaU2p6w9n895ooDa3w3BJSQncCOgjsae76ZaAmBdqkjff1YVNEou+PnEj0zGZusTwyA== X-Received: by 2002:a6b:f305:: with SMTP id m5mr6061646ioh.50.1547129633220; Thu, 10 Jan 2019 06:13:53 -0800 (PST) Received: from ?IPv6:2601:681:5500:1e3c:c000:29b7:ceb5:a327? ([2601:681:5500:1e3c:c000:29b7:ceb5:a327]) by smtp.gmail.com with ESMTPSA id m81sm8804699itb.43.2019.01.10.06.13.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 10 Jan 2019 06:13:52 -0800 (PST) Content-Type: multipart/alternative; boundary=Apple-Mail-1C1015CC-CD57-4B8E-A6D3-ECF3757048DB Mime-Version: 1.0 (1.0) Subject: Re: jdbc PGCopyOutputStream close() v. endCopy() From: Rob Sargent X-Mailer: iPhone Mail (16C101) In-Reply-To: Date: Thu, 10 Jan 2019 07:13:51 -0700 Cc: "pgsql-general@postgresql.org" Content-Transfer-Encoding: 7bit Message-Id: <3CA73083-2A92-45FE-8EF3-89E0F5EBC77D@gmail.com> References: To: Dave Cramer List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail-1C1015CC-CD57-4B8E-A6D3-ECF3757048DB Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Ok I=E2=80=99ll repost there. Thanks > On Jan 10, 2019, at 6:09 AM, Dave Cramer wrote: >=20 > Hi Rob, >=20 > Interesting. I've not looked too much into the copy implementation.=20 > The JDBC list or the jdbc github repo https://github.com/pgjdbc/pgjdbc mig= ht be a better place to report this. I know Lukas Edar monitors it as well >=20 > Dave Cramer >=20 > davec@postgresintl.com > www.postgresintl.com >=20 >=20 >> On Tue, 8 Jan 2019 at 16:29, Rob Sargent wrote: >> As is often the case, I'm unsure of which of these methods to use, or if I= 'm using them correctly. >>=20 >> PG10.5, jooq-3.10.8, postgresql-42.1.4, linux (redhat 6.9) and logback to= a file. >>=20 >> I have been using close() for a while but thought I would make use of eit= her the returned long from endCopy() or perhaps getHandledRowCount(). >>=20 >> Both work perfectly, but when I use endCopy() I always get the exception s= hown near the bottom of this log excerpt. The COPY is on its own thread from= the same connection as the direct jooq writes also listed. Again, the data= is all saved but I am worried that I'm not closing properly even if I use c= lose(). The data here doesn't warrent bulk copy but it's just a quick exampl= e to repeat. >>=20 >> 13:32:55.785 [pool-1-thread-1] DEBUG edu.utah.camplab.jx.PayloadFromMux -= STAGING TABLE CREATED: bulk."flk_g16-forcing very long name to trigger trun= cation_22_8045c0" >> 13:32:55.786 [pool-1-thread-1] INFO edu.utah.camplab.jx.PayloadFromMux -= 8045c057-99ec-490b-90c1-85875269afee: started COPY work at 1546979575786 >> 13:32:55.789 [pool-1-thread-1] INFO edu.utah.camplab.jx.PayloadFromMux -= 8045c057-99ec-490b-90c1-85875269afee: Total segment save took 22 ms >> 13:32:55.790 [pool-1-thread-1] INFO edu.utah.camplab.jx.AbstractPayload -= 8045c057-99ec-490b-90c1-85875269afee: closing process 8045c057-99ec-490b-90= c1-85875269afee >> 13:32:55.790 [8045c057-99ec-490b-90c1-85875269afee] INFO e.u.camplab.jx.= PayloadWriterThread - bulk."flk_g16-forcing very long name to trigger trunca= tion_22_8045c0": Begin bulk copy segment >> 13:32:55.793 [8045c057-99ec-490b-90c1-85875269afee] INFO e.u.camplab.jx.= PayloadWriterThread - bulked up to 89, maybe? >> 13:32:55.793 [pool-1-thread-1] DEBUG org.jooq.tools.LoggerListener - Exec= uting batch query : insert into "process_input" ("id", "process_id", "inp= ut_type", "input_ref") values (?, ?, ?, ?) >> 13:32:55.795 [8045c057-99ec-490b-90c1-85875269afee] INFO e.u.camplab.jx.= PayloadWriterThread - bulked up to 197, maybe? >> 13:32:55.797 [8045c057-99ec-490b-90c1-85875269afee] INFO e.u.camplab.jx.= PayloadWriterThread - bulked up to 318, maybe? >> 13:32:55.798 [8045c057-99ec-490b-90c1-85875269afee] INFO e.u.camplab.jx.= PayloadWriterThread - bulked up to 393, maybe? >> 13:32:55.799 [8045c057-99ec-490b-90c1-85875269afee] INFO e.u.camplab.jx.= PayloadWriterThread - 393/393 segments delivered in 9 ms >> 13:32:55.799 [8045c057-99ec-490b-90c1-85875269afee] DEBUG e.u.camplab.jx.= PayloadWriterThread - staged in 9 ms >> 13:32:55.800 [pool-1-thread-1] DEBUG org.jooq.tools.LoggerListener - Exec= uting batch query : insert into "process_output" ("id", "process_id", "ou= tput_type", "output_ref") values (?, ?, ?, ?) >> 13:32:55.805 [8045c057-99ec-490b-90c1-85875269afee] ERROR e.u.camplab.jx.= PayloadWriterThread - bulk."flk_g16-forcing very long name to trigger trunca= tion_22_8045c0": i/o trouble >> java.io.IOException: Ending write to copy failed. >> at org.postgresql.copy.PGCopyOutputStream.close(PGCopyOutputStream.ja= va:107) ~[postgresql-42.1.4.jar:42.1.4] >> at edu.utah.camplab.jx.PayloadWriterThread.run(PayloadWriterThread.ja= va:75) ~[transport/:na] >> Caused by: org.postgresql.util.PSQLException: Tried to write to an inacti= ve copy operation >> at org.postgresql.core.v3.QueryExecutorImpl.writeToCopy(QueryExecutor= Impl.java:978) ~[postgresql-42.1.4.jar:42.1.4] >> at org.postgresql.core.v3.CopyInImpl.writeToCopy(CopyInImpl.java:35) ~= [postgresql-42.1.4.jar:42.1.4] >> at org.postgresql.copy.PGCopyOutputStream.endCopy(PGCopyOutputStream.= java:166) ~[postgresql-42.1.4.jar:42.1.4] >> at org.postgresql.copy.PGCopyOutputStream.close(PGCopyOutputStream.ja= va:105) ~[postgresql-42.1.4.jar:42.1.4] >> ... 1 common frames omitted >> 13:32:55.810 [pool-1-thread-1] DEBUG org.jooq.tools.LoggerListener - Exec= uting batch query : insert into "process_arg" ("id", "process_id", "argna= me", "argvalue_int", "argvalue_float", "argvalue_text") values (?, ?, ?, ?, ?= , ?) >>=20 >> The class doing the bulk work, PayloadWriterThread extends Thread, the th= read name is set from the caller and the critical parts are as follows: >>=20 >> @Override >> public void run() { >> try (PGCopyOutputStream writer =3D new PGCopyOutputStream(copyIn)= ) { >> long startAt =3D System.currentTimeMillis(); >> deliverSegments(writer); >> long postDeliver =3D System.currentTimeMillis(); >> logger.debug("staged in {} ms", postDeliver - startAt); >> } >> catch (SQLException sqle) { >> sqle.printStackTrace(); >> logger.error("{}: sql trouble", tableName, sqle); >> throw new RuntimeException("Bulk copy failed on sql", sqle); >> } >> catch (IOException ioe) { >> ioe.printStackTrace(); >> logger.error("{}: i/o trouble", tableName, ioe); >> throw new RuntimeException("Bulk copy failed on i/o", ioe); >> } >> } >> private void deliverSegments(PGCopyOutputStream writer) throws IOExce= ption, SQLException { >> logger.info("{}: Begin bulk copy segment", tableName); >>=20 >> long counter =3D 0; >> long copied =3D 0; >> //These had better end up the same number >> long st =3D System.currentTimeMillis(); >> for (Map.Entry> seglistME : segmentListMap.= entrySet()) { >> String pbsCSV =3D seglistME.getKey(); >> UUID currentPbsId =3D probandsetIdMap.get(pbsCSV); >> if (currentPbsId =3D=3D null) {=20 >> throw new RuntimeException("Could not find pbsId by " + p= bsCSV); >> } >> counter +=3D seglistME.getValue().size(); >> for (Segment segment : seglistME.getValue()) { >> String segmentCSV =3D makeCSV(segment, currentPbsId); >> writer.write(segmentCSV.getBytes()); >> } >> logger.info("bulked up to {}, maybe?", counter); >> } >> copied =3D writer.endCopy(); >> logger.info("{}/{} segments delivered in {} ms", copied, counter,= System.currentTimeMillis() - st); >> } >>=20 >> Also, the stack trace always references the line of the closing brace of t= he try-with-resources and not on the call to endCopy. Because it's a caught e= xception? Or it's closing a closed writer? >>=20 >>=20 --Apple-Mail-1C1015CC-CD57-4B8E-A6D3-ECF3757048DB Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
Ok I= =E2=80=99ll repost there. Thanks

On Jan 10, 2019, a= t 6:09 AM, Dave Cramer <pg@fastcrypt.= com> wrote:

<= div dir=3D"ltr">
Hi Rob,

Interesting. I'v= e not looked too much into the copy implementation. 
The JDBC= list or the jdbc github repo https://github.com/pgjdbc/pgjdbc might be a better place to report t= his. I know Lukas Edar monitors it as well

On Tue, 8 Jan 2019 at 16:= 29, Rob Sargent <robjsargent@gma= il.com> wrote:
=20 =20 =20

As is often the case, I'm unsure of which of these methods to use, or if I'm using them correctly.

PG10.5, jooq-3.10.8, postgresql-42.1.4, linux (redhat 6.9) and logback to a file.

I have been using close() for a while but thought I would make use of either the returned long from endCopy() or perhaps getHandledRowCount().

Both work perfectly, but when I use endCopy() I always get the exception shown near the bottom of this log excerpt. The COPY is on its own thread from the same connection as the direct jooq writes also listed.  Again, the data is all saved but I am= worried that I'm not closing properly even if I use close(). The data here doesn't warrent bulk copy but it's just a quick example to repeat.

13:32:55.785 [pool-1-thread-1] DEBUG edu.utah.camplab.jx.PayloadFromMux - STAGING TABLE CREATED: bulk."flk_g16-forcing very long name to trigger truncation_22_8045c0"
13:32:55.786 [pool-1-thread-1] INFO  edu.utah.camplab.jx.PayloadFromMux - 8045c057-99ec-490b-90c1-85875269afee: started COPY work at 1546979575786
13:32:55.789 [pool-1-thread-1] INFO  edu.utah.camplab.jx.PayloadFromMux - 8045c057-99ec-490b-90c1-85875269afee: Total segment save took 22 ms
13:32:55.790 [pool-1-thread-1] INFO  edu.utah.camplab.jx.AbstractPayload - 8045c057-99ec-490b-90c1-85875269afee: closing process 8045c057-99ec-490b-90c1-85875269afee
13:32:55.790 [8045c057-99ec-490b-90c1-85875269afee] INFO  e.u.camplab.jx.PayloadWriterThread - bulk."flk_g16-forcing very long name to trigger truncation_22_8045c0": Begin bulk copy segment
13:32:55.793 [8045c057-99ec-490b-90c1-85875269afee] INFO  e.u.camplab.jx.PayloadWriterThread - bulked up to 89, maybe?
13:32:55.793 [pool-1-thread-1] DEBUG org.jooq.tools.LoggerListener - Executing batch query  &= nbsp; : insert into "process_input" ("id", "process_id", "input_type", "input_ref") values (?, ?, ?, ?)
13:32:55.795 [8045c057-99ec-490b-90c1-85875269afee] INFO  e.u.camplab.jx.PayloadWriterThread - bulked up to 197, maybe?
13:32:55.797 [8045c057-99ec-490b-90c1-85875269afee] INFO  e.u.camplab.jx.PayloadWriterThread - bulked up to 318, maybe?
13:32:55.798 [8045c057-99ec-490b-90c1-85875269afee] INFO  e.u.camplab.jx.PayloadWriterThread - bulked up to 393, maybe?
13:32:55.799 [8045c057-99ec-490b-90c1-85875269afee] INFO  e.u.camplab.jx.PayloadWriterThread - 393/393 segments delivered in 9 ms
13:32:55.799 [8045c057-99ec-490b-90c1-85875269afee] DEBUG e.u.camplab.jx.PayloadWriterThread - staged in 9 ms
13:32:55.800 [pool-1-thread-1] DEBUG org.jooq.tools.LoggerListener - Executing batch query  &= nbsp; : insert into "process_output" ("id", "process_id", "output_type", "output_ref") values (?, ?, ?, ?)
13:32:55.805 [8045c057-99ec-490b-90c1-85875269afee] ERROR e.u.camplab.jx.PayloadWriterThread - bulk."flk_g16-forcing very long name to trigger truncation_22_8045c0": i/o trouble
java.io.IOException: Ending write to copy failed.
    at org.postgresql.copy.PGCopyOutputStream.close(PGCopyOutputStream.java:107) ~[postgresql-42.1.4.jar:42.1.4]
    at edu.utah.camplab.jx.PayloadWriterThread.run(PayloadWriterThread.ja= va:75) ~[transport/:na]
Caused by: org.postgresql.util.PSQLException: Tried to write to an inactive copy operation
    at org.postgresql.core.v3.QueryExecutorImpl.writeToCopy(QueryExecutorImpl.java:= 978) ~[postgresql-42.1.4.jar:42.1.4]
    at org.postgresql.core.v3.CopyInImpl.writeToCopy(CopyInImpl.java:35) ~[postgresql-42.1.4.jar:42.1.4]
    at org.postgresql.copy.PGCopyOutputStream.endCopy(PGCopyOutputStream.java:166) ~[postgresql-42.1.4.jar:42.1.4]
    at org.postgresql.copy.PGCopyOutputStream.close(PGCopyOutputStream.java:105) ~[postgresql-42.1.4.jar:42.1.4]
    ... 1 common frames omitted
13:32:55.810 [pool-1-thread-1] DEBUG org.jooq.tools.LoggerListener - Executing batch query  &= nbsp; : insert into "process_arg" ("id", "process_id", "argname", "argvalue_int", "argvalue_float", "argvalue_text") values (?, ?, ?, ?, ?, ?)

The class doing the bulk work, PayloadWriterThread extends Thread, the thread name is set from the caller and the critical parts are as follows:

    @Override
    public void run() {
        try (PGCopyOutputStream w= riter =3D new PGCopyOutputStream(copyIn)) {
            l= ong startAt =3D System.currentTimeMillis();
            d= eliverSegments(writer);
            l= ong postDeliver =3D System.currentTimeMillis();
            l= ogger.debug("staged in {} ms", postDeliver - startAt);
        }
        catch (SQLException sqle)= {
            s= qle.printStackTrace();
            l= ogger.error("{}: sql trouble", tableName, sqle);
            t= hrow new RuntimeException("Bulk copy failed on sql", sqle);
        }
        catch (IOException ioe) {=
            i= oe.printStackTrace();
            l= ogger.error("{}: i/o trouble", tableName, ioe);
            t= hrow new RuntimeException("Bulk copy failed on i/o", ioe);
        }
    }
    private void deliverSegments(PGCopyOutputStream w= riter) throws IOException, SQLException {
        logger.info("{}: Begin bulk copy segment", table= Name);

        long counter =3D 0;
        long copied =3D 0;
        //These had better end u= p the same number
        long st =3D System.curren= tTimeMillis();
        for (Map.Entry<String,= List<Segment>> seglistME : segmentListMap.entrySet()) {
            S= tring pbsCSV =3D seglistME.getKey();
            U= UID currentPbsId =3D probandsetIdMap.get(pbsCSV);
            i= f (currentPbsId =3D=3D null) {
            &= nbsp;   throw new RuntimeException("Could not find pbsId by " + pbsCSV);
            }=
            c= ounter +=3D seglistME.getValue().size();
            f= or (Segment segment : seglistME.getValue()) {
           &n= bsp;    String segmentCSV =3D makeCSV(segment, currentPbsId);
           &n= bsp;    writer.write(segmentCSV.getBytes());
            }=
            <= a href=3D"http://logger.info" target=3D"_blank">logger.info("bulked up t= o {}, maybe?", counter);
        }
        copied =3D writer.endCopy= ();
        logger.info("{}/{} segments delivered in {} ms",= copied, counter, System.currentTimeMillis() - st);
    }

Also, the stack trace always references the line of the closing brace of the try-with-resources and not on the call to endCopy. Because it's a caught exception? Or it's closing a closed writer?


= --Apple-Mail-1C1015CC-CD57-4B8E-A6D3-ECF3757048DB--