Skip to content

Add CASCADE when installing a PG extensions #3682

Description

@mguinness

For PostgreSQL extensions there is a CASCADE parameter for including dependencies as noted in CREATE EXTENSION. Would it be desirable to include an overload on HasPostgresExtension to signify installing dependencies?

Activity

  1. roji commented on Dec 7, 2025

    @roji
    Member

    Have you a specific instance where you need this? We could also add this always if we want to (not that HasPostgresExtension already adds IF NOT EXISTS, so it does not directly correspond to PG CREATE EXTENSIONS.

  2. mguinness commented on Dec 7, 2025

    @mguinness
    ContributorAuthor

    The earthdistance extension relies on the cube extension, so the following is necessitated. Not a deal breaker, but Just thought it might be a nice addition.

    CREATE EXTENSION cube;
    CREATE EXTENSION earthdistance;
    
  3. roji commented on Dec 8, 2025

    @roji
    Member

    Sure, feel free to submit a PR. I'd simply always add the CASCADE rather than adding another parameter - at least I don't see any downside for doing so.

  4. added this to the Backlog milestone on Dec 8, 2025
  5. added theissue type on Dec 8, 2025
  6. mguinness commented on Dec 9, 2025

    @mguinness
    ContributorAuthor

    On a related note, looking at how best to translate the earthdistance functions:

    Cube-Based Earth Distances

    For the SQL SELECT earth_distance( ll_to_earth(point1_lat, point1_lon), ll_to_earth(point2_lat, point2_lon) );

    I was able to get it working using the code below

    .Where(x =>
      ctx.EarthDistance(
        ctx.LatLngToEarth(x.Latitude, x.Longitude),
        ctx.LatLngToEarth(loc.Latitude, loc.Longitude)
      ) < loc.Distance
    )

    With the functions defined in the DbContext

    //earth is a constrained version of cube
    public NpgsqlCube LatLngToEarth(double latitude, double longitude) => throw new NotSupportedException();
    
    public double EarthDistance(NpgsqlCube location1, NpgsqlCube location2) => throw new NotSupportedException();
    
    modelBuilder.HasDbFunction(typeof(MyContext).GetMethod(nameof(LatLngToEarth), [typeof(double), typeof(double)])!)
      .HasName("ll_to_earth");
    
    modelBuilder.HasDbFunction(typeof(MyContext).GetMethod(nameof(EarthDistance), [typeof(NpgsqlCube), typeof(NpgsqlCube)])!)
      .HasName("earth_distance");

    Point-Based Earth Distances

    For the SQL SELECT point(point1_lat, point1_lon) <@> point(point2_lat, point2_lon);

    I would like to get it working using Mapping a method to a custom SQL. I could create a new NpgsqlPoint(x, y) but I would be stuck on the <@> operator.

    I see you have something similar for the cube distance function. Any suggestions on defining this new operator, or would this necessitate changes to the provider?

  7. roji commented on Dec 9, 2025

    @roji
    Member

    For more complicated SQL translations, you should be able to get by with custom SQL translation as explained in this section of the docs). For the actual expression representing <@>, you should be able to use PgUnknownBinaryExpression.

    Though I'd consider packaging all the earthdistance in a clean plugin, which would contain an IMethodTranslator, and which you'd enable via a single EnableEarthDistance() in the DbContext options; take a look at the NodaTime/NetTopologySuite plugins to see how that looks (it's really simple, there's just a bit of boilerplate that's needed). At that point you can even publish the plugin as a nuget package for others to use.

  8. modified the milestones: Backlog, 11.0.0 on Dec 9, 2025
  9. mguinness commented on Dec 10, 2025

    @mguinness
    ContributorAuthor

    Thanks PgUnknownBinaryExpression was the missing piece to get it working.

    public double PointDistance(NpgsqlPoint location1, NpgsqlPoint location2) => throw new NotSupportedException();
    
    modelBuilder.HasDbFunction(typeof(MyContext).GetMethod(nameof(PointDistance), [typeof(NpgsqlPoint), typeof(NpgsqlPoint)])!)
        .HasTranslation(args => new PgUnknownBinaryExpression(
            args[0], args[1], "<@>", typeof(double), new DoubleTypeMapping("double precision", DbType.Double)
        ));
  10. mguinness commented on Dec 10, 2025

    @mguinness
    ContributorAuthor

    Is there a reason NpgsqlPoint doesn't have a translation to point? It's trivial to add, but it would be good to include in provider.

    public NpgsqlPoint LngLatToPoint(double longitude, double latitude) => throw new NotSupportedException();
    
    modelBuilder.HasDbFunction(typeof(MyContext).GetMethod(nameof(LngLatToPoint), [typeof(double), typeof(double)])!)
        .HasName("point");
  11. roji commented on Dec 10, 2025

    @roji
    Member

    There's generally little translation for the built-in geometry types (point, linestring) since they're pretty rarely-used (compared to PostGIS).

    Specifically for constructing a point, I'd add a constructor translation (so for new Point(x, y)) - that probably makes sense for all the geometry types. But if you're interested in these types, a more general pass for missing translation also makes sense - I'm sure there's various other stuff beyond construction that could be done.

  12. changed the title [-]Dependencies option for HasPostgresExtension[/-] [+]Add CASCADE when installing a PG extensions[/+] on Dec 10, 2025
  13. added a commit that references this issue on Dec 10, 2025
    7867fd8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions