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 1jPFVS-0008M0-4n for pgsql-general@arkaria.postgresql.org; Fri, 17 Apr 2020 01:09: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 1jPFVR-0005tR-1l for pgsql-general@arkaria.postgresql.org; Fri, 17 Apr 2020 01:09:21 +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 1jPFVQ-0005tK-OQ for pgsql-general@lists.postgresql.org; Fri, 17 Apr 2020 01:09:20 +0000 Received: from mail-io1-xd41.google.com ([2607:f8b0:4864:20::d41]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jPFVJ-0003Mr-Ch for pgsql-general@lists.postgresql.org; Fri, 17 Apr 2020 01:09:19 +0000 Received: by mail-io1-xd41.google.com with SMTP id o127so583266iof.0 for ; Thu, 16 Apr 2020 18:09:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=ns+jAjQ8xKM3twFlFJiL6ZJTCUIEfieCQOBHsp+KNlQ=; b=aBkLg2jkHkvkvQulTIB5LYY9aRX5ifKdLqBqEToNn6+lHSkuajf5vx96Hqa7apEzow 2TLGk03It3p3HQKvhkvxLDOqGZ4uWgIly0MTRL1cvZNLePWtA5AkWKocdyZQ0FCDVVlA 3Ehc5nnEhMu7Rq+HI8+GT3JsUTXgOUAy1eK/It8MFPo8feKVKBYZ909F28Dhj5x4Jgid yIcDZ4+JRqIeCXXKOHMeJbQIAF05bt9DAxDtbVwFApODsfdaMGY8wet7mKGaqgSUXn0G yeCfXUEIl7e+R1gIloFB8iXK0wy6leiaTz/eCicjSq7z3k+ZSXC1r7UuXN5CgoUYpeJt BC5A== 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-transfer-encoding :content-language; bh=ns+jAjQ8xKM3twFlFJiL6ZJTCUIEfieCQOBHsp+KNlQ=; b=kw2Af5Hidu6ptpo4wxr+aVQqtKKS/NH+QbF79LTNWU27nrGF457VUO9KODDRXLA2+X BZBlOuXGtpV2IIFJ38P14rWptKfv8Xfu4tYQOpEViJ5LanUMPsGm66B8NOMdvDRZUozB VZ4UiSAsNDDu0c2qE+igjtwQ0/2hBAB45+zR1NDKY3Htx1X5VyjWbr/bT1dd8sS3P34y Ki+81mnRTnG7BUwK23PwWRw1JIt5w+oonALr2Z9y8j1g+EJvGogwdl59b4xUkAFOmRc0 sgcmNS2rIoLjueuQuL+ox7bHrsUfLKbd6+Jr0SDpqIHHkzC6wQZgWTjlfQMf0EuVfHv+ bssA== X-Gm-Message-State: AGi0PuZdI9TxnYMtXp5N7tsECFwCpEe6xME+k9FOisc7nitvQ12Zpjjj TvMUHE8Yn44EaCsSez7HwdwTlTDlYFE= X-Google-Smtp-Source: APiQypKaDQJIWSZgoGHCEwJvIVrnhtpaJ9E9c73cbuXhS6Ww0JQa7zYTPY7KoveZ5aMtlRUZqpzMVg== X-Received: by 2002:a02:ac94:: with SMTP id x20mr1044106jan.133.1587085752178; Thu, 16 Apr 2020 18:09:12 -0700 (PDT) Received: from ?IPv6:2601:681:5500:dde0:9d75:25e8:3f8a:bde6? ([2601:681:5500:dde0:9d75:25e8:3f8a:bde6]) by smtp.gmail.com with ESMTPSA id z8sm6517592ilq.61.2020.04.16.18.09.11 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 16 Apr 2020 18:09:11 -0700 (PDT) Subject: Re: performance of first exec of prepared statement To: pgsql-general@lists.postgresql.org References: <46565270-f610-a7ef-eff2-75e23ca27bcb@aklaver.com> From: Rob Sargent Message-ID: Date: Thu, 16 Apr 2020 19:09:10 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: <46565270-f610-a7ef-eff2-75e23ca27bcb@aklaver.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 4/16/20 6:15 PM, Adrian Klaver wrote: > On 4/16/20 4:59 PM, Ted Toth wrote: >> >> >> On Thu, Apr 16, 2020 at 6:29 PM Ted Toth > > wrote: >> >>     I've noticed that the first exec of an INSERT prepared statement >>     takes ~5 time longer (I'm using libpq in C and wrapping the calls to >>     time them) then subsequent exec's is this the expected behavior and >>     if so is there any thing I can do to mitigate this affect? >> >>     Ted >> >> >> For example (in my environment) I'm seeing the prepare take ~10ms, >> the first exec take ~30 ms and subsequent exec's take ~4 ms. >> > > I don't have an answer. I believe though that to help those that might > it would be helpful to show the actual code. > > You expect the subsequent calls to benefit from the cached query parse and planning.  What does you query cost without begin wrapped in a prepared statement (preferably from a cold start).