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 1ggyw3-00053Q-Qx for pgsql-general@arkaria.postgresql.org; Tue, 08 Jan 2019 21:29:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ggyvz-0002U6-VI for pgsql-general@arkaria.postgresql.org; Tue, 08 Jan 2019 21:29:15 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ggyvz-0002Ty-JS for pgsql-general@lists.postgresql.org; Tue, 08 Jan 2019 21:29:15 +0000 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ggyvw-0002Ig-H6 for pgsql-general@postgresql.org; Tue, 08 Jan 2019 21:29:14 +0000 Received: by mail-pf1-x434.google.com with SMTP id b85so2548229pfc.3 for ; Tue, 08 Jan 2019 13:29:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-language; bh=DX5C+c/a+8uLOmh0FuFUltXe9MYauKB8+pzKEsNCaC8=; b=ZB9wBSv5DAmIdY8yvU3INjVGQlwbhy38MfX8XAqnNDqKTH9p4S+OU6UzvCBWnAZZla bzrvqm7a1VfQPIUnKJxKeJyxcRJi7IS4sxNBKmj1DoL4uv8VgoxwnBFlfDM+zzCC/9F0 1H1idprf+zfdjxOSaL31YqAUafEiPviiALpYGISeeDM6/3hTFVFUGf2eify/HqGBbYXY 6oLC9y+1Kxao6Ytu4AlLVlNtOVI47CMeoHxcBm1U7Ihy1Qi0idhEjpwUDG+5JBxZbRRp v5RQzoJyCfjSF2xRyk/JEBm/hYHv6vx75FyO3iwaAokgrYMhbt/rol6Vi4fE7GojONEf F0Ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language; bh=DX5C+c/a+8uLOmh0FuFUltXe9MYauKB8+pzKEsNCaC8=; b=bWo4mWpgZQRE+BHZtggMazZ5GSH0jzOlqIcpD2GDSzgx4T8U5yWbDs3sZToBjsxqlH pKgiGJ16rIVcdcXbYpJY6k//21T23wwSBrKfFvCGsv8FW6zaf86OdrTIOtwB+m6JHun5 wLJJZlBaq+uTGFeVtKjQPQRF+rMvZCIOlhYTvBXVDbag1Uv1eow987AM836creLDUBIw 1pnU3CSxqahQTTC1M9hh3tePRQ5QGRT337nQ7MJHi6+rK1qdOmwXjOLQ3YHRlkLKw4bN YO6uOLNWKvOeFyP8zllvKgGFH5aH7vsXfqMEBfMlq/6/leUjeKWcG/K1RSCmv4607NNf EsYg== X-Gm-Message-State: AJcUukeAKtgz4DXDKpd2WDqyxv/6n4NLN7PZN97fzlmUIpcfhxjDZ4E5 miA7WlskqDBnG7Yi40Q9WZCAP6kr X-Google-Smtp-Source: ALg8bN5pDd/bxesrOQApoIPYXFEiu+agv7a9+Ku/ow1Q5VCvVBhJ5cTWGhtFRr0zqs71TUnzEm43Hw== X-Received: by 2002:a62:4c:: with SMTP id 73mr3334091pfa.24.1546982949530; Tue, 08 Jan 2019 13:29:09 -0800 (PST) Received: from [10.104.134.14] ([155.100.47.6]) by smtp.gmail.com with ESMTPSA id c9sm140202341pfc.92.2019.01.08.13.29.08 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Jan 2019 13:29:08 -0800 (PST) To: "pgsql-general@postgresql.org" From: Rob Sargent Subject: jdbc PGCopyOutputStream close() v. endCopy() Message-ID: Date: Tue, 8 Jan 2019 14:29:07 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="------------F1801B5DE3124B2A64F27DF3" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------F1801B5DE3124B2A64F27DF3 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit 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    : 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    : 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.java: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    : 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 writer = new PGCopyOutputStream(copyIn)) {             long startAt = System.currentTimeMillis();             deliverSegments(writer);             long postDeliver = 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 IOException, SQLException {         logger.info("{}: Begin bulk copy segment", tableName);         long counter = 0;         long copied = 0;         //These had better end up the same number         long st = System.currentTimeMillis();         for (Map.Entry> seglistME : segmentListMap.entrySet()) {             String pbsCSV = seglistME.getKey();             UUID currentPbsId = probandsetIdMap.get(pbsCSV);             if (currentPbsId == null) {                 throw new RuntimeException("Could not find pbsId by " + pbsCSV);             }             counter += seglistME.getValue().size();             for (Segment segment : seglistME.getValue()) {                 String segmentCSV = makeCSV(segment, currentPbsId);                 writer.write(segmentCSV.getBytes());             }             logger.info("bulked up to {}, maybe?", counter);         }         copied = 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? --------------F1801B5DE3124B2A64F27DF3 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

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    : 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    : 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.java: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    : 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 writer = new PGCopyOutputStream(copyIn)) {
            long startAt = System.currentTimeMillis();
            deliverSegments(writer);
            long postDeliver = 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 IOException, SQLException {
        logger.info("{}: Begin bulk copy segment", tableName);

        long counter = 0;
        long copied = 0;
        //These had better end up the same number
        long st = System.currentTimeMillis();
        for (Map.Entry<String, List<Segment>> seglistME : segmentListMap.entrySet()) {
            String pbsCSV = seglistME.getKey();
            UUID currentPbsId = probandsetIdMap.get(pbsCSV);
            if (currentPbsId == null) {
                throw new RuntimeException("Could not find pbsId by " + pbsCSV);
            }
            counter += seglistME.getValue().size();
            for (Segment segment : seglistME.getValue()) {
                String segmentCSV = makeCSV(segment, currentPbsId);
                writer.write(segmentCSV.getBytes());
            }
            logger.info("bulked up to {}, maybe?", counter);
        }
        copied = 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?


--------------F1801B5DE3124B2A64F27DF3--