Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kgtCg-0003sR-UD for pgsql-performance@arkaria.postgresql.org; Sun, 22 Nov 2020 17:31:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kgtCe-0004nF-UB for pgsql-performance@arkaria.postgresql.org; Sun, 22 Nov 2020 17:31:08 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kgtCe-0004n7-JU for pgsql-performance@lists.postgresql.org; Sun, 22 Nov 2020 17:31:08 +0000 Received: from mail-il1-x143.google.com ([2607:f8b0:4864:20::143]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kgtCX-0006Vx-GB for pgsql-performance@lists.postgresql.org; Sun, 22 Nov 2020 17:31:07 +0000 Received: by mail-il1-x143.google.com with SMTP id w8so13658843ilg.12 for ; Sun, 22 Nov 2020 09:31:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=CcJHVffqtxJibDX29A84hBapIW/VAye7iclissMIpzo=; b=lrB7vcLijENJvmPkDRgvwoShx8zcAPbP4vppWWHlr9AVHZOG+0UMAa28YpcDm+BT5j D8gRWnWqmAMCZKRkJ0m/AAg2OaiIOUDEpdCBVF03xHQfs14t/1AMHm1RdzentqQfb5WU fzKnUiEQFSYLO3ZFQCBnGquno9cJRKG07uO5q9hglfU51fcQ1td2tvWbC6K0kff5dR0X 8O5gArDCUxlgfcum7+miPGuxSBUAWiNnN0zF3tosnOXp0t+7fybcJo+OJQjV5Vc7Ry+W 6O8Fzuv61+S7Ukrx2guHaWnOtuRmf0glIpDv9l7o2CTPDiKJWOmcWn3rfaXUl0gLUGbO DRrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=CcJHVffqtxJibDX29A84hBapIW/VAye7iclissMIpzo=; b=hMqElffdvEMHJI/NcPlZd4x5K0KnZhjAD8vfZl9UIdsToZaDihx7aoSVmcckXWgVug tNOguO7qp4FStMAASv7h2vO3MK1ODB9w5QnYm4zkyWrG9nhp+xiOe3T/GepbO9A4RYDV YXYYD4f6CXEp/fYTMI+PtT/R9YVeV/0O3ZJDSDBuevSPP9OVy0j3wEVBcmRivwqTcv99 CpewkjtjVI5b90Xptlx6JOvM7IpyfqzV96Zr6j7dgvxok5qOSEZOY6c30Rj6YvMUQr9H PbKHqs523kWnEO0oP7QQ4M+YGDvf8HENFInzlQUkqZugAJgih2fMyhaikKFznWQnDMai B6eg== X-Gm-Message-State: AOAM531djxeF4NAIPJYsx72pM9f9/i3g2QfiyxrI5VeiKPDH+vFvGVDq iPLQRgEWZuUKuevwcJOHnag3bA== X-Google-Smtp-Source: ABdhPJzcGvfN0Dw1CY0m1bp2Pe5jG3n9nr9NPGNkaVrv0k5pWoSGrkIpjbmsl11UOQnWvau3NthTrQ== X-Received: by 2002:a92:520b:: with SMTP id g11mr29381466ilb.14.1606066259319; Sun, 22 Nov 2020 09:30:59 -0800 (PST) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id o3sm6398769ilk.27.2020.11.22.09.30.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 22 Nov 2020 09:30:58 -0800 (PST) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 7C07D80070E; Sun, 22 Nov 2020 11:30:57 -0600 (CST) Date: Sun, 22 Nov 2020 11:30:57 -0600 From: Justin Pryzby To: Nandakumar M Cc: Pgsql Performance Subject: Re: Simple update query is slow Message-ID: <20201122173057.GA24052@telsasoft.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Sun, Nov 22, 2020 at 02:18:10AM +0530, Nandakumar M wrote: > Just realised that the time difference between explain analyze plan > and /timing result is due to the implicit commit. Can you run with SET client_min_messages=debug; and SET log_lock_waits=on; Oh, but your server is too old for that... On Sun, 22 Nov 2020 at 01:57, Nandakumar M wrote: > > Hi, > > I noticed something strange in our PG server. I have a table named > 'timetable' that has only one bigint column and one row. > > Once in every 5 seconds this row is updated to the current time epoch > value in milliseconds. > > The update query seems to be taking considerable time (avg 50 > milliseconds). When I tried generating the explain (analyze,buffers) > for the query, the planning time + execution time is always less than > 0.1 millisecond. However the query time as shown when /timing of psql > is enabled shows approx 30 milliseconds (I am connecting via psql from > the localhost). > > > Please find the details below. > > postgres=> select version(); > version > ---------------------------------------------------------------------------------------------------------------- > PostgreSQL 9.4.15 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) > 4.4.7 20120313 (Red Hat 4.4.7-18), 64-bit > (1 row) > > Time: 0.572 ms > > > > > > postgres=> \d+ timetable > Table "public.timetable" > Column | Type | Modifiers | Storage | Stats target | Description > --------+--------+-----------+---------+--------------+------------- > time | bigint | | plain | | > > > > > > > postgres=> table timetable ; > time > ------------ > 1605988584 > (1 row) > > Time: 0.402 ms > > > > > > postgres=> explain (analyze,buffers,verbose) update timetable set time = time+0; > QUERY PLAN > ----------------------------------------------------------------------------------------------------------------- > Update on public.timetable (cost=0.00..4.01 rows=1 width=14) (actual > time=0.064..0.064 rows=0 loops=1) > Buffers: shared hit=5 > -> Seq Scan on public.timetable (cost=0.00..4.01 rows=1 width=14) > (actual time=0.029..0.029 rows=1 loops=1) > Output: ("time" + 0), ctid > Buffers: shared hit=4 > Planning time: 0.054 ms > Execution time: 0.093 ms > (7 rows) > > Time: 27.685 ms > > > Sometimes this shoots up to even a few hundred milliseconds. > > postgres=> explain (analyze,buffers,verbose) update timetable set time = time+0; > QUERY PLAN > ----------------------------------------------------------------------------------------------------------------- > Update on public.timetable (cost=0.00..4.01 rows=1 width=14) (actual > time=0.048..0.048 rows=0 loops=1) > Buffers: shared hit=5 > -> Seq Scan on public.timetable (cost=0.00..4.01 rows=1 width=14) > (actual time=0.027..0.028 rows=1 loops=1) > Output: ("time" + 0), ctid > Buffers: shared hit=4 > Planning time: 0.063 ms > Execution time: 0.084 ms > (7 rows) > > Time: 291.090 ms > > I guess the problem here may somehow be linked to frequent updates to > the one row. However I want to understand what exactly is going wrong > here. Also I don't understand the discrepancy between planning + > execution time from explain analyze and the time taken by the query as > reported in pg log and in psql console.