pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Problems with using function input paramaters
4+ messages / 3 participants
[nested] [flat]

* Problems with using function input paramaters
@ 2019-08-31 14:05  stan <stanb@panix.com>
  0 siblings, 2 replies; 4+ messages in thread

From: stan @ 2019-08-31 14:05 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

I have been chasing a bug for a bit now. I even wound up completely rewriting the
function, which in the end turns out to be a good thing, as it is much cleaner and
easy to read. 

I now believe that the bug is in how I am using an input parameter to the
function. Here is the function deceleration:

CREATE FUNCTION
return_previous_month_start_and_end(
integer)
RETURNS interval_dates AS $$

Here is one of the places I am using it:


my_year :=      ( select
                 cast(extract(year from
							cast(date_trunc('month',
								CURRENT_DATE) - interval '  $1
								month - 1 ' day as date)
								) as integer)
						) ;
This prints the correct value BTW:

 RAISE notice 'Called with %', $1 ;

 Nay thoughts as to what I am doing wrong?


-- 
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
						-- Benjamin Franklin





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Problems with using function input paramaters
@ 2019-08-31 14:24  stan <stanb@panix.com>
  parent: stan <stanb@panix.com>
  1 sibling, 0 replies; 4+ messages in thread

From: stan @ 2019-08-31 14:24 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On Sat, Aug 31, 2019 at 10:05:10AM -0400, stan wrote:
> I have been chasing a bug for a bit now. I even wound up completely rewriting the
> function, which in the end turns out to be a good thing, as it is much cleaner and
> easy to read. 
> 
> I now believe that the bug is in how I am using an input parameter to the
> function. Here is the function deceleration:
> 
> CREATE FUNCTION
> return_previous_month_start_and_end(
> integer)
> RETURNS interval_dates AS $$
> 
> Here is one of the places I am using it:
> 
> 
> my_year :=      ( select
>                  cast(extract(year from
> 							cast(date_trunc('month',
> 								CURRENT_DATE) - interval '  $1
> 								month - 1 ' day as date)
> 								) as integer)
> 						) ;
> This prints the correct value BTW:
> 
>  RAISE notice 'Called with %', $1 ;
> 
>  Nay thoughts as to what I am doing wrong?
> 
BTW, this article
https://dba.stackexchange.com/questions/159424/how-to-use-function-parameters-in-dynamic-sql-with-ex...
seems to imply that I can use the $1 syntax in a string, but others refer to using
the COALESCE functionality, but I am uncertain exactly how to use this here.


-- 
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
						-- Benjamin Franklin





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Problems with using function input paramaters
@ 2019-08-31 14:45  David G. Johnston <david.g.johnston@gmail.com>
  parent: stan <stanb@panix.com>
  1 sibling, 1 reply; 4+ messages in thread

From: David G. Johnston @ 2019-08-31 14:45 UTC (permalink / raw)
  To: stan <stanb@panix.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Sat, Aug 31, 2019 at 7:05 AM stan <stanb@panix.com> wrote:

>
> CURRENT_DATE) - interval '  $1
>                                                                 month - 1
> ' day as date)
>

($1 || ' month')::interval  -- should work

Content within a literal (i.e., between single quotes) is always treated as
literal content.

David J.

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Problems with using function input paramaters
@ 2019-08-31 15:13  Tom Lane <tgl@sss.pgh.pa.us>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2019-08-31 15:13 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: stan <stanb@panix.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Sat, Aug 31, 2019 at 7:05 AM stan <stanb@panix.com> wrote:
>> CURRENT_DATE) - interval '  $1
>> month - 1
>> ' day as date)

> ($1 || ' month')::interval  -- should work

FWIW, I tend to prefer doing it with interval arithmetic, like this:

$1 * '1 month'::interval - '1 day'::interval

It's faster (not enormously so, but measurably) thanks to not having
to construct and then parse a text string.  To my mind it's easier
to reason about, too, and a bit safer.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2019-08-31 15:13 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-08-31 14:05 Problems with using function input paramaters stan <stanb@panix.com>
2019-08-31 14:24 ` stan <stanb@panix.com>
2019-08-31 14:45 ` David G. Johnston <david.g.johnston@gmail.com>
2019-08-31 15:13   ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox