Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fVOEI-0007x5-Ao for pgsql-general@arkaria.postgresql.org; Tue, 19 Jun 2018 21:31:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fVOEG-0001Hy-Ty for pgsql-general@arkaria.postgresql.org; Tue, 19 Jun 2018 21:31:56 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fVOEG-0001Hp-F2 for pgsql-general@lists.postgresql.org; Tue, 19 Jun 2018 21:31:56 +0000 Received: from mail-pf0-x231.google.com ([2607:f8b0:400e:c00::231]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fVOED-0004dI-9x for pgsql-general@lists.postgresql.org; Tue, 19 Jun 2018 21:31:55 +0000 Received: by mail-pf0-x231.google.com with SMTP id q1-v6so479893pff.13 for ; Tue, 19 Jun 2018 14:31:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=illuminatedcomputing-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=22sdBOGFaszHYSkPDqG+azXXF6331cezHs24UtcD80E=; b=GWF6ovEksIglQjZzHTQlqQmNadLY2lGlNorBb5iNDRw4lk6ijxQMUgeY48dCR0YTjR tKk3WQnYb3HANHEUMvM4JwL49FC40fhpyUdMuaGRC2h0Lhr7M19H0K2ioy9o904gUD5p 6DLxeNHStEGKO2BmI8oUx6dG/zqykarIpcgeJ75HqKzD8SLomi7m+zan4SmmPNXPGLiL uHz/CBIh2WeTdLSknKQsx/41mxJ02jYb89/Is9/Ur+9KhFSwLGkePfaxWQ4BSzNPnsHr +DpOFRPe6zW1WjiCLJOfZEiQeJBT7QBw/X5bkMuFbT6odcMQbiIXgoZwMnjq2QiydCzU z0HA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=22sdBOGFaszHYSkPDqG+azXXF6331cezHs24UtcD80E=; b=YbIH+XFif9xCEepjrlO7GMycJL9iNA7xEYuMNAsr5dGtR/K3TYQVJQuDFzioL4/bj9 ocutk7NMauDGrqcRWt/bQUwweip60PD4oyUoKOtpRS+Jy8AsDZr0bmJGOq4JWG/2bkJZ XNq31I354f/GdBCzvqY4jsAHX+89YQcwTR1xRDDWhEOYLKbaSX3mLkFvtoQDnD86lySP 7CH3D9U3NOFhzRfoteNjGT4H6NbPSGJCkhjrGPOAS+SQePqQ8QrFaUsI7oTI3jR5s+iL YhJYbPPN8KONb8ajSW+Bz8CPqDsUOGCq3aiL271VIyJHos673iBbuRZ0iu2vKKPTRN36 AoCQ== X-Gm-Message-State: APt69E02AZs7bq8szoTZsE48op0RzpjI4xL3g6/OJNa07lSbkNgaSOnI ycagu+dgkgaWm7il+sJ1N6htz5sc X-Google-Smtp-Source: ADUXVKIEsGC5IhFxAe746UAwio8K4L9OWIBJpJVVKfj8kb7Ykp+GgvWGnh0T3eQ5bIWWvH7ZOHfO7A== X-Received: by 2002:a63:bd01:: with SMTP id a1-v6mr16236444pgf.319.1529443911824; Tue, 19 Jun 2018 14:31:51 -0700 (PDT) Received: from [192.168.2.201] (static-50-53-109-254.bvtn.or.frontiernet.net. [50.53.109.254]) by smtp.googlemail.com with ESMTPSA id h124-v6sm790159pfc.100.2018.06.19.14.31.51 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Jun 2018 14:31:51 -0700 (PDT) Subject: Re: Is postorder tree traversal possible with recursive CTE's? To: pgsql-general@lists.postgresql.org References: <5196CEBE-CF2E-48AF-A496-3CE2FE1B6FD2@gmail.com> <8C64B1D2-B326-44F3-8526-08B0AEEA29E4@gmail.com> From: Paul Jungwirth Message-ID: <1e2e2542-3fbf-e419-fd9a-4e3f02421bbb@illuminatedcomputing.com> Date: Tue, 19 Jun 2018 14:31:50 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: <8C64B1D2-B326-44F3-8526-08B0AEEA29E4@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 06/19/2018 02:05 PM, Alban Hertroys wrote: > On the more theoretical front: The question remains whether it is possible to calculate fields in post-order tree traversal. I think that would be a semantically proper way to express this type of problem and it wouldn't need the kinds of pre/post-processing that after-the-fact aggregation (like in above solution) requires. So, leaner, and probably faster. > That implies that the SQL committee thought of the possibility in the first place though, which I'm beginning to doubt... If this interests you, you might enjoy this StackOverflow question: https://stackoverflow.com/questions/35956486/generate-nested-json-with-couting-in-postgresql Briefly, how do you construct a nested JSON structure from a recursive CTE? The only answers at that link rely on plpgsql, but of course that is cheating. :-) I took a stab at it a couple years ago but couldn't figure it out, and it seemed like post-order processing was exactly the missing piece. If anyone has any ideas I'd be intrigued to hear them! -- Paul ~{:-) pj@illuminatedcomputing.com