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 1hdH6y-0003Sq-1g for pgsql-general@arkaria.postgresql.org; Tue, 18 Jun 2019 16:37:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hdH6w-0002ft-Sv for pgsql-general@arkaria.postgresql.org; Tue, 18 Jun 2019 16:37:30 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hdH6w-0002ZV-G6 for pgsql-general@lists.postgresql.org; Tue, 18 Jun 2019 16:37:30 +0000 Received: from mail-qt1-x842.google.com ([2607:f8b0:4864:20::842]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hdH6t-0005iu-Ln for pgsql-general@postgresql.org; Tue, 18 Jun 2019 16:37:29 +0000 Received: by mail-qt1-x842.google.com with SMTP id w17so9718160qto.10 for ; Tue, 18 Jun 2019 09:37:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=MelmVNUl8DPEbsUPdY69KIqLVUwNMeQ7DdWMcnP04Yw=; b=eK4zM8sStAbOqNbsDioHMf6y88KG+qQ7vbAhmpb2Wh/2PxemFHvmK7nZcfWwyxLN7J lX4OBRHFYLZOTLG0BiFo0Ko6aOIwcieR/sQx0B84M7DAvdRxxuyNYagW6lVdAlPll+oX A9YRnVXRIfIEQVtx6F8+q5Y6LELRBGoPGsbOz/c0oP14XOABuXPk0pXgq9jss5XwVrAo qOWUXLaIgWmhYwVAEeFc9bTxpZseJa3ML3gOGuNO1c/iC35pELWJOeYQJSRaFxQgeexC Gtf+/Mh2XggTdPnHczhtqBAVk4+z0VHAteSW4kxDErNh6ZwXKDeIsiTVdo6i9/dnghon Fs3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=MelmVNUl8DPEbsUPdY69KIqLVUwNMeQ7DdWMcnP04Yw=; b=BeuAy/E5zYVc0R2qv84bEgVCGYTfDqDD7HvD4dxJJbt7ZMYjQWTJ6VMZ0AKW6qeygO acCoh/cPeOWg9mFm48JNNUJgGTRwgt8qkipbyMYQo2vXObhQf8S0dBtMaqkh6aRuWLMI YMPRIe7vwqVbTY8LeVGwknbgexxIrTpqbxbNE8FlregsxUqwCwRtDgPSUt+iGfK7BXrw qQaJhfw9oSCX9/LbZ0vjnobDF3cAyzEfadGFV2ozf1oOMN1xDXa3JGlWygUUXv/oH5xI 3s7esFUjUXiMHwcsSmEUSYBYl9RRq1ZUapF/Kk/pwNhbLpwgPoW4ZZpPxbJfJOPgv2eE zPtg== X-Gm-Message-State: APjAAAVcO469YpPrg1XOSZrNbNk5QRoARYcVC52kraGjSuWeVJfSheFB aVVRvzVp0Kw2LmwDDmEZnrllLw== X-Google-Smtp-Source: APXvYqz+Xi3qEVDG0O6TyJ7mr7KPaaxsVhWN1se+QaIFAezPpjKLY2Zc0kDqco5LYyV0aRYMijqCNA== X-Received: by 2002:ac8:877:: with SMTP id x52mr5258236qth.328.1560875846777; Tue, 18 Jun 2019 09:37:26 -0700 (PDT) Received: from nimloth.alvh.no-ip.org ([190.217.181.124]) by smtp.gmail.com with ESMTPSA id k6sm7931993qkd.21.2019.06.18.09.37.26 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 18 Jun 2019 09:37:26 -0700 (PDT) Received: by nimloth.alvh.no-ip.org (Postfix, from userid 1000) id E992212076F; Tue, 18 Jun 2019 12:37:23 -0400 (-04) Date: Tue, 18 Jun 2019 12:37:23 -0400 From: Alvaro Herrera To: Rob Nikander Cc: Postgres General Subject: Re: Is array_append O(n)? Message-ID: <20190618163723.GA14377@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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)? I’m trying to use plpgsql and > realizing I don’t really have my usual data structures for basic > algorithms the way I’d normally write them. I probably shouldn’t be > treating arrays like Java ArrayList or C++ vector, where you can > add/remove on the end with little cost (O(1), almost.) Starting with 9.5, there's an optimization that I think is supposed to make it O(1): commit 1dc5ebc9077ab742079ce5dac9a6664248d42916 Author: Tom Lane AuthorDate: Thu May 14 12:08:40 2015 -0400 CommitDate: Thu May 14 12:08:49 2015 -0400 Support "expanded" objects, particularly arrays, for better performance. This patch introduces the ability for complex datatypes to have an in-memory representation that is different from their on-disk format. On-disk formats are typically optimized for minimal size, and in any case they can't contain pointers, so they are often not well-suited for computation. Now a datatype can invent an "expanded" in-memory format that is better suited for its operations, and then pass that around among the C functions that operate on the datatype. There are also provisions (rudimentary as yet) to allow an expanded object to be modified in-place under suitable conditions, so that operations like assignment to an element of an array need not involve copying the entire array. The initial application for this feature is arrays, but it is not hard to foresee using it for other container types like JSON, XML and hstore. I have hopes that it will be useful to PostGIS as well. In this initial implementation, a few heuristics have been hard-wired into plpgsql to improve performance for arrays that are stored in plpgsql variables. We would like to generalize those hacks so that other datatypes can obtain similar improvements, but figuring out some appropriate APIs is left as a task for future work. (The heuristics themselves are probably not optimal yet, either, as they sometimes force expansion of arrays that would be better left alone.) Preliminary performance testing shows impressive speed gains for plpgsql functions that do element-by-element access or update of large arrays. There are other cases that get a little slower, as a result of added array format conversions; but we can hope to improve anything that's annoyingly bad. In any case most applications should see a net win. Tom Lane, reviewed by Andres Freund -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services