In Postgres 15, how can we define constants that pertain to business logic rather than db configuration settings? I'd like to define them within SQL scripts rather than passed through the psql -v MY_CONSTANT=123 command & I don't care if they persist only for the session.
In prior versions I quietly got away with the following syntax:
SET MY_CONSTANT TO 123; -- declared
CALL my_func(:MY_CONSTANT); -- used
Which from the documentation seems to be intended for modifying known db config settings & explains why I am encountering issues like this:
my_script.sql:2: ERROR: unrecognized configuration parameter "my_constant"
In my postgis scenario I am trying to set the spatial-reference identifier as a constant integer for all
– eliangius Dec 23 '22 at 21:23Geometry(Point, :SRID)columns in my schema. Defining as\set SRID 3347for psql interpolation should have worked, but gives syntax errors when the constant is referenced with & without quotes as:"SRID". Any ideas?Geometry(Point, :SRID)does not look like a valid PostGis function call. – Erwin Brandstetter Dec 23 '22 at 22:06\set MY_CONSTANT 123 ; -- some commentdoes NOT work as intended because the whole line is treated as its value(s). – eliangius Dec 23 '22 at 22:35current_setting('custom.MY_CONSTANT')::INTEGERlove to know of it as that complexity didn't seem to work for type modifiers like postgis'Geometry()wanting simple constants. – eliangius Dec 23 '22 at 23:00DECLAREsection from a "customized option" into such a variable once ... Ask a new question if you need more. Comments are not the place ... – Erwin Brandstetter Dec 23 '22 at 23:07