Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dpEn4-0003GS-72 for pgsql-performance@arkaria.postgresql.org; Tue, 05 Sep 2017 14:25:22 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dpEn3-00053U-Mr for pgsql-performance@arkaria.postgresql.org; Tue, 05 Sep 2017 14:25:21 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dpElH-0001tF-Hy for pgsql-performance@postgresql.org; Tue, 05 Sep 2017 14:23:31 +0000 Received: from resqmta-po-09v.sys.comcast.net ([2001:558:fe16:19:96:114:154:168]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dpEl6-0008Vo-2w for pgsql-performance@postgresql.org; Tue, 05 Sep 2017 14:23:30 +0000 Received: from resomta-po-12v.sys.comcast.net ([96.114.154.236]) by resqmta-po-09v.sys.comcast.net with ESMTP id pEkddcjMfdf97pEl4dBVgn; Tue, 05 Sep 2017 14:23:18 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=comcast.net; s=q20161114; t=1504621398; bh=krWGEhyYiTGuDBWaMMpemmD3Fin3zjZ5klNTawYpsoo=; h=Received:Received:Subject:To:From:Message-ID:Date:MIME-Version: Content-Type; b=MPR07fWV9Hx1brRBdyBkwyNZQ74nrsEY4YijShlKbxzq3qunsfw/Smjr6mVjT3h4L +oF81gptIRW/rU0Ar6rQwTbnXszOLlTA4Gg7voUEpesZLBMGbUZJdDxuWZaoUttsr5 7UjntusTVz3T/aVN1DgVsPqTjIthYqEcJunbrGIHg6PTINrr55pdCBb0Hlv9r7rYAD qU18VSrVr3uC5m0rcwbT9fdJymMKeH+BntZayv8yx5SalgNx8Q0Hs7SH+XkxBfuLQo hiwhGt3vVC/8zDO4eciBaiAzyGsFrkKUsV+qJ+EXr9vPUZpO1HMNzje3etFeiWGtmG al4qTipxV9rvQ== Received: from [192.168.0.67] ([73.68.146.26]) by resomta-po-12v.sys.comcast.net with SMTP id pEl2dmzALajpwpEl3d7keZ; Tue, 05 Sep 2017 14:23:18 +0000 Subject: Re: printing results of query to file in different times To: Mariel Cherkassky References: <634f2eb6-daf6-6314-e6f5-1b531b50a9ec@comcast.net> From: George Neuner Cc: pgsql-performance@postgresql.org Message-ID: <62fce205-db63-ed25-c726-d8c442f6223f@comcast.net> Date: Tue, 5 Sep 2017 10:23:14 -0400 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------DD03C1300D3AAAF0D28A261A" Content-Language: en-US X-CMAE-Envelope: MS4wfKOl/JSVB9oC0nswvndLoszwqEkn4mCcIGV83DdeJETuVE6q5Mm0df05r9Z0vO4Ca15cwXlGlxDb3UIwZxyixI1SZ0VXBdEHjnJLMfqoz8B+/xRnJJIV lh/U5J+FCok0C1CrJkgWRddorpuoFJf1zD17WrqCK5wR8FxdVuTthFNZL9aOR2btobcg5a69iZi+XVnt7+W8e9t8H2SRG8cnQKJZmROFjqQ6latKteqDpg2c List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org This is a multi-part message in MIME format. --------------DD03C1300D3AAAF0D28A261A Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit Hi Mariel, Please don't top post in the Postgresql groups. On 9/5/2017 7:28 AM, Mariel Cherkassky wrote: > 2017-08-31 16:24 GMT+03:00 George Neuner >: > >> One thing you might look at is the isolation level of the query. >> If you are using READ_COMMITTED or less, and the table is busy, >> other writing queries may be stepping on yours and even >> potentially changing your result set during the cursor loop. I >> would try using REPEATABLE_READ and see what happens. > > I didn't understand what do you mean by REPEATABLE_READ . I was referring to transaction isolation levels.  When multiple transactions are running concurrently, the DBMS can (or not) prevent them from seeing changes made by one another.  Consider 2 transactions A and B running concurrently:   T1:   A reads table X   T2:   B writes to table X   T3:   B commits   T4:   A reads table X again. Depending on the isolation levels [and the specific query, obviously], A may or may not be able to see what changes B made to X. The default isolation level in Postgresql is READ COMMITTED, which does allow transactions to see committed writes made by concurrently running transactions.  REPEATABLE READ is a higher level of isolation which effectively takes a snapshot of the table(s) when they are 1st read, and guarantees that any further reads (e.g., by cursors) of the tables made by the transaction continue to see the same results. My thought was that your loop may be running slowly because the table is being changed underneath your cursor.  It may be better to pull the results into a temporary table and run your cursor loop over that. For more information, see: https://www.postgresql.org/docs/current/static/transaction-iso.html https://www.postgresql.org/docs/9.6/static/sql-begin.html https://stackoverflow.com/questions/6274457/set-isolation-level-for-postgresql-stored-procedures George --------------DD03C1300D3AAAF0D28A261A Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: 8bit Hi Mariel,

Please don't top post in the Postgresql groups.

On 9/5/2017 7:28 AM, Mariel Cherkassky wrote:
> 2017-08-31 16:24 GMT+03:00 George Neuner <gneuner2@comcast.net > <mailto:gneuner2@comcast.net>>: > >> One thing you might look at is the isolation level of the query. >> If you are using READ_COMMITTED or less, and the table is busy, >> other writing queries may be stepping on yours and even >> potentially changing your result set during the cursor loop. I >> would try using REPEATABLE_READ and see what happens. > > I didn't understand what do you mean by REPEATABLE_READ .
I was referring to transaction isolation levels.  When multiple transactions are running concurrently, the DBMS can (or not) prevent them from seeing changes made by one another.  Consider 2 transactions A and B running concurrently:

  T1:   A reads table X
  T2:   B writes to table X
  T3:   B commits
  T4:   A reads table X again.

Depending on the isolation levels [and the specific query, obviously], A may or may not be able to see what changes B made to X.

The default isolation level in Postgresql is READ COMMITTED, which does allow transactions to see committed writes made by concurrently running transactions.  REPEATABLE READ is a higher level of isolation which effectively takes a snapshot of the table(s) when they are 1st read, and guarantees that any further reads (e.g., by cursors) of the tables made by the transaction continue to see the same results.


My thought was that your loop may be running slowly because the table is being changed underneath your cursor.  It may be better to pull the results into a temporary table and run your cursor loop over that.

For more information, see:
https://www.postgresql.org/docs/current/static/transaction-iso.html
https://www.postgresql.org/docs/9.6/static/sql-begin.html
https://stackoverflow.com/questions/6274457/set-isolation-level-for-postgresql-stored-procedures

George
--------------DD03C1300D3AAAF0D28A261A--