Обсуждение: Understanding Datum

Поиск
Список
Период
Сортировка

Understanding Datum

От
Nick Raj
Дата:
Hi,
I am understanding the postgres code. In code, i just want to see what are values that are passing through the variables?
Can you please tell me if the variable is of type Datum, then how to print its value? Because i dont the variable v type.

And also what the structure of Datum?

Thanks,
Raj

Re: Understanding Datum

От
Nick Raj
Дата:
Hi,
In postgres, typedef uintptr_t Datum
Datum is getting value from PG_GETARG_POINTER(1);
But, now problem is how would i know the type of PG_GETARG_POINTER(1) (postgres internally pass this argument) to figure out datum type?

Can you tell detailed structure of Datum, so i can print the value?? How to find out what type of pointer argument is PG_GETARG_POINTER(1)??

Thanks,
Nirmesh

On Wed, Mar 23, 2011 at 11:40 PM, Radosław Smogura <mail@smogura.eu> wrote:
Nick Raj <nickrajjain@gmail.com> Wednesday 23 March 2011 18:45:41
> Hi,
> I am understanding the postgres code. In code, i just want to see what are
> values that are passing through the variables?
> Can you please tell me if the variable is of type Datum, then how to print
> its value? Because i dont the variable v type.
>
> And also what the structure of Datum?
>
> Thanks,
> Raj

The structure is explained in one of headers, generally Datum is pointer. It
points to memory containing at first four bytes integer describing size of
data in datum (use macro to extract this), and then, it's followed by bytes
containing data.

Actually almost each object is represented by structure like this
struct something {
       int4 size; //Required
//      Here put what you want
}
see headers.

If you want to present data from datum you need to 1) check what type of data
datum has (datum doesn't contain this) 2) Find datum representation for this
type.

Regards,
Radek

Re: Understanding Datum

От
Tom Lane
Дата:
Nick Raj <nickrajjain@gmail.com> writes:
> In postgres, typedef uintptr_t Datum
> Datum is getting value from PG_GETARG_POINTER(1);
> But, now problem is how would i know the type of PG_GETARG_POINTER(1)
> (postgres internally pass this argument) to figure out datum type?

Datum does not carry any type information, only a value.  Functions are
typically coded to know their input types a priori.  If you want to
write code that is not type-specific then you'd better be passing around
type OIDs as well as values.

            regards, tom lane

Re: Understanding Datum

От
Radosław Smogura
Дата:
Nick Raj <nickrajjain@gmail.com> Wednesday 23 March 2011 18:45:41
> Hi,
> I am understanding the postgres code. In code, i just want to see what are
> values that are passing through the variables?
> Can you please tell me if the variable is of type Datum, then how to print
> its value? Because i dont the variable v type.
>
> And also what the structure of Datum?
>
> Thanks,
> Raj

The structure is explained in one of headers, generally Datum is pointer. It
points to memory containing at first four bytes integer describing size of
data in datum (use macro to extract this), and then, it's followed by bytes
containing data.

Actually almost each object is represented by structure like this
struct something {
    int4 size; //Required
//    Here put what you want
}
see headers.

If you want to present data from datum you need to 1) check what type of data
datum has (datum doesn't contain this) 2) Find datum representation for this
type.

Regards,
Radek

Re: Understanding Datum

От
Nick Raj
Дата:
If Datum contains only the value (not having type specific info.), then
Suppose i want to print the Datum V value (already defined in postgres)
then printf("%??", V);

Because V is assigned by PG_GETARG_POINTER(1);
I don't having the information of type Datum.

How to print the value of Datum in postgres?


On Thu, Mar 24, 2011 at 2:35 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Nick Raj <nickrajjain@gmail.com> writes:
> In postgres, typedef uintptr_t Datum
> Datum is getting value from PG_GETARG_POINTER(1);
> But, now problem is how would i know the type of PG_GETARG_POINTER(1)
> (postgres internally pass this argument) to figure out datum type?

Datum does not carry any type information, only a value.  Functions are
typically coded to know their input types a priori.  If you want to
write code that is not type-specific then you'd better be passing around
type OIDs as well as values.

                       regards, tom lane

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Re: Understanding Datum

От
Pavel Stehule
Дата:
2011/3/24 Nick Raj <nickrajjain@gmail.com>:
> If Datum contains only the value (not having type specific info.), then
> Suppose i want to print the Datum V value (already defined in postgres)
> then printf("%??", V);
>
> Because V is assigned by PG_GETARG_POINTER(1);
> I don't having the information of type Datum.
>
> How to print the value of Datum in postgres?

you have to find a adequate out function and you have to call it. A
out functions transform a binary Datum value to CString value.

#include "executor/spi.h"               /* this is what you need to
work with SPI */
#include "utils/lsyscache.h"

PG_MODULE_MAGIC;

extern Datum quote_literal(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(quote_literal);

Datum
quote_literal(PG_FUNCTION_ARGS)
{
        text *result;
        Oid         valtype = get_fn_expr_argtype(fcinfo->flinfo, 0);
        Datum       value = PG_GETARG_DATUM(0);
        Oid                     typoutput;
        bool            typIsVarlena;
        StringInfoData buf;

        if (!OidIsValid(valtype))
                elog(ERROR, "could not determine data type of input");

        initStringInfo(&buf);

        appendStringInfoChar(&buf, '\'');

        getTypeOutputInfo(valtype, &typoutput, &typIsVarlena);
        appendStringInfoString(&buf, OidOutputFunctionCall(typoutput, value));

        appendStringInfoChar(&buf, '\'');

        result = DatumGetTextP(DirectFunctionCall1(textin,
CStringGetDatum(buf.data)));

        PG_RETURN_TEXT_P(result);
}

CREATE OR REPLACE FUNCTION quote_literal(anyelement)
RETURNS text
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;

CREATE OR REPLACE FUNCTION quote_literal(text)
RETURNS text
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;

Regards

Pavel Stehule

p.s. this is simple variant of this function. For production usage is
using cache necessary.

>
>
> On Thu, Mar 24, 2011 at 2:35 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>>
>> Nick Raj <nickrajjain@gmail.com> writes:
>> > In postgres, typedef uintptr_t Datum
>> > Datum is getting value from PG_GETARG_POINTER(1);
>> > But, now problem is how would i know the type of PG_GETARG_POINTER(1)
>> > (postgres internally pass this argument) to figure out datum type?
>>
>> Datum does not carry any type information, only a value.  Functions are
>> typically coded to know their input types a priori.  If you want to
>> write code that is not type-specific then you'd better be passing around
>> type OIDs as well as values.
>>
>>                        regards, tom lane
>>
>> --
>> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-general
>
>

Re: Understanding Datum

От
rsmogura
Дата:
 On Thu, 24 Mar 2011 10:50:32 +0530, Nick Raj wrote:
> If Datum contains only the value (not having type specific info.),
> then
> Suppose i want to print the Datum V value (already defined in
> postgres)
> then printf("%??", V);
>
> Because V is assigned by PG_GETARG_POINTER(1);
>  I dont having the information of type Datum.
>
> How to print the value of Datum in postgres?
>
> On Thu, Mar 24, 2011 at 2:35 AM, Tom Lane  wrote:
>
>> Nick Raj writes:
>> > In postgres, typedef uintptr_t Datum
>> > Datum is getting value from PG_GETARG_POINTER(1);
>> > But, now problem is how would i know the type of
>> PG_GETARG_POINTER(1)
>> > (postgres internally pass this argument) to figure out datum
>> type?
>>
>> Datum does not carry any type information, only a value.
>>  Functions are
>> typically coded to know their input types a priori.  If you want
>> to
>> write code that is not type-specific then youd better be passing
>> around
>> type OIDs as well as values.
>>
>>                        regards, tom lane
>>
>> --
>> Sent via pgsql-general mailing list (pgsql-general@postgresql.org
>> [2])
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-general [3]
>
>
>
> Links:
> ------
> [1] mailto:nickrajjain@gmail.com
> [2] mailto:pgsql-general@postgresql.org
> [3] http://www.postgresql.org/mailpref/pgsql-general
> [4] mailto:tgl@sss.pgh.pa.us

 You may only find this from code context. Bear in mind, in simple
 words, datum is like void* with additional size header.

 If You write C function you now what kind of datum will income, and
 what should outcome (You declare this!). If you examine table, it's same
 situation, but You need ask system for this. Even if PG will have type
 bytes in Datum, PG supports custom types...

 If you wan't to operate on datums You can't work in a way take some
 datum and work on it.

 You need to obtain /context/, by getting what type tables has on column
 x, what type of results function will return, etc; You need to know from
 where Your datum income.