Re: [HACKERS] PL/pgSQL and SPI - Mailing list pgsql-hackers

From jwieck@debis.com (Jan Wieck)
Subject Re: [HACKERS] PL/pgSQL and SPI
Date
Msg-id m105qbG-000EBQC@orion.SAPserv.Hamburg.dsh.de
Whole thread Raw
In response to Re: [HACKERS] PL/pgSQL and SPI  (Vadim Mikheev <vadim@krs.ru>)
List pgsql-hackers
Vadim wrote:
>
> Jan Wieck wrote:
> >
> >     1.  I've  just committed some changes to PL/pgSQL and the SPI
> >         manager.
> >
> >         It's  a  speedup  of  PL/pgSQL   execution   by   calling
> >         ExecEvalExpr()   in  the  executor  directly  for  simple
> >         expressions that return one single Datum.
>
> BTW, Jan, did you consider ability to add new function
> for fast expression evaluation to SPI itself and than just
> use this func in PL/pgSQL?
> This function seems to be generally usefull.
> And we could avoid SPI_push/SPI_pop...

    Clarification:

        I'm  doing many tests on the SPI generated plan to ensure
        that it is so simple that ExecEvalExpr()  cannot  stumble
        over it. In detail it must be something that has only one
        targetentry, absolutely no qual, lefttree,  righttree  or
        something  else.  And all the nodes in the TLE expression
        must only be Expr (OP, FUNC, OR, AND, NOT only), Const or
        Param ones.

        This  is  required, because I have to fake an ExprContext
        that contains the values for  the  parameters  only.  The
        above  ensures,  that  ExecEvalExpr()  will  never  touch
        anything else than the ecxt_param_list_info and thus will
        not notice that it is a faked one.

    Well,  but  you're  right, I could add some smartness to SPI.
    First, it could do the same checks on the generated plan that
    ensure it really returns 1 (and only ever 1) Datum based only
    on function calls, constants or parameters. If  this  is  the
    case,  it  could  internally  call ExecEvalExpr() and build a
    faked heap tuple on  SPI_execp().  Someone  using  SPI_exec()
    isn't interested in speed, so I would leave it out there.

    And two new functions

        bool SPI_is_simple_expr(void *plan);
        Datum SPI_eval_simple_expr(void *plan,
                                   Datum *values,
                                   char *Nulls,
                                   bool *isNull,
                                   Oid *rettype);

    could gain more direct access to such expressions suppressing
    the need to diddle with the SPI tuple table for getting  just
    one Datum.

    Yes,  I think it would be a good enhancement. I'll go for it.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #

pgsql-hackers by date:

Previous
From: jwieck@debis.com (Jan Wieck)
Date:
Subject: PL/pgSQL mixed case support
Next
From: The Hermit Hacker
Date:
Subject: Re: [HACKERS] Death!