Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id AAA91B5D91F for ; Thu, 16 Jun 2011 15:36:41 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 48409-04 for ; Thu, 16 Jun 2011 18:36:35 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp2.peak6.com (smtp2.peak6.com [38.98.137.9]) by mail.postgresql.org (Postfix) with ESMTP id 097D7B5DBD6 for ; Thu, 16 Jun 2011 15:36:34 -0300 (ADT) X-AuditID: 0a02cb1e-b7b55ae000000a02-71-4dfa4d300ac3 Message-ID: <4DFA4D2F.8020101@peak6.com> Date: Thu, 16 Jun 2011 13:36:31 -0500 From: Shaun Thomas Reply-To: Organization: Peak6 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101210 Lightning/1.0b2 Thunderbird/3.1.7 MIME-Version: 1.0 To: Magnus Hagander CC: Svetlin Manavski , Subject: Re: seq scan in the case of max() on the primary key column References: In-Reply-To: Content-Type: text/plain; charset="ISO-8859-1"; format=flowed content-transfer-encoding: 7bit X-Originating-IP: [10.10.221.50] X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrLIsWRmVeSWpSXmKPExsXCxcQrpmvg+8vX4N4MYYsVn+6wWlyfeY7Z Yu7ds0wOzB47Z91l99jUdpzZ48K+aewBzFENjDaJeXn5JYklqQopqcXJtkoBro7eZgrBJYl5 KYlFKQoumcXJOYmZualFSgqZKbZKRkoKBTmJyam5qXkltkqJBQWpeSlKdlwKGMAGqCwzTyE1 Lzk/JTMv3VbJM9hf18LC1FLXUMkuHgES2lgyWo7+ZCzYxlux6vRaxgbGJq4uRk4OCQETiUM7 TjND2GISF+6tZwOxeQU0JNbt+QxmswioSuxtussEYrMJqEks3H2TEcQWEpCV2L/qHCuIzS8g LLHvQxPYHFGBQolvR28xQ8wRlDg58wkLiC0ioCnxae5hsF5mgXCJC9unsYPYwgJ+ElfPNwLV cwDNLJVYcb4AJMwp4Chx/s4NNohyW4kLc66zQNjyEtvfzmGGOEFO4k7DczaQVgkBBYl7f50m MArNQrJ4FpLuWUi6FzAyr2IULy5LTM7INCsozi0pMNQrSE3MNtPLSy3ZxAiO5NNyOxhXXFA8 xCjAwajEwyv49IevEGtiWXFl7iFGCQ5mJRHeU79/+grxpiRWVqUW5ccXleakFh9i1AaG2ERm Ke7kfGCSySuJNzYwUBLn/eL61ldIIB2YBLJTUwtSi2CqmTg4pRoYPRnXL1+7zy+Z74MXw6b5 jya6iAspSBRwbxJcaHiP94S4gO57vcbQutxjZ+r2LilwuBl+Y4IaW7zuAukLu8MeyX2/9+W3 aP5Vk9nXRTR6bx/6rl3wXm/leRf5+3lxN7nW24oLtRhPZGTKWCX44tWkBJXCWO9fQVyJG9Kl ApceXNNtqRj5cdkXJZbijERDLeai4kQA+tzzzPACAAA= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201106/143 X-Sequence-Number: 44076 On 06/16/2011 12:25 PM, Magnus Hagander wrote: > PostgreSQL 9.0 is unable to use an index scan to find min/max on a > partitioned table. 9.1, however, can do that. Unfortunately this is true. You can fake it this way though: /** * Return the Maximum INT Value for a Partitioned Table Column * * @param string Name of Schema of the base partition table. * @param string Name of the base partition table. * @param string Name of column to search. */ CREATE OR REPLACE FUNCTION spc_max_part_int(VARCHAR, VARCHAR, VARCHAR) RETURNS INT AS $$ DECLARE sSchema ALIAS FOR $1; sTable ALIAS FOR $2; sColName ALIAS FOR $3; sChild VARCHAR; nMax INT; nTemp INT; nParent OID; BEGIN EXECUTE ' SELECT max(' || sColName ||') FROM ONLY ' || sSchema || '.' || quote_ident(sTable) INTO nMax; SELECT INTO nParent t.oid FROM pg_class t JOIN pg_namespace n ON (t.relnamespace=n.oid) WHERE n.nspname = sSchema AND t.relname = sTable; FOR sChild IN SELECT t.relname FROM pg_class t JOIN pg_inherits c ON (c.inhrelid=t.oid AND c.inhparent=nParent) LOOP nTemp := utility.spc_max_part_int(sSchema, sChild, sColName); nMax := greatest(nTemp, nMax); END LOOP; RETURN nMax; END; $$ LANGUAGE plpgsql STABLE; You can call that instead of max, and it'll be much faster. You can create an analog for min if you need it. So for this, you'd call: SELECT spc_max_part_int('appqosdata', 'tcpsessions', 'id'); Someone probably has a better solution. :) -- Shaun Thomas OptionsHouse | 141 W. Jackson Blvd. | Suite 800 | Chicago IL, 60604 312-676-8870 sthomas@peak6.com ______________________________________________ See http://www.peak6.com/email_disclaimer.php for terms and conditions related to this email