Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hdID0-0005rx-Vx for pgsql-general@arkaria.postgresql.org; Tue, 18 Jun 2019 17:47:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hdICz-0006F5-EG for pgsql-general@arkaria.postgresql.org; Tue, 18 Jun 2019 17:47:49 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hdICz-0006Be-1x for pgsql-general@lists.postgresql.org; Tue, 18 Jun 2019 17:47:49 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hdICw-0004Sk-D4 for pgsql-general@postgresql.org; Tue, 18 Jun 2019 17:47:48 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x5IHlgXx015731; Tue, 18 Jun 2019 13:47:42 -0400 From: Tom Lane To: Rob Nikander cc: Postgres General Subject: Re: Is array_append O(n)? In-reply-to: References: <20190618163723.GA14377@alvherre.pgsql> Comments: In-reply-to Rob Nikander message dated "Tue, 18 Jun 2019 19:48:39 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <15729.1560880062.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 18 Jun 2019 13:47:42 -0400 Message-ID: <15730.1560880062@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Rob Nikander writes: > On Jun 18, 2019, at 7:37 PM, Alvaro Herrera w= rote: >> On 2019-Jun-18, Rob Nikander wrote: >>> Does `array_append(arr, elt)` create a new array and copy everything? >>> In other words, is it O(n) or O(1)? [=E2=80=A6] >> Starting with 9.5, there's an optimization that I think is supposed to >> make it O(1): > Sweet! Thanks. Now I=E2=80=99d like to know how to tell if that optimiza= tion is being used, or not. = Time it and see ;-) =46rom memory, the cases that should be fast now are * Assigning to an element of a plpgsql variable that is an array, ie "arrayvar[n] :=3D newelement". * "arrayvar :=3D arrayvar || newarrayelement". I think it doesn't matter whether you use operator or function-call syntax. > Same question for the a way to pop an element off the end in O(1) time. Offhand I don't remember any optimization for that, but I might be wrong. Looking at the comments in the plpgsql portion of the aforementioned commit would probably give more info. regards, tom lane