Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id 1FDF02E002D for ; Thu, 22 May 2008 20:08:25 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 17513-02 for ; Thu, 22 May 2008 20:08:17 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from oxford.xeocode.com (unknown [207.219.199.245]) by developer.postgresql.org (Postfix) with ESMTP id E147E2E005E for ; Thu, 22 May 2008 20:08:21 -0300 (ADT) Received: from localhost ([127.0.0.1] helo=oxford.xeocode.com) by oxford.xeocode.com with esmtp (Exim 4.69) (envelope-from ) id 1JzHt4-0001sQ-VB; Thu, 22 May 2008 16:59:47 -0400 To: "Richard Huxton" Cc: "Jonah H. Harris" , "H. Hall" , Subject: Re: "Big O" notation for postgres? In-Reply-To: <4834343A.8020607@archonet.com> (Richard Huxton's message of "Wed\, 21 May 2008 15\:39\:54 +0100") User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux) X-Draft-From: ("nnimap+mail01.enterprisedb.com:INBOX.performance" 5752) References: <48342D6D.1020506@reedyriver.com> <36e682920805210728p4fa6c12ft592b1045cbae9dfd@mail.gmail.com> <4834343A.8020607@archonet.com> From: Gregory Stark Organization: EnterpriseDB Date: Thu, 22 May 2008 16:59:46 -0400 Message-ID: <8763t69hrx.fsf@oxford.xeocode.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200805/311 X-Sequence-Number: 30279 "Richard Huxton" writes: > Jonah H. Harris wrote: >> On Wed, May 21, 2008 at 10:10 AM, H. Hall wrote: >>> Does anyone know if there is a source that provides "Big O" notation for >>> postgres's aggregate functions and operations? For example is count(*) = >>> O(1) or O(n)? >> >> I don't know of any document containing the complexity of each >> aggregate, but it's sometimes left as a comment in the souce code. > > Recent max() and min() can be O(n) or O(1) depending on the where-clause and > presence of an index too, just to muddy the waters. Hm, true. But excluding those special cases all Postgres aggregate functions will be O(n) unless they're doing something very odd. None of the built-in functions (except min/max as noted) do anything odd like that. The reason way is because of the basic design of Postgres aggregate functions. They are fed every tuple one by one and have to keep their state in a single variable. Most of the aggregate functions like count(*) etc just keep a static non-growing state and the state transition function is a simple arithmetic operation which is O(1). So the resulting operation is O(n). Actually one exception would be something like CREATE AGGREGATE array_agg(anyelement) (SFUNC = array_append, STYPE = anyarray, INITCOND='{}'); Since the state variable has to keep accumulating more and more stuff the array_append becomes more and more expensive (it has to generate a new array so it has to copy the existing stuff). So actually it woul dbe O(n^2). The only builtin aggregate which looks like it falls in this category would be xmlagg() -- Gregory Stark EnterpriseDB http://www.enterprisedb.com Ask me about EnterpriseDB's RemoteDBA services!