Re: Practice advice for use of %type in declaring a subprogram's formal arguments

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Bryn Llewellyn <bryn(at)yugabyte(dot)com>
Cc: pgsql-general list <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Practice advice for use of %type in declaring a subprogram's formal arguments
Date: 2023-03-10 21:53:48
Message-ID: CAKFQuwbKsv7FR=AHDo1TcK23mcnJ09W9rM7_a6p6vGeiSMKfug@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Mar 10, 2023 at 2:28 PM Bryn Llewellyn <bryn(at)yugabyte(dot)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.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Runqi Tian 2023-03-10 22:41:44 Re: Support logical replication of DDLs
Previous Message Bryn Llewellyn 2023-03-10 21:28:21 Practice advice for use of %type in declaring a subprogram's formal arguments