pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Resources on modeling ordered hierachies?
3+ messages / 3 participants
[nested] [flat]

* Resources on modeling ordered hierachies?
@ 2022-04-07 05:45 Tim Uckun <timuckun@gmail.com>
  2022-04-07 16:55 ` Re: Resources on modeling ordered hierachies? Peter J. Holzer <hjp-pgsql@hjp.at>
  0 siblings, 1 reply; 3+ messages in thread

From: Tim Uckun @ 2022-04-07 05:45 UTC (permalink / raw)
  To: pgsql-general

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.

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

Items could of course be "promoted"  this way or moved up and down the
hierarchy to be below or above their peers.

Does anybody know of a more elegant way to do this?





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Resources on modeling ordered hierachies?
  2022-04-07 05:45 Resources on modeling ordered hierachies? Tim Uckun <timuckun@gmail.com>
@ 2022-04-07 16:55 ` Peter J. Holzer <hjp-pgsql@hjp.at>
  2022-04-07 18:12   ` Re: Resources on modeling ordered hierachies? Rob Sargent <robjsargent@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Peter J. Holzer @ 2022-04-07 16:55 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

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

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Resources on modeling ordered hierachies?
  2022-04-07 05:45 Resources on modeling ordered hierachies? Tim Uckun <timuckun@gmail.com>
  2022-04-07 16:55 ` Re: Resources on modeling ordered hierachies? Peter J. Holzer <hjp-pgsql@hjp.at>
@ 2022-04-07 18:12   ` Rob Sargent <robjsargent@gmail.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Rob Sargent @ 2022-04-07 18:12 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

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.

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2022-04-07 18:12 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-04-07 05:45 Resources on modeling ordered hierachies? Tim Uckun <timuckun@gmail.com>
2022-04-07 16:55 ` Peter J. Holzer <hjp-pgsql@hjp.at>
2022-04-07 18:12   ` Rob Sargent <robjsargent@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox