PL/PGSQL: Dynamic Record Introspection

From: Titus von Boxberg <ut(at)bhi-hamburg(dot)de>
To: pgsql-patches(at)postgresql(dot)org
Subject: PL/PGSQL: Dynamic Record Introspection
Date: 2005-07-14 00:32:23
Message-ID: 42D5B297.7090804@bhi-hamburg.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-patches

Hi all,

I needed introspection capabilities for record types to write more generic
trigger procedures in PL/PGSQL.

With the following patch it's possible to
- extract all field names of a record into an array
- extract field count of a record
- address a single field of a record with a variable
containing the field name (additional to the usual record.fieldname
notation where the fieldname is hardcoded).

The syntax is
- record%NFIELDS gives the number of fields in the record
- record%FIELDNAMES gives the array of the field names
- record%scalarvariable extracts the field whose name
is equal to the contents of scalarvariable

------------

The patch is nonintrusive in the sense that it only adds things
with one exception:
In function exec_eval_datum(), file pl_exec.c, line 3557 (after the patch)
I chose to convert the record field values to TEXT if the caller does
not require a certain type (expectedtypeid == InvalidOid).
Additionally, I cast the value from the record field type if
the destination Datum is of different type.

As far as I can see, this does no harm because in most cases
the expectedtypeid is correctly set. I just wanted to avoid that
if it is not set, the returned datum is of a more restrictive type
than TEXT.

------------

The patch is against a HEAD checkout from 07/12/05
The output comes from difforig.

Test code for the patch can be extracted from an example I put into
plpgsql.sgml

------------

Here is a summary of things that get patched by the file:
- add three new special parsing functions to pl_comp.c
(plpgsql_parse_wordpercentword, plpgsql_parse_wordnfields,
plpgsql_parse_wordfieldnames).
- modify PLpgSQL_recfield in plpgsql.h to either hold
a conventional field name (record.fieldname) or a dno
for the variable (record%variable).
- add two PLPGSQL_DTYPEs for the two new % notations
- modify "case PLPGSQL_DTYPE_RECFIELD:"
in exec_eval_datum() and exec_assign_value()
to deal with index strings from a variable
- add "case PLPGSQL_DTYPE_RECFIELDNAMES"
and "case PLPGSQL_DTYPE_NRECFIELD"
to exec_eval_datum() to evaluate %FIELDNAMES
and %NFIELDS expressions.
- update plpgsql.sgml in the docs directory

------------

Please notify me if I can be of further assistance.

Regards
Titus

Attachment Content-Type Size
assocrec-current.patch text/plain 20.7 KB

Responses

Browse pgsql-patches by date

  From Date Subject
Next Message Jeffrey W. Baker 2005-07-14 00:33:45 Re: [PATCHES] O_DIRECT for WAL writes
Previous Message Andrew Dunstan 2005-07-13 19:46:23 Re: [PATCHES] HEAD doesn't cope with libraries in non-default