Skip to content

Enhanced Client-Side Support for LTree Type (Similar to Microsoft.EntityFrameworkCore.SqlServer.HierarchyId) #3610

Closed as not planned

Description

@gimmickj

Hello Npgsql Team,

First, thank you for your excellent work on the Npgsql.EntityFrameworkCore.PostgreSQL provider!

I'm writing to propose the development of an enhanced library for the LTree type that provides full client-side operation capabilities, aiming to achieve feature parity and a similar developer experience with Microsoft.EntityFrameworkCore.SqlServer.HierarchyId.

Reasoning:
Currently, the LTree type is primarily a thin wrapper around string, with all logical operations executed on the database server. This requires developers to use LINQ queries for even simple operations, making client-side manipulation and inspection of hierarchical data cumbersome. Many operations that are natural in C# code throw exceptions.

An enhanced LTree type with client-side capabilities would significantly improve developer productivity and flexibility when working with hierarchical data in PostgreSQL. It would make the choice between PostgreSQL's ltree and SQL Server's HierarchyId less influenced by differences in client-side development experience.

Desired Enhancements:

Client-Side API: A rich set of methods (e.g., GetLevel(), IsDescendantOf(), GetAncestor(), GetDescendant(), GetReparentedValue()) that can execute entirely in memory without requiring database round-trips.5

LINQ Translation: Maintain and potentially extend the existing excellent LINQ translation for server-side queries for these new methods.

Extended Functionality: Incorporate more advanced hierarchical operations currently available in HierarchyId.5

Optional Package: This enhanced functionality could be provided in a separate optional NuGet package (e.g., Npgsql.EntityFrameworkCore.PostgreSQL.LTree) to keep the core provider lightweight.

Considerations:
I understand that this would increase maintenance overhead and require careful design to avoid naming conflicts and performance pitfalls. Clear documentation on the intended use cases (client-side vs. server-side operations) would be crucial.

Thank you for considering this proposal. I believe this would be a valuable addition for developers leveraging PostgreSQL's powerful ltree module.

Activity

  1. changed the title [-]Enhanced Client-Side Support for LTree Type (Similar to SQL Server's HierarchyId[/-] [+]Enhanced Client-Side Support for LTree Type (Similar to Microsoft.EntityFrameworkCore.SqlServer.HierarchyId)[/+] on Sep 2, 2025
  2. roji commented on Sep 2, 2025

    @roji
    Member

    Can you provide more context on why you think client-side operations on LTree is something that developers need?

    ltree is a PostgreSQL type, and the EFCore.PG support is intended to allow users to use that (server-side) support. It's definitely not the intent here to provide a general .NET hierarchical data API that could be used without the database - that's out of scope for an EF provider/database driver. As you noted, this would add a non-trivial maintenance burden on the provider which I don't think makes sense.

    There are various other such examples, where a database-side capability is merely exposed, without any attempt to provide client-side implementations. For example, it obviously makes no sense to attempt to provide a client-side implementation of full-text search; ltree is slightly different but I believe the general principle still applies.

    BTW in any case, if we were to do something like this, it would belong at the lower-level Npgsql layer rather than in the EF provider layer.

  3. gimmickj commented on Sep 2, 2025

    @gimmickj
    Author

    You're absolutely right. I agree that keeping the focus on the database-side implementation is the correct approach for the EF provider. Thanks for explaining the scope and maintenance considerations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions