Thread

  1. Re: COMMUTATOR doesn't seem to work

    D'Arcy Cain <darcy@druid.net> — 2012-06-22T16:28:19Z

    On 12-06-22 11:36 AM, Tom Lane wrote:
    > "D'Arcy Cain"<darcy@druid.net>  writes:
    > The thing is that either of those approaches is hugely more expensive
    > than just providing a second C function.  It costs probably thousands
    > of cycles to inline that SQL function, each time it's used in a query.
    
    I assumed itwould be more expensive but didn't know it would be that
    much more.
    
    > I doubt that an "auto reverse the arguments" facility would be very
    > much cheaper.  You could maybe argue that the aggregated maintenance
    > and space costs of all the commutator-pair functions are enough to
    > justify having some such solution instead, but I'm doubtful --- and
    > even if true, getting from here to there would be painful.
    
    And it would only apply to a very specific type of function.
    
    The other idea I had was to just have the second C function call the
    first but that didn't work.  Here is what I tried.
    
    PG_FUNCTION_INFO_V1(chkpass_eq);
    Datum
    chkpass_eq(PG_FUNCTION_ARGS)
    {
         chkpass    *a1 = (chkpass *) PG_GETARG_POINTER(0);
         text       *a2 = (text *) PG_GETARG_TEXT_P(1);
         char        str[9];
    
         strlcpy(str, a2->vl_dat, sizeof(str));
         PG_RETURN_BOOL(strcmp(a1->password, crypt(str, a1->password)) == 0);
    }
    
    PG_FUNCTION_INFO_V1(chkpass_eq2);
    Datum
    chkpass_eq2(PG_FUNCTION_ARGS)
    {
         return chkpass_eq(PG_GETARG_POINTER(1), PG_GETARG_TEXT_P(0));
    }
    
    Now in this specific case the function is trivial and writing it twice
    is no big deal but in general I hate writing the same code twice.  I
    suppose I could extract the actual operation out to a third function
    and call it from the others.  I may do that anyway just for the value
    of the example.  Or is there a way to do what I tried above?
    
    -- 
    D'Arcy J.M. Cain <darcy@druid.net>         |  Democracy is three wolves
    http://www.druid.net/darcy/                |  and a sheep voting on
    +1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.
    IM: darcy@Vex.Net