Re: plpython3

From: James William Pye <lists(at)jwp(dot)name>
To: Greg Smith <greg(at)2ndquadrant(dot)com>
Cc: Peter Eisentraut <peter_e(at)gmx(dot)net>, jd(at)commandprompt(dot)com, Josh Berkus <josh(at)agliodbs(dot)com>, Robert Haas <robertmhaas(at)gmail(dot)com>, PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: plpython3
Date: 2010-01-23 20:28:18
Message-ID: DC4B46E2-4643-4558-800C-2466F7CB34B3@jwp.name
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Jan 14, 2010, at 7:08 PM, Greg Smith wrote:
> So more targeted examples like you're considering now would help.

Here's the trigger example which should help reveal some of the advantages of "native typing". This is a generic trigger that constructs and logs manipulation statements for simple replication purposes.

The original plpython version is located here:

http://ar.pycon.org/common/2009/talkdata/PyCon2009/020/plpython.txt
[You'll need to scroll down to the very bottom of that page.]

There are three points in this example that need to be highlighted:

1. There is no need for a "mogrify" function (see original in the above link).
2. Attributes/columns of the records (new/old) are extracted when referenced.
3. The comparisons in after_update uses the data type's actual inequality operator.

The first point is true because "native typing" gives the user direct access to a given type's typoutput via ``str(ob)``. This makes constructing the PG string representation of a given object *much* easier--quote_nullable, and done. The original plpython example will need to be updated to compensate for any changes in conversion: arrays will now need special handling and MD arrays will not work at all. It also relies heavily on the Python object representation matching PG's; where that fails, special cases need to be implemented(composites, notably). All of that compensation performed in the original version is unnecessary in the plpython3 version.

The second point touches on the "efficiency" that was referenced in an earlier message. No cycles are spent converting the contents of a container object unless the user chooses to. Naturally, there is no advantage performance-wise if you are always converting everything.
I'd wager that with triggers, it's rare that everything needs to be converted.

The third point reveals that Postgres.Object instances--a component of native typing--use the data type's operator for inequality. It's not limited to comparisons as all available Python operators are mapped to corresponding operators in PG. For many or all primitives, there is no added value over conversion. However, this provides a lot of convenience when working with UDTs, datetime types, and geometric types.

...ISTM that the primary advantage of "native typing" is that we get to define the Python interface to a given Postgres data type.

Three files are attached:

afterlog.py - the trigger returning function
afterlog.sql - the sql exercising the TRF (creates the replica_log table as well)
afterlog.out - the contents of the replica_log table after executing afterlog.sql

To replay:

\i afterlog.py
\i afterlog.sql
SELECT * FROM replica_log;

Attachment Content-Type Size
afterlog.py text/x-python-script 1.5 KB
afterlog.sql application/octet-stream 1.2 KB
afterlog.out application/octet-stream 810 bytes

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Simon Riggs 2010-01-23 20:39:53 Re: pgsql: In HS, Startup process sets SIGALRM when waiting for buffer pin.
Previous Message Simon Riggs 2010-01-23 20:28:00 Re: pgsql: In HS, Startup process sets SIGALRM when waiting for buffer pin.