Skip to content
Advertisement

How do I access a field of nested user defined types?

How do I access the fields of user defined types when they are nested ? When I tried it using dot notation it failed:

For example, how do I “compile” this code ?

Trying to run it:

Advertisement

Answer

Looks like a PL/PgSQL deficiency. In normal SQL you can do so with

but pl/pgsql doesn’t accept the same form:

so I think it’s a bug/oversight/limitation. Consider raising it on pgsql-bugs.

You can access it by unpacking it into a temp var, modifying it, and storing it again, but it’s hideously inefficient.

That said: I don’t suggest using plpgsql to do this kind of pseudo-OO stuff. SQL is not OO and doesn’t play well with nested user defined composite types. It’s very inefficient to modify things – most things in SQL are immutable so new copies are made when you modify values. Iterative change and procedural code works appallingly poorly.

You should seek to work with sets and relations. Construct new values in one pass, don’t iteratively modify them and set fields one by one. Use relationships rather than having one object contain another.

Also, please drop the hungarian notation. Ugh.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement