How can I interpolate psql variables in function bodies? - Mailing list pgsql-general

From J. Greg Davidson
Subject How can I interpolate psql variables in function bodies?
Date
Msg-id 1245104934.15935.32.camel@shevek.puuhonua.org
Whole thread Raw
Responses Re: How can I interpolate psql variables in function bodies?
Re: How can I interpolate psql variables in function bodies? - workaround
List pgsql-general
Hi dear colleagues,

I'm trying to pull some platform-specific constants out of my
code by using psql variables, e.g.:

$ psql -v TypeLength=4

# CREATE TYPE tref (
  INTERNALLENGTH = :TRefTypeLength,
  INPUT = tref_in,
  OUTPUT = tref_out,
  PASSEDBYVALUE
);

which works fine, but when I need such a constant in a function
it is not substituted.  A simplified example:

$ psql -v foo=10

# select :foo;
 ?column?
----------
       10
(1 row)

# create function foo() returns integer as 'select '(:foo) language sql;
ERROR:  syntax error at or near "(" at character 51

I'm sure that I could do something horrible by using EXECUTE inside of
a plpgsql function, and I'm hoping that someone will have a simpler
alternative.  For example, is there some kind of quoting mechanism I can
use which will not impede psql from doing substitutions?

Thanks,

_Greg


pgsql-general by date:

Previous
From: Glyn Astill
Date:
Subject: Re: DB Migration 8.4 -> 8.3
Next
From: David Kerr
Date:
Subject: Re: Amazon EC2 | Any recent developments