Postgresql – Using SELECT to call a function in a jdbc migration

clojurejdbcpostgispostgresql

I'm looking to set up some Postgres/PostGIS migrations with clojure/jdbc.

running side-effect functions with SELECT is proving to be an issue, with most migration libs eventually throwing the A result was returned when none was expected error, because at some point they use clojure.java.jdbc/execute! or clojure.java.jdbc/db-do-commands, which seems understandable, but frustrating when you need to call a function that's critical to the migration.

The PostGIS's docs encourage using SELECT statement to create a spatial column:

SELECT AddGeometryColumn('parks', 'park_geom', 128, 'MULTIPOLYGON', 2 );

Has anyone run into this or found an appropriate workaround for using functions in a clojure/jdbc and Postgres migration?


related tidbits:

  • this description for manually registering a spatial column looks promising but seems remarkably heavy-handed for something that already has a supporting function
  • there's also PL/PgSQL's PERFORM statement that I stumbled across but it seems like I'm grasping at straws at that point, despite it looking promising
  • clojure.java.jdbc/execute! docs give a specific heads-up about only using "general (non-select) SQL operation[s]"

Best Answer

You can avoid this by using the DO command like so:

DO $$ 
BEGIN
    PERFORM AddGeometryColumn('parks', 'park_geom', 128, 'MULTIPOLYGON', 2 );
END;
$$

Note the BEGIN and END are necessary as it has to be a valid plpgsql block.