Re: Practice advice for use of %type in declaring a subprogram's formal arguments - Mailing list pgsql-general

From David G. Johnston
Subject Re: Practice advice for use of %type in declaring a subprogram's formal arguments
Date
Msg-id CAKFQuwbKsv7FR=AHDo1TcK23mcnJ09W9rM7_a6p6vGeiSMKfug@mail.gmail.com
Whole thread Raw
In response to Practice advice for use of %type in declaring a subprogram's formal arguments  (Bryn Llewellyn <bryn@yugabyte.com>)
Responses Re: Practice advice for use of %type in declaring a subprogram's formal arguments
List pgsql-general
On Fri, Mar 10, 2023 at 2:28 PM Bryn Llewellyn <bryn@yugabyte.com> wrote:
Do you (all) think that, notwithstanding this, it's a good plan to use "%type" for the declaration of a formal argument just for the reason that it documents your intention explicitly?

If my function is likely to be table-specific I would define its signature to be:

function(i_var tablename)

i.e., they accept a row of said table as the input.

As most functions don't, and likely shouldn't, care from what source their arguments come from, but instead perform an operation based upon the data types of the inputs, using %type is not something I've ever really had occasion to use.

Given that the source of record loses that metadata anyway that would be further reason why I would just pretend the %type syntax doesn't exist.  I suspect that on rare occasion its use could be beneficial, and maybe if I remember it exists while writing one of those use cases up I'll use it, but there doesn't seem to be much downside to just using concrete types everywhere.  Code comments can handle intent well enough here, as opposed to some, frankly, esoteric feature/syntax (which seems not all that SQL-ish but I don't see where it is specifically noted as our extension).

You comments do seem, at first glance at least, to refute the statement in the documentation:

> The type of a column is referenced by writing table_name.column_name%TYPE. Using this feature can sometimes help make a function independent of changes to the definition of a table.

I mean, while it is indeed "independent of changes to the definition of a table" so does simply writing "text"...the %type syntax seems like it should follow the changes of the definition of a table...

David J.

pgsql-general by date:

Previous
From: Bryn Llewellyn
Date:
Subject: Practice advice for use of %type in declaring a subprogram's formal arguments
Next
From: Ron
Date:
Subject: Re: Practice advice for use of %type in declaring a subprogram's formal arguments