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 1nyA4m-0004ig-GG for pgsql-performance@arkaria.postgresql.org; Mon, 06 Jun 2022 10:35:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nyA4k-0002sl-El for pgsql-performance@arkaria.postgresql.org; Mon, 06 Jun 2022 10:35:10 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nyA4k-0002sb-0K for pgsql-performance@lists.postgresql.org; Mon, 06 Jun 2022 10:35:10 +0000 Received: from mail-io1-xd2f.google.com ([2607:f8b0:4864:20::d2f]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nyA4g-0005px-Hd for pgsql-performance@postgresql.org; Mon, 06 Jun 2022 10:35:08 +0000 Received: by mail-io1-xd2f.google.com with SMTP id i201so6139691ioa.6 for ; Mon, 06 Jun 2022 03:35:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=PfV5/sPleXP1zNErEqBx1BszOKX9nyc6kUDhdopt2nk=; b=1IW58mW7VSqGVs/tO0F0Q0Es8+vNivlNpzw6NbA44VVq/X3qjOla0ZS7EIiNYZea65 N7/rqIy65TiiXTNsQzSe6pMT8zH9oMDfn4OZXZnXgiqs4Tr6NRHTyB84cFGicdxzFSFT LjCx2bedUgn3MxrDc9Zt2THqvlhTHmfhZ48YicMFUNtQFcoIsA1ipVTJyhn7C/DTKO5u Nmd4uFJgKDhgvGlhHrFWbawshWk150iwYdMFCLnBCSjYGqdSSZv/L71a8vJrdLf/0gox 0xhv/8PpHS88sbivqV2ytRdLVvRE5hqOwu/O9TxDI0ep+6SwgJCoTxfigo/k8ZlIzydX QwGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=PfV5/sPleXP1zNErEqBx1BszOKX9nyc6kUDhdopt2nk=; b=IqeYnjF1DzX8MJh7ahir9rSSHKoy3VwIqAuJYGJEaqE8dx4xaGmiv2U3ZCRbJe8kKe ZP7CBejv++X2adgdAUWvtcFNnaGakmtWBdEO538NeqgFds7J/W47vQHj4f2kxDWe2VAk qsshqSUulJ8cxZqmyNDUrX4Dl3RJU2i9qVpcqcFflmI9AUmpX9rMds5wT2dqlHfpwKWl dlsW+ctXbN1IkXFPrpmSLSnh6RO4Y+bs5G8ZmPoMU3b4itjrAPgJbi7uazxgK7E+mOly UeOAFcpLtDunnJ5XqKL8XEgNQGxp4z11EGR4RXxzTuta06fLCzmbHSIGJ7AR4RwkWsjW eT8g== X-Gm-Message-State: AOAM530/sKRtNjbI1LvH5OV/eHeqrNQJrOX0FbagMNLEpPaG2tW6T8oj CBTSCGTwsTP1V1bZ+4CiqtKsfQ== X-Google-Smtp-Source: ABdhPJxq5vO3LPBZ0UB2x5uR+Wlma9BfvO2NslPuWov3PggRBLlKfLpzM0xZ+HsCFJJcQLgeMk4DVw== X-Received: by 2002:a6b:2cce:0:b0:669:6ba7:11ce with SMTP id s197-20020a6b2cce000000b006696ba711cemr277659ios.158.1654511705499; Mon, 06 Jun 2022 03:35:05 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id q15-20020a92750f000000b002d3aff5d8b0sm5907531ilc.14.2022.06.06.03.35.05 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Jun 2022 03:35:05 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 4769B8007F5; Mon, 6 Jun 2022 05:35:04 -0500 (CDT) Date: Mon, 6 Jun 2022 05:35:04 -0500 From: Justin Pryzby To: Mayank Kandari Cc: pgsql-performance@postgresql.org Subject: Re: Query is taking too long i intermittent Message-ID: <20220606103504.GS29853@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: Archived-At: Precedence: bulk On Mon, Jun 06, 2022 at 03:28:43PM +0530, Mayank Kandari wrote: > Thanks for including the link*. (*FYI, I find it to be kind of unfriendly to ask the same question in multiple forums, simultaneously - it's like cross-posting. The goal seems to be to demand an answer from the internet community as quickly as possible.) > Indexes: > "event_pkey" PRIMARY KEY, btree (event_id, pkey) > "event_event_sec_event_usec_idx" btree (event_sec, event_usec) > When I execute the following query it takes 1 - 2 milliseconds to execute. > I am using libpq to connect the Postgres server in c++ code. Postgres > server version is 12.10 > Time is provided as a parameter to function executing this query, it > contains epoche seconds and microseconds. Are you using the simple query protocol or the extended protocol ? > This query is executed every 30 seconds on the same client connection > (Which is persistent for weeks). This process runs for weeks, but some time > same query starts taking more than 10 minutes. Once it takes 10 minutes, > after that every execution takes > 10 minutes. > If I restart the process it recreated connection with the server and now > execution time again falls back to 1-2 milliseconds. This issue is > intermittent, sometimes it triggers after a week of the running process and > sometime after 2 - 3 weeks of the running process. Could you get the query plan for the good vs bad executions ? To get the "bad" plan, I suggest to enable auto-explain and set its min duration to 10 seconds or 1 minute. The "good" plan you can get any time from psql. > SELECT event_id FROM event WHERE (event_sec > time.seconds) OR > ((event_sec=time.seconds) AND (event_usec>=time.useconds) ORDER BY > event_sec, event_usec LIMIT 1 I think it'd be better if the column was a float storing the fractional number of seconds. Currently, it may be hard for the planner to estimate rowcounts if the conditions are not independent. I don't know if it's related to this problem, though.