Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ncWd3-0007ze-KX for pgsql-general@arkaria.postgresql.org; Thu, 07 Apr 2022 18:13:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ncWd2-0002no-6b for pgsql-general@arkaria.postgresql.org; Thu, 07 Apr 2022 18:13:08 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ncWd1-0002nf-RI for pgsql-general@lists.postgresql.org; Thu, 07 Apr 2022 18:13:07 +0000 Received: from mail-pj1-x1034.google.com ([2607:f8b0:4864:20::1034]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ncWcv-0004tv-JB for pgsql-general@lists.postgresql.org; Thu, 07 Apr 2022 18:13:07 +0000 Received: by mail-pj1-x1034.google.com with SMTP id m12-20020a17090b068c00b001cabe30a98dso9603935pjz.4 for ; Thu, 07 Apr 2022 11:13:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to; bh=envce2m8WhYDaVLGTekcu3LcAwl8fphY91/AZY9EhQs=; b=LtXcoJCqVBbn6e3wlEKM9zPLH9/9gifoFzcy4hV27PSRC5lfgTwG2Bnclet1Ve5oZE HmwiONYTSkrY9b8OiauEKow54WFXAK+zrN1ICACNlNB5R09nW71O2ROZdFtUWTO0yCUU oRE0hsR3vRUB60naPz4FMo8QqMUn44pVgWxkF4MQANigX9feIfLONNbfzHGWhWVotQ5D W6iQ20sG9NRf8pIn1k+iGX0QT4x+UUTsAsxM7fdwSYNngoye8clQRBgztO5JGe+J1xz0 kghTKuVFTtGfmSL7GA6USAuj8KMyK588XizVqNARJTOGy8vKvDhwBFxENYN66qxo/jSu 6vaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to; bh=envce2m8WhYDaVLGTekcu3LcAwl8fphY91/AZY9EhQs=; b=ZTG1Yy4zi1Fko6Yw+Ys/bP1JdlI89GSBdqlkhN5dQDzsCDJ9AE7/hgI/FaQOfK2Arh J9Tt83kOj4b0v8E1NFdnZ7ch1glR4XXHMoKGVsF7COZqIT7mWGMwtzq9Ej1hTUU9C92y W940NKGdX7RorVi5yzdGK8JQ6aVHQgcYAUJ82Dyws21NQOj0+3RO4VcnzYlYu+X2m4vH aRj9pPe/gWSg67kbPL4Z/LYo6a32YvShaD4x/Htyw83aLbWTaBGns1sUOWfLmilhPd1c RitDZRmK0udPYs2MCl/1779KUTnYhWH0F8gL25WROgnOFvNOIrkDJgUKW5+a+RBvMkUn L8RQ== X-Gm-Message-State: AOAM532Z24oZKGEniWPQnFRJQpl3ppTFi2xPB1nH2ewk/O6Z2Z8VRP4J 72KARp6VcFenslX3uQzh6MBcQhf+nuA= X-Google-Smtp-Source: ABdhPJxWUPqkMNYM1niVAtx30c6L4HzGCjC5irfDDiQ05XpNiJ+KGrPyVMf333f3WtpK40mwn3c7Tg== X-Received: by 2002:a17:903:2cb:b0:14f:4fb6:2fb0 with SMTP id s11-20020a17090302cb00b0014f4fb62fb0mr14644700plk.172.1649355178489; Thu, 07 Apr 2022 11:12:58 -0700 (PDT) Received: from [10.128.71.194] ([155.98.131.2]) by smtp.gmail.com with ESMTPSA id d21-20020a056a0024d500b004fb0e7c7c3bsm24968015pfv.161.2022.04.07.11.12.57 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 07 Apr 2022 11:12:57 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------IcmKkg0V7m0nWIlnB0BNznvI" Message-ID: Date: Thu, 7 Apr 2022 12:12:56 -0600 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: Resources on modeling ordered hierachies? Content-Language: en-CA To: pgsql-general@lists.postgresql.org References: <20220407165534.ekazovgid6n5y2i3@hjp.at> From: Rob Sargent In-Reply-To: <20220407165534.ekazovgid6n5y2i3@hjp.at> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------IcmKkg0V7m0nWIlnB0BNznvI Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 4/7/22 10:55, Peter J. Holzer wrote: > On 2022-04-07 17:45:49 +1200, Tim Uckun wrote: >> There a tons of articles about how to model hierarchies in SQL but I >> haven't seen any about dealing with hierarchies where the order of >> children is important. >> >> The canonical example is a simple outline >> >> 1. >> 1.1 >> 1.1.1 >> 1.2 >> 2. >> 2.1 >> >> etc >> >> If I am doing an insert where parent is 1.1 it should name it 1.1.2 >> which to me means doing something like select max(id) + 1 where parent >> = 1.1 or something like that which might turn out to be expensive. > Shouldn't be that bad with an appropriate index. Bigger problem might be > that two transactions could attempt this at the same time. > > >> Similarly if I want to insert something between 1.1 and 1.2 I need to >> do something like update id set id = id+1 where parent = 1 and id >1 > > You can mostly get around that by using float8 or even numeric instead > of int. Chances are that there is a free number between you numbers. > > hp > Is there any chance the 1/1.1/1.1.1 stuff can be cosmetic/generated? Corollary: what determines the hierarchical position of the next record? Each item could know its parent (nullable) and it next-sib (nullable). If, as your example might suggest, you are always adding to the end, then you're updating the last sib's null next-sib point and the incoming record has next-sib null.  All the single numbers are parentless, 2 is next sib of 1.  Trickier to interject a new record between to sibs but not impractical.  Can move sibships up/down hierarchy by updating parent and resetting next-sib of last in moved sibship and the next-sib of the injection point to the head of the move sibs. --------------IcmKkg0V7m0nWIlnB0BNznvI Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
On 4/7/22 10:55, Peter J. Holzer wrote:
On 2022-04-07 17:45:49 +1200, Tim Uckun wrote:
There a tons of articles about how to model hierarchies in SQL but I
haven't seen any about dealing with hierarchies where the order of
children is important.

The canonical example is a simple outline

1.
1.1
1.1.1
1.2
2.
2.1

etc

If I am doing an insert where parent is 1.1 it should name it 1.1.2
which to me means doing something like select max(id) + 1 where parent
= 1.1 or something like that which might turn out to be expensive.
Shouldn't be that bad with an appropriate index. Bigger problem might be
that two transactions could attempt this at the same time.


Similarly if I want to insert something between 1.1 and 1.2 I need to
do something like update id set id = id+1 where parent = 1 and id >1

You can mostly get around that by using float8 or even numeric instead
of int. Chances are that there is a free number between you numbers.

        hp

Is there any chance the 1/1.1/1.1.1 stuff can be cosmetic/generated? Corollary: what determines the hierarchical position of the next record? 

Each item could know its parent (nullable) and it next-sib (nullable). If, as your example might suggest, you are always adding to the end, then you're updating the last sib's null next-sib point and the incoming record has next-sib null.  All the single numbers are parentless, 2 is next sib of 1.  Trickier to interject a new record between to sibs but not impractical.  Can move sibships up/down hierarchy by updating parent and resetting next-sib of last in moved sibship and the next-sib of the injection point to the head of the move sibs.
--------------IcmKkg0V7m0nWIlnB0BNznvI--