​function arguments are not PG_FUNCTION_ARGS, how to pass Node *escontext

Started by jian heover 2 years ago5 messages
#1jian he
jian.universality@gmail.com

hi. simple question....

The following one works.
------------------------------------------------------------
Datum
test_direct_inputcall(PG_FUNCTION_ARGS)
{
char *token = PG_GETARG_CSTRING(0);
Datum numd;
if (!DirectInputFunctionCallSafe(numeric_in, token,
InvalidOid, -1,
fcinfo->context,
&numd))
{
elog(INFO,"convert to cstring failed");
PG_RETURN_BOOL(false);
}
elog(INFO,"%s",DatumGetCString(DirectFunctionCall1(numeric_out,numd)));
PG_RETURN_BOOL(true);
}
------------------------------------------------------------
--the following one does not work. will print out something is wrong

Datum
test_direct_inputcall(PG_FUNCTION_ARGS)
{
char *token = PG_GETARG_CSTRING(0);
Datum numd;
numd = return_numeric_datum(token);
elog(INFO,"%s",DatumGetCString(DirectFunctionCall1(numeric_out,numd)));
PG_RETURN_BOOL(true);
}

static
Datum return_numeric_datum(char *token)
{
Datum numd;
Node *escontext;

if (!DirectInputFunctionCallSafe(numeric_in, token,
InvalidOid, -1,
escontext,
&numd));
elog(INFO,"something is wrong");
return numd;
}
------------------------------------------------------------
I wonder how to make it return_numeric_datum works in functions that
arguments are not PG_FUNCTION_ARGS.

To make it work, I need to understand the Node *context, which is kind
of a vague idea for me.
In the top level function (input as PG_FUNCTION_ARGS) the Node
*context can be used to print out errors and back to normal state, I
kind of get it.

I really appreciate someone showing a minimal, reproducible example
based on return_numeric_datum....

#2Andrew Dunstan
andrew@dunslane.net
In reply to: jian he (#1)
Re: ​function arguments are not PG_FUNCTION_ARGS, how to pass Node *escontext

On 2023-06-26 Mo 07:20, jian he wrote:

static
Datum return_numeric_datum(char *token)
{
Datum numd;
Node *escontext;

if (!DirectInputFunctionCallSafe(numeric_in, token,
InvalidOid, -1,
escontext,
&numd));
elog(INFO,"something is wrong");
return numd;
}

To start with, the semicolon at the end of that if appears bogus. The
elog is indented to look like it's conditioned by the if but the
semicolon makes it not be.

There are compiler switches in modern gcc at least that help you detect
things like this.

cheers

andrew

--
Andrew Dunstan
EDB:https://www.enterprisedb.com

#3jian he
jian.universality@gmail.com
In reply to: Andrew Dunstan (#2)
Re: ​function arguments are not PG_FUNCTION_ARGS, how to pass Node *escontext

On Mon, Jun 26, 2023 at 7:50 PM Andrew Dunstan <andrew@dunslane.net> wrote:

On 2023-06-26 Mo 07:20, jian he wrote:

static
Datum return_numeric_datum(char *token)
{
Datum numd;
Node *escontext;

if (!DirectInputFunctionCallSafe(numeric_in, token,
InvalidOid, -1,
escontext,
&numd));
elog(INFO,"something is wrong");
return numd;
}

To start with, the semicolon at the end of that if appears bogus. The elog is indented to look like it's conditioned by the if but the semicolon makes it not be.

There are compiler switches in modern gcc at least that help you detect things like this.

cheers

andrew

--
Andrew Dunstan
EDB: https://www.enterprisedb.com

sorry. It was my mistake.

Node *escontext;
if (!DirectInputFunctionCallSafe(numeric_in, token,
InvalidOid, -1,
escontext,
&numd))
elog(INFO,"something is wrong");

I wonder about the implication of just declaring Node *escontext in here.
In this DirectInputFunctionCallSafe, what does escontext point to.

#4jian he
jian.universality@gmail.com
In reply to: jian he (#3)
Re: ​function arguments are not PG_FUNCTION_ARGS, how to pass Node *escontext

On Mon, Jun 26, 2023 at 9:32 PM jian he <jian.universality@gmail.com> wrote:

On Mon, Jun 26, 2023 at 7:50 PM Andrew Dunstan <andrew@dunslane.net>

wrote:

On 2023-06-26 Mo 07:20, jian he wrote:

static
Datum return_numeric_datum(char *token)
{
Datum numd;
Node *escontext;

if (!DirectInputFunctionCallSafe(numeric_in, token,
InvalidOid, -1,
escontext,
&numd));
elog(INFO,"something is wrong");
return numd;
}

To start with, the semicolon at the end of that if appears bogus. The

elog is indented to look like it's conditioned by the if but the semicolon
makes it not be.

There are compiler switches in modern gcc at least that help you detect

things like this.

cheers

andrew

--
Andrew Dunstan
EDB: https://www.enterprisedb.com

sorry. It was my mistake.

Node *escontext;
if (!DirectInputFunctionCallSafe(numeric_in, token,
InvalidOid, -1,
escontext,
&numd))
elog(INFO,"something is wrong");

I wonder about the implication of just declaring Node *escontext in here.
In this DirectInputFunctionCallSafe, what does escontext point to.

gcc -Wempty-body will detect my error.

However, gcc -Wextra includes -Wempty-body and -Wuninitialized and others.
i guess in here, the real question in here is how to initialize escontext.

#5Andrew Dunstan
andrew@dunslane.net
In reply to: jian he (#4)
Re: ​function arguments are not PG_FUNCTION_ARGS, how to pass Node *escontext

On 2023-06-26 Mo 09:45, jian he wrote:

On Mon, Jun 26, 2023 at 9:32 PM jian he <jian.universality@gmail.com>
wrote:

On Mon, Jun 26, 2023 at 7:50 PM Andrew Dunstan <andrew@dunslane.net>

wrote:

On 2023-06-26 Mo 07:20, jian he wrote:

static
Datum return_numeric_datum(char *token)
{
    Datum   numd;
    Node    *escontext;

    if (!DirectInputFunctionCallSafe(numeric_in, token,
                                    InvalidOid, -1,
                                    escontext,
                                    &numd));
        elog(INFO,"something is wrong");
    return numd;
}

To start with, the semicolon at the end of that if appears bogus.

The elog is indented to look like it's conditioned by the if but the
semicolon makes it not be.

There are compiler switches in modern gcc at least that help you

detect things like this.

cheers

andrew

--
Andrew Dunstan
EDB: https://www.enterprisedb.com

sorry. It was my mistake.

    Node    *escontext;
    if (!DirectInputFunctionCallSafe(numeric_in, token,
                                    InvalidOid, -1,
                                    escontext,
                                    &numd))
        elog(INFO,"something is wrong");

I wonder about the implication of just declaring Node *escontext in

here.

In this DirectInputFunctionCallSafe, what does escontext point to.

gcc -Wempty-body will detect my error.

However, gcc -Wextra includes  -Wempty-body and  -Wuninitializedand
others.
i guess in here, the real question in here is how to initializeescontext.

See code for pg_input_error_info and friends for examples.

cheers

andrew

--
Andrew Dunstan
EDB:https://www.enterprisedb.com