Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare software version in postgres

Is there a way to compare software version (e.g. X.Y.Z > A.B.C) in postgres ? I'm searching for a function on string/varchar or a "version" type.

I found out that http://pgxn.org/dist/semver/doc/semver.html, but i'm looking for alternatives (not so easy to deploy..)

Thanks a lot.

like image 638
131 Avatar asked Jun 14 '14 19:06

131


People also ask

Which PostgreSQL version is best?

SaltStack Config requires a PostgreSQL 9.6 database, but PostgreSQL 12.4 is recommended. The recommended version of PostgreSQL is included with the SaltStack Config installer. PostgreSQL is a third-party open source database that is required for SaltStack Config.


1 Answers

Use the cheaper string_to_array(). There is no need for expensive regular expressions here:

SELECT string_to_array(v1, '.')::int[] AS v1
     , string_to_array(v2, '.')::int[] AS v2
     ,(string_to_array(v1, '.')::int[] > string_to_array(v2, '.')::int[]) AS cmp
FROM   versions;

db<>fiddle here
Old sqlfiddle

like image 99
Erwin Brandstetter Avatar answered Sep 18 '22 19:09

Erwin Brandstetter