Add red-black tree missing comparison searches

Lists: pgsql-hackers
From: Steve Chavez <steve(at)supabase(dot)io>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Add red-black tree missing comparison searches
Date: 2022-06-30 16:51:22
Message-ID: CAGRrpzYE8-7GCoaPjOiL9T_HY605MRax-2jgTtLq236uksZ1Sw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

Hello hackers,

Currently the red-black tree implementation only has an equality search.
Other extensions might need other comparison searches, like less-or-equal
or greater-or-equal. For example OrioleDB defines a greater-or-equal search
on its postgres fork:

https://github.com/orioledb/postgres/blob/4c18ae94c20e3e95c374b9947f1ace7d1d6497a1/src/backend/lib/rbtree.c#L164-L186

So I thought this might be valuable to have in core. I've added
less-or-equal and greater-or-equal searches functions plus tests in
the test_rbtree module. I can add the remaining less/great searches if this
is deemed worth it.

Also I refactored the sentinel used in the rbtree.c to use C99 designators.

Thanks in advance for any feedback!

--
Steve Chavez
Engineering at https://supabase.com/

Attachment Content-Type Size
0001-Add-red-black-tree-missing-comparison-searches.patch text/x-patch 6.4 KB

From: Greg Stark <stark(at)mit(dot)edu>
To: Steve Chavez <steve(at)supabase(dot)io>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Add red-black tree missing comparison searches
Date: 2022-06-30 17:09:02
Message-ID: CAM-w4HNHdd2PUPmbHM155v4MHY_nF7TxWNouMHX-wq05Nmg4aQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

Please add this to the commitfest at
https://commitfest.postgresql.org/38/ so it doesn't get missed. The
commitfest starts imminently so best add it today.


From: Steve Chavez <steve(at)supabase(dot)io>
To: Greg Stark <stark(at)mit(dot)edu>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Add red-black tree missing comparison searches
Date: 2022-06-30 17:15:41
Message-ID: CAGRrpzY-LiZ8=LMXTyG2UTC79mJm2VL=cPMhiT381NrFyUqcoQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

Yes, I've already added it here: https://commitfest.postgresql.org/38/3742/

Thanks!

On Thu, 30 Jun 2022 at 12:09, Greg Stark <stark(at)mit(dot)edu> wrote:

> Please add this to the commitfest at
> https://commitfest.postgresql.org/38/ so it doesn't get missed. The
> commitfest starts imminently so best add it today.
>


From: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
To: Steve Chavez <steve(at)supabase(dot)io>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Add red-black tree missing comparison searches
Date: 2022-06-30 19:34:38
Message-ID: CAPpHfdvZmZASCRv9X+=aQJnuGqB+8h5PDTDD6=n6G2EcmaaPdw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

Hi, Steve!

Thank you for working on this.

On Thu, Jun 30, 2022 at 7:51 PM Steve Chavez <steve(at)supabase(dot)io> wrote:
> Currently the red-black tree implementation only has an equality search. Other extensions might need other comparison searches, like less-or-equal or greater-or-equal. For example OrioleDB defines a greater-or-equal search on its postgres fork:
>
> https://github.com/orioledb/postgres/blob/4c18ae94c20e3e95c374b9947f1ace7d1d6497a1/src/backend/lib/rbtree.c#L164-L186
>
> So I thought this might be valuable to have in core. I've added less-or-equal and greater-or-equal searches functions plus tests in the test_rbtree module. I can add the remaining less/great searches if this is deemed worth it.

Looks good. But I think we can support strict inequalities too (e.g.
less and greater without equals). Could you please make it a bool
argument equal_matches?

> Also I refactored the sentinel used in the rbtree.c to use C99 designators.

Could you please extract this change as a separate patch.

------
Regards,
Alexander Korotkov


From: Steve Chavez <steve(at)supabase(dot)io>
To: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Add red-black tree missing comparison searches
Date: 2022-07-02 19:38:41
Message-ID: CAGRrpzZLX8P4vq1othhDtDuYWE5rKtd3PHAjss-39fH_ud9CJA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

Hey Alexander,

> But I think we can support strict inequalities too (e.g.
less and greater without equals). Could you please make it a bool
argument equal_matches?

Sure, an argument is a good idea to keep the code shorter.

> Could you please extract this change as a separate patch.

Done!

On Thu, 30 Jun 2022 at 14:34, Alexander Korotkov <aekorotkov(at)gmail(dot)com>
wrote:

> Hi, Steve!
>
> Thank you for working on this.
>
> On Thu, Jun 30, 2022 at 7:51 PM Steve Chavez <steve(at)supabase(dot)io> wrote:
> > Currently the red-black tree implementation only has an equality search.
> Other extensions might need other comparison searches, like less-or-equal
> or greater-or-equal. For example OrioleDB defines a greater-or-equal search
> on its postgres fork:
> >
> >
> https://github.com/orioledb/postgres/blob/4c18ae94c20e3e95c374b9947f1ace7d1d6497a1/src/backend/lib/rbtree.c#L164-L186
> >
> > So I thought this might be valuable to have in core. I've added
> less-or-equal and greater-or-equal searches functions plus tests in the
> test_rbtree module. I can add the remaining less/great searches if this is
> deemed worth it.
>
> Looks good. But I think we can support strict inequalities too (e.g.
> less and greater without equals). Could you please make it a bool
> argument equal_matches?
>
> > Also I refactored the sentinel used in the rbtree.c to use C99
> designators.
>
> Could you please extract this change as a separate patch.
>
> ------
> Regards,
> Alexander Korotkov
>

Attachment Content-Type Size
0001-Change-rbtree-sentinel-to-C99-designator.patch text/x-patch 649 bytes
0002-Add-rbtree-missing-comparison-searches.patch text/x-patch 6.3 KB

From: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
To: Steve Chavez <steve(at)supabase(dot)io>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Add red-black tree missing comparison searches
Date: 2022-07-06 18:53:41
Message-ID: CAPpHfdsuMK0R0FKga+UbJs9Ni_gO5tRAm_EJ=e4KJhWuatuq2A@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

Hi, Steve!

On Sat, Jul 2, 2022 at 10:38 PM Steve Chavez <steve(at)supabase(dot)io> wrote:
> > But I think we can support strict inequalities too (e.g.
> less and greater without equals). Could you please make it a bool
> argument equal_matches?
>
> Sure, an argument is a good idea to keep the code shorter.
>
> > Could you please extract this change as a separate patch.
>
> Done!

Thank you!

I did some improvements to the test suite, run pgindent and wrote
commit messages.

I think this is quite straightforward and low-risk patch. I'm going
to push it if no objections.

------
Regards,
Alexander Korotkov

Attachment Content-Type Size
0001-Use-C99-designator-in-the-rbtree-sentinel-definit-v2.patch application/octet-stream 728 bytes
0002-Add-missing-inequality-searches-to-rbtree-v2.patch application/octet-stream 7.0 KB

From: Steve Chavez <steve(at)supabase(dot)io>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Fwd: Add red-black tree missing comparison searches
Date: 2022-07-06 23:15:45
Message-ID: CAGRrpzaeKaLiVu9HEdxFn4F1LQVdQkZPcHXham0o4ct+yuYDPg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

---------- Forwarded message ---------
From: Steve Chavez <steve(at)supabase(dot)io>
Date: Wed, 6 Jul 2022 at 18:14
Subject: Re: Add red-black tree missing comparison searches
To: Alexander Korotkov <aekorotkov(at)gmail(dot)com>

Thanks Alexander!

wrt to the new patch. I think the following comment is misleading since
keyDeleted can be true or false:

+ /* switch equal_match to false so we only find greater matches now */
+ node = (IntRBTreeNode *) rbt_find_great(tree, (RBTNode *) &searchNode,
+ keyDeleted);

Maybe it should be the same used for searching lesser keys:

+ /*
+ * Find the next key. If the current key is deleted, we can pass
+ * equal_match == true and still find the next one.
+ */

On Wed, 6 Jul 2022 at 13:53, Alexander Korotkov <aekorotkov(at)gmail(dot)com>
wrote:

> Hi, Steve!
>
> On Sat, Jul 2, 2022 at 10:38 PM Steve Chavez <steve(at)supabase(dot)io> wrote:
> > > But I think we can support strict inequalities too (e.g.
> > less and greater without equals). Could you please make it a bool
> > argument equal_matches?
> >
> > Sure, an argument is a good idea to keep the code shorter.
> >
> > > Could you please extract this change as a separate patch.
> >
> > Done!
>
> Thank you!
>
> I did some improvements to the test suite, run pgindent and wrote
> commit messages.
>
> I think this is quite straightforward and low-risk patch. I'm going
> to push it if no objections.
>
> ------
> Regards,
> Alexander Korotkov
>


From: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
To: Steve Chavez <steve(at)supabase(dot)io>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Add red-black tree missing comparison searches
Date: 2022-07-07 10:43:55
Message-ID: CAPpHfdur38rJhzWPw52DWrtS5QFxMBZjDWrLs1P_uPb0MjuV7A@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Jul 7, 2022 at 2:16 AM Steve Chavez <steve(at)supabase(dot)io> wrote:
> Thanks Alexander!
>
> wrt to the new patch. I think the following comment is misleading since keyDeleted can be true or false:
>
> + /* switch equal_match to false so we only find greater matches now */
> + node = (IntRBTreeNode *) rbt_find_great(tree, (RBTNode *) &searchNode,
> + keyDeleted);
>
> Maybe it should be the same used for searching lesser keys:
>
> + /*
> + * Find the next key. If the current key is deleted, we can pass
> + * equal_match == true and still find the next one.
> + */

Thank you for catching this.
The revised version of patch is attached!

------
Regards,
Alexander Korotkov

Attachment Content-Type Size
0001-Use-C99-designator-in-the-rbtree-sentinel-definit-v3.patch application/octet-stream 908 bytes
0002-Add-missing-inequality-searches-to-rbtree-v3.patch application/octet-stream 7.3 KB

From: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
To: Steve Chavez <steve(at)supabase(dot)io>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Add red-black tree missing comparison searches
Date: 2022-07-08 19:01:24
Message-ID: CAPpHfdukKZFehZX8sGJCEzPxNdvDu7GQ8SFZV1zi0hLrhT8HaA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Jul 7, 2022 at 1:43 PM Alexander Korotkov <aekorotkov(at)gmail(dot)com> wrote:
> On Thu, Jul 7, 2022 at 2:16 AM Steve Chavez <steve(at)supabase(dot)io> wrote:
> > Thanks Alexander!
> >
> > wrt to the new patch. I think the following comment is misleading since keyDeleted can be true or false:
> >
> > + /* switch equal_match to false so we only find greater matches now */
> > + node = (IntRBTreeNode *) rbt_find_great(tree, (RBTNode *) &searchNode,
> > + keyDeleted);
> >
> > Maybe it should be the same used for searching lesser keys:
> >
> > + /*
> > + * Find the next key. If the current key is deleted, we can pass
> > + * equal_match == true and still find the next one.
> > + */
>
> Thank you for catching this.
> The revised version of patch is attached!

Pushed!

------
Regards,
Alexander Korotkov