From: Tom Lane <tgl@sss.pgh.pa.us>
To: Rob Nikander <rob.nikander@gmail.com>
Cc: Postgres General <pgsql-general@postgresql.org>
Subject: Re: Is array_append O(n)?
Date: Tue, 18 Jun 2019 13:47:42 -0400
Message-ID: <15730.1560880062@sss.pgh.pa.us> (raw)
In-Reply-To: <BBFE0E5C-BC0C-446C-921A-E498DFFF48CC@gmail.com>
References: <20190618163723.GA14377@alvherre.pgsql>
<BBFE0E5C-BC0C-446C-921A-E498DFFF48CC@gmail.com>
Rob Nikander <rob.nikander@gmail.com> writes:
> On Jun 18, 2019, at 7:37 PM, Alvaro Herrera <alvherre@2ndquadrant.com> wrote:
>> 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)? […]
>> Starting with 9.5, there's an optimization that I think is supposed to
>> make it O(1):
> Sweet! Thanks. Now I’d like to know how to tell if that optimization is being used, or not.
Time it and see ;-)
From memory, the cases that should be fast now are
* Assigning to an element of a plpgsql variable that is an array,
ie "arrayvar[n] := newelement".
* "arrayvar := 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: tgl@sss.pgh.pa.us, rob.nikander@gmail.com
Subject: Re: Is array_append O(n)?
In-Reply-To: <15730.1560880062@sss.pgh.pa.us>
References: <20190618163723.GA14377@alvherre.pgsql> <BBFE0E5C-BC0C-446C-921A-E498DFFF48CC@gmail.com> <15730.1560880062@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox