pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Implement a new data type
11+ messages / 8 participants
[nested] [flat]

* Implement a new data type
@ 2020-08-11 11:31  mohand oubelkacem makhoukhene <mohand-oubelkacem@outlook.com>
  0 siblings, 3 replies; 11+ messages in thread

From: mohand oubelkacem makhoukhene @ 2020-08-11 11:31 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Hello;
I whould like to implement a new data type next to char, number, varchar... A Money type.
So i'll have to change the source code, but i don't know which fonctions i need to change and which part to work on.
I need some help to implement this new data type.
Thank you and best regards
Mohand

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Implement a new data type
@ 2020-08-11 12:00  Laurenz Albe <laurenz.albe@cybertec.at>
  parent: mohand oubelkacem makhoukhene <mohand-oubelkacem@outlook.com>
  2 siblings, 0 replies; 11+ messages in thread

From: Laurenz Albe @ 2020-08-11 12:00 UTC (permalink / raw)
  To: mohand oubelkacem makhoukhene <mohand-oubelkacem@outlook.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On Tue, 2020-08-11 at 11:31 +0000, mohand oubelkacem makhoukhene wrote:
> I whould like to implement a new data type next to char, number, varchar... A Money type. 
> So i'll have to change the source code, but i don't know which fonctions i need to change and which part to work on.

You don't need to change the source, you could create an extension
using a shared library that can be loaded into the server at run time.

The documentation has more:
https://www.postgresql.org/docs/current/xfunc-c.html#XFUNC-C-BASETYPE
https://www.postgresql.org/docs/current/extend-extensions.html
https://www.postgresql.org/docs/current/extend-pgxs.html

Yours,
Laurenz Albe






^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Implement a new data type
@ 2020-08-11 12:01  Pavel Stehule <pavel.stehule@gmail.com>
  parent: mohand oubelkacem makhoukhene <mohand-oubelkacem@outlook.com>
  2 siblings, 1 reply; 11+ messages in thread

From: Pavel Stehule @ 2020-08-11 12:01 UTC (permalink / raw)
  To: mohand oubelkacem makhoukhene <mohand-oubelkacem@outlook.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Hi

út 11. 8. 2020 v 13:31 odesílatel mohand oubelkacem makhoukhene <
mohand-oubelkacem@outlook.com> napsal:

> Hello;
> I whould like to implement a new data type next to char, number,
> varchar... A Money type.
> So i'll have to change the source code, but i don't know which fonctions i
> need to change and which part to work on.
> I need some help to implement this new data type.
>

If you need to implement just a new data type, then you don't need to
modify Postgres source code. You can write your own extension.

For own custom type you need to write minimally "in" and "out" function,
and then you can run "CREATE TYPE" statement

one example of custom type can be a extension
https://github.com/okbob/pgDecimal

Regards

Pavel


Thank you and best regards
> Mohand
>
>
>
>

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Implement a new data type
@ 2020-08-11 13:38  Miles Elam <miles.elam@productops.com>
  parent: Pavel Stehule <pavel.stehule@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Miles Elam @ 2020-08-11 13:38 UTC (permalink / raw)
  To: Pavel Stehule <pavel.stehule@gmail.com>; +Cc: mohand oubelkacem makhoukhene <mohand-oubelkacem@outlook.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Also of note: PostgreSQL already has a money type (
https://www.postgresql.org/docs/current/datatype-money.html)
But you shouldn't use it (
https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_money).

I only bring it up so that you can know to make your money type a slightly
different name to avoid a conflict. Money is deceptively hard to implement
correctly. I'd recommend reading the second link if you have not already to
avoid previously known issues.


On Tue, Aug 11, 2020 at 5:02 AM Pavel Stehule <pavel.stehule@gmail.com>
wrote:

> Hi
>
> út 11. 8. 2020 v 13:31 odesílatel mohand oubelkacem makhoukhene <
> mohand-oubelkacem@outlook.com> napsal:
>
>> Hello;
>> I whould like to implement a new data type next to char, number,
>> varchar... A Money type.
>> So i'll have to change the source code, but i don't know which fonctions
>> i need to change and which part to work on.
>> I need some help to implement this new data type.
>>
>
> If you need to implement just a new data type, then you don't need to
> modify Postgres source code. You can write your own extension.
>
> For own custom type you need to write minimally "in" and "out" function,
> and then you can run "CREATE TYPE" statement
>
> one example of custom type can be a extension
> https://github.com/okbob/pgDecimal
>
> Regards
>
> Pavel
>
>
> Thank you and best regards
>> Mohand
>>
>>
>>
>>

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Implement a new data type
@ 2020-08-11 13:39  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: mohand oubelkacem makhoukhene <mohand-oubelkacem@outlook.com>
  2 siblings, 0 replies; 11+ messages in thread

From: Adrian Klaver @ 2020-08-11 13:39 UTC (permalink / raw)
  To: mohand oubelkacem makhoukhene <mohand-oubelkacem@outlook.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On 8/11/20 4:31 AM, mohand oubelkacem makhoukhene wrote:
> Hello;
> I whould like to implement a new data type next to char, number, 
> varchar... A Money type.

One that is different from the current one?:

https://www.postgresql.org/docs/12/datatype-money.html

> So i'll have to change the source code, but i don't know which fonctions 
> i need to change and which part to work on.
> I need some help to implement this new data type.
> Thank you and best regards
> Mohand
> 
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Implement a new data type
@ 2020-08-12 00:01  raf <raf@raf.org>
  parent: Miles Elam <miles.elam@productops.com>
  0 siblings, 2 replies; 11+ messages in thread

From: raf @ 2020-08-12 00:01 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On Tue, Aug 11, 2020 at 06:38:39AM -0700, Miles Elam <miles.elam@productops.com> wrote:

> Also of note: PostgreSQL already has a money type (
> https://www.postgresql.org/docs/current/datatype-money.html)
> But you shouldn't use it (
> https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_money).
> 
> I only bring it up so that you can know to make your money type a slightly
> different name to avoid a conflict. Money is deceptively hard to implement
> correctly. I'd recommend reading the second link if you have not already to
> avoid previously known issues.

I use decimal(10,2) for whole cents, and decimal(12,6)
for sub-cents. Single currency only. I didn't know
there was a money type originally, but it wouldn't be
usable for me anyway without the ability to specify the
scale and precision.

I recommend considering passing values to the database
as "decimal '1.23'" rather than bare numeric literals,
just so there's no chance of the value being
interpreted as a float at any stage by postgres. Maybe
that's being too paranoid but that's a good idea when
it comes to money. :-)

Perhaps the incorporation of currency would make a new
money type interesting. Currencies probably don't
change as often as timezones but there would probably
still be some ongoing need for updates.

cheers,
raf






^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Implement a new data type
@ 2020-08-12 05:38  Chris Travers <chris.travers@gmail.com>
  parent: raf <raf@raf.org>
  1 sibling, 0 replies; 11+ messages in thread

From: Chris Travers @ 2020-08-12 05:38 UTC (permalink / raw)
  To: raf <raf@raf.org>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Wed, Aug 12, 2020 at 2:01 AM raf <raf@raf.org> wrote:

> On Tue, Aug 11, 2020 at 06:38:39AM -0700, Miles Elam <
> miles.elam@productops.com> wrote:
>
> > Also of note: PostgreSQL already has a money type (
> > https://www.postgresql.org/docs/current/datatype-money.html)
> > But you shouldn't use it (
> > https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_money).
> >
> > I only bring it up so that you can know to make your money type a
> slightly
> > different name to avoid a conflict. Money is deceptively hard to
> implement
> > correctly. I'd recommend reading the second link if you have not already
> to
> > avoid previously known issues.
>
> I use decimal(10,2) for whole cents, and decimal(12,6)
> for sub-cents. Single currency only. I didn't know
> there was a money type originally, but it wouldn't be
> usable for me anyway without the ability to specify the
> scale and precision.
>

It is worth noting that decimal is an alias for numeric in Postgres.   For
that reason you will have less confusion if you use numeric instead.

>
> I recommend considering passing values to the database
> as "decimal '1.23'" rather than bare numeric literals,
> just so there's no chance of the value being
> interpreted as a float at any stage by postgres. Maybe
> that's being too paranoid but that's a good idea when
> it comes to money. :-)
>

I don't think the type designation buys you anything. unless it is a part
of an arithmetic expression  The single quotes do and cannot be omitted
here.

So I think there is a difference between ('1.23' + 1)::numeric and
'1.23'::numeric + 1 but there is also a difference between 1.23::numeric +
1 and '1.23'::numeric + 1

But there is no reason to add the cast when doing something like an insert
of a single value.

>
> Perhaps the incorporation of currency would make a new
> money type interesting. Currencies probably don't
> change as often as timezones but there would probably
> still be some ongoing need for updates.
>

The existing money type has another problem in that the currency it is
attached to is taken from the current locale.  So if you change your locale
settings you can change a value from, say, 100 IDR to 100 EUR at least for
display purposes.

I have some thoughts about how to do a multi-currency type but I am not
actually sure you get anything by tying the data together instead of having
it in separate columns.

>
> cheers,
> raf
>
>
>
>

-- 
Best Wishes,
Chris Travers

Efficito:  Hosted Accounting and ERP.  Robust and Flexible.  No vendor
lock-in.
http://www.efficito.com/learn_more

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Implement a new data type
@ 2020-08-12 16:44  Philip Semanchuk <philip@americanefficient.com>
  parent: raf <raf@raf.org>
  1 sibling, 1 reply; 11+ messages in thread

From: Philip Semanchuk @ 2020-08-12 16:44 UTC (permalink / raw)
  To: raf <raf@raf.org>; +Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>



> On Aug 11, 2020, at 8:01 PM, raf <raf@raf.org> wrote:
> 
> On Tue, Aug 11, 2020 at 06:38:39AM -0700, Miles Elam <miles.elam@productops.com> wrote:
> 
>> Also of note: PostgreSQL already has a money type (
>> https://www.postgresql.org/docs/current/datatype-money.html)
>> But you shouldn't use it (
>> https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_money).
>> 
>> I only bring it up so that you can know to make your money type a slightly
>> different name to avoid a conflict. Money is deceptively hard to implement
>> correctly. I'd recommend reading the second link if you have not already to
>> avoid previously known issues.
> 
> I use decimal(10,2) for whole cents, and decimal(12,6)
> for sub-cents. Single currency only. I didn't know
> there was a money type originally, but it wouldn't be
> usable for me anyway without the ability to specify the
> scale and precision.
> 
> I recommend considering passing values to the database
> as "decimal '1.23'" rather than bare numeric literals,
> just so there's no chance of the value being
> interpreted as a float at any stage by postgres. Maybe
> that's being too paranoid but that's a good idea when
> it comes to money. :-)

Yes, I agree, this is also important (and easy to overlook) if you’re accessing the database via a non-SQL language. We use Python which, like most (all?) languages that rely on the underlying C library for floating point support, is vulnerable to floating point noise. Python has a fixed precision type, and like Postgres it also accepts character and float input. The float input can give surprising results.

>>> decimal.Decimal('1.79')   # This is OK
Decimal('1.79')
>>> decimal.Decimal(1.79)     # This will not end well!
Decimal('1.79000000000000003552713678800500929355621337890625')
>>>

In the case of a Postgres column like numeric(10,2), input like 1.79000000000000003552713678800500929355621337890625 will get rounded to 1.79 anyway and no harm will be done. But like you said, raf, it’s a good idea to be too paranoid. :-)

Cheers
Philip




^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Implement a new data type
@ 2020-08-12 23:38  raf <raf@raf.org>
  parent: Philip Semanchuk <philip@americanefficient.com>
  0 siblings, 0 replies; 11+ messages in thread

From: raf @ 2020-08-12 23:38 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On Wed, Aug 12, 2020 at 12:44:21PM -0400, Philip Semanchuk <philip@americanefficient.com> wrote:

> > On Aug 11, 2020, at 8:01 PM, raf <raf@raf.org> wrote:
> > 
> > On Tue, Aug 11, 2020 at 06:38:39AM -0700, Miles Elam <miles.elam@productops.com> wrote:
> > 
> >> Also of note: PostgreSQL already has a money type (
> >> https://www.postgresql.org/docs/current/datatype-money.html)
> >> But you shouldn't use it (
> >> https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_money).
> >> 
> >> I only bring it up so that you can know to make your money type a slightly
> >> different name to avoid a conflict. Money is deceptively hard to implement
> >> correctly. I'd recommend reading the second link if you have not already to
> >> avoid previously known issues.
> > 
> > I use decimal(10,2) for whole cents, and decimal(12,6)
> > for sub-cents. Single currency only. I didn't know
> > there was a money type originally, but it wouldn't be
> > usable for me anyway without the ability to specify the
> > scale and precision.
> > 
> > I recommend considering passing values to the database
> > as "decimal '1.23'" rather than bare numeric literals,
> > just so there's no chance of the value being
> > interpreted as a float at any stage by postgres. Maybe
> > that's being too paranoid but that's a good idea when
> > it comes to money. :-)
> 
> Yes, I agree, this is also important (and easy to overlook) if
> you’re accessing the database via a non-SQL language. We use Python
> which, like most (all?) languages that rely on the underlying C
> library for floating point support, is vulnerable to floating point
> noise. Python has a fixed precision type, and like Postgres it also
> accepts character and float input. The float input can give surprising
> results.
> 
> >>> decimal.Decimal('1.79')   # This is OK
> Decimal('1.79')
> >>> decimal.Decimal(1.79)     # This will not end well!
> Decimal('1.79000000000000003552713678800500929355621337890625')
> >>>
> 
> In the case of a Postgres column like numeric(10,2), input like
> 1.79000000000000003552713678800500929355621337890625 will get rounded
> to 1.79 anyway and no harm will be done. But like you said, raf, it’s
> a good idea to be too paranoid. :-)
> 
> Cheers
> Philip

Chris Travers pointed out to me that Postgres itself
parses floating point literals as the numeric type
(i.e. "select pg_typeof(1.23);" returns numeric) so
Postgres has made the right choice for its parser,
unlike most(?) languages (except raku). But yes, in
Python, it's decimal.Decimal with integer/string input
all the way (with dec=decimal.Decimal if you have a lot
of them).

cheers,
raf






^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Implement a new data type
@ 2020-08-13 10:25  mohand oubelkacem makhoukhene <mohand-oubelkacem@outlook.com>
  0 siblings, 0 replies; 11+ messages in thread

From: mohand oubelkacem makhoukhene @ 2020-08-13 10:25 UTC (permalink / raw)
  To: pgsql-general; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Hello

I would like to implement a new data type next to char, number, varchar... for example a special "Money" type, but
I don't want to use extensions and the Create type command.  I want to implement it directly inside source code,
because I want to implement my new type at lower level, in order to perform some more sophisticated functions after.
Just as an example,  help the optimizer in its decisions.
How should I proceed ? Is it an easy task ?

Thanks
Mohand

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Implement a new data type
@ 2020-09-04 10:45  mohand oubelkacem makhoukhene <mohand-oubelkacem@outlook.com>
  0 siblings, 0 replies; 11+ messages in thread

From: mohand oubelkacem makhoukhene @ 2020-09-04 10:45 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Hello,

If I want to create a new type Person (firstname varchar, lastname varchar, address varchar ...) what is the best way to procede in Postgresql

Create type or  create extension ? What are advantages /disadvantages of both solutions ?
Thank you and best regards
Mohand

^ permalink  raw  reply  [nested|flat] 11+ messages in thread


end of thread, other threads:[~2020-09-04 10:45 UTC | newest]

Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-08-11 11:31 Implement a new data type mohand oubelkacem makhoukhene <mohand-oubelkacem@outlook.com>
2020-08-11 12:00 ` Laurenz Albe <laurenz.albe@cybertec.at>
2020-08-11 12:01 ` Pavel Stehule <pavel.stehule@gmail.com>
2020-08-11 13:38   ` Miles Elam <miles.elam@productops.com>
2020-08-12 00:01     ` raf <raf@raf.org>
2020-08-12 05:38       ` Chris Travers <chris.travers@gmail.com>
2020-08-12 16:44       ` Philip Semanchuk <philip@americanefficient.com>
2020-08-12 23:38         ` raf <raf@raf.org>
2020-08-11 13:39 ` Adrian Klaver <adrian.klaver@aklaver.com>
2020-08-13 10:25 Implement a new data type mohand oubelkacem makhoukhene <mohand-oubelkacem@outlook.com>
2020-09-04 10:45 Implement a new data type mohand oubelkacem makhoukhene <mohand-oubelkacem@outlook.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox