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 1lzKBS-0007pK-5t for pgsql-performance@arkaria.postgresql.org; Fri, 02 Jul 2021 14:30:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lzKBQ-0003Nc-6h for pgsql-performance@arkaria.postgresql.org; Fri, 02 Jul 2021 14:30:20 +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 1lzKBP-0003NU-QV for pgsql-performance@lists.postgresql.org; Fri, 02 Jul 2021 14:30:19 +0000 Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lzKBM-0007it-So for pgsql-performance@lists.postgresql.org; Fri, 02 Jul 2021 14:30:18 +0000 Received: by mail-wm1-x333.google.com with SMTP id j39-20020a05600c1c27b029020028e48b8fso864412wms.0 for ; Fri, 02 Jul 2021 07:30:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=gggGcB1Nkd5NtWBZlyyoHxEYw2gl0tmoQDFpjlloT3I=; b=VjHZ0FHVmpqFWTrIZt4Xkx14HurhPOq3LSDB8N1JP+9wQ2lMn/4smtnG5JjeVU0QOc MhY9jUMx2fpAZW9r/2n1YxXI5sCMF0FaOSWb59qPxPhOsktfQEWhHt55LKch40efSLjC aHFEXqNZiCMmzmidZ43/EYqhoOX25tUDFrwdElKw3dKmNGONp0Wp2aR4lvrv7kn52tSL h/VMYTGgXmQ/ng21LhEX0kV7gVokmGZm4bib0zdFQTq2NC4sdD7dj7RckkOrfrTPC/rr wd+lbKcd4qf/2IDrQdfjywoklcXu9pNFcv7fpEMypT88YWT/VGLd6XJKEW02oY4yRKYw hJTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=gggGcB1Nkd5NtWBZlyyoHxEYw2gl0tmoQDFpjlloT3I=; b=qu9bJKlzBbhhy6YZLvumM1caSDDTvKUpYjcGFeA5kqq72D4ZiSShcWU7SiX1uickTr H0ExRM224qopRKRWbQxsqLKw7g/v1QPWF/FPyca4PDGxYvS1bs9ZJaeN4rl12+4XTms6 IuKdcG0jPAQB/dE7rOjeNabjbsFJf94eah9o4nsrjgD/U2xeZajb2XRAOMeG9CAkPByy 5jAXO+VYRC+iB0MK8bmD/Zs9R9ktirsrhSyx/SNeR1jyFpZEJTi5SKaMIEuoWS1rKviZ 6FEfnUgTEP7yOGLg5EvNVINQG4JQCUiLCXOM9xJ0rXQKr+RmH/Q/ws7kAy2X+JQ+NPkt a3Bw== X-Gm-Message-State: AOAM531ilXgqabJAgDU5c6YtVazeIqloap4rMSdHt8LHdlvhXyao4jfZ MA7aX6IS1+LSaGFkde4+vXwcO4+odA3Mm9B9KnYgynEWgR6CLBKx9vNPJhWoq9SefIl57REl/hZ ds6zzh6t7iVfcHvoebyfyn69cB6i95h3T1P0SnE4wpVr4F0VfiNGeN9Z9MzFZI+2CD2MUl1sllj 4BYqwy3JO1LWbgha0AvtGmQEw+kLwqI1tLr0XNYlkv59AHOvzBM3yw2KIk0+KVlELpIRreH7pfR YE3raTaEqu9nGJnHdBSr+8jHORp581ad6SEEhaPdhT3cnMsOz3A99F5PgsgKHzV X-Google-Smtp-Source: ABdhPJxJjUGk/zALD/K9ACOYtdWxthWiew69NxNv0Hly/onubUTtKGt0GjHP7inuxo4xucyh8pvb5A== X-Received: by 2002:a1c:7201:: with SMTP id n1mr111704wmc.4.1625236215131; Fri, 02 Jul 2021 07:30:15 -0700 (PDT) Received: from [10.137.0.19] (ip-86-49-254-183.net.upcbroadband.cz. [86.49.254.183]) by smtp.gmail.com with ESMTPSA id j35sm13093067wms.7.2021.07.02.07.30.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 02 Jul 2021 07:30:14 -0700 (PDT) Subject: Re: slow performance with cursor To: Ayub Khan , Pgsql Performance References: From: Tomas Vondra Message-ID: <7a75ebb8-b5bc-81aa-ba9a-7c17abdeb706@enterprisedb.com> Date: Fri, 2 Jul 2021 16:30:13 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 7/1/21 10:25 PM, Ayub Khan wrote: > Justin, > > Below is the stored procedure, is there any scope for improvement? > Hard to say, based on just the stored procedure source code. The queries are not too complex, but we don't know which of them gets selected for each cursor, and which of them is the slow one. I suggest you identify which of the cursors is the most problematic one, and focus on investigating it alone. Show us the explain analyze for that query with different cursor_tuple_fraction values and without the cursort, and so on. As Tom said, for a cursor the optimizer may be picking a plan with low startup cost, on the basis that that's good for a cursor. But if you're always consuming all the tuples, that may be inefficient. It's often an issue for queries with LIMIT, but none of the queries you include that clause, so who knows ... Try identifying which of the cursors is causing the issues, show us the explain analyze for that query (with and without the cursor), and that should tell us more. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company