Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Hackers

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-12-2008, 05:36 AM
Greg Mitchell
 
Posts: n/a
Default Custom Data Type Question

I'm trying to create a custom data type similar to an enumeration type.
However, I'd like the mapping of the int<->string to be dynamic instead
of hard coded. I'd like to have a table that contains this mapping that
can be appended to. Creating this type is not very difficult. However,
for performance reasons, I'd like to cache the mapping so that the table
is only queried once every connection unless it changes. I'm thinking a
combination of a flag that can be triggered on insert and a transaction
id could be used to decide if the table needs to be reloaded.
Unfortunately, I'm not exactly sure how to get started on this, any ideas?

Thanks,
Greg

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-12-2008, 05:36 AM
Andrew Dunstan
 
Posts: n/a
Default Re: Custom Data Type Question

Greg Mitchell wrote:
> I'm trying to create a custom data type similar to an enumeration
> type. However, I'd like the mapping of the int<->string to be dynamic
> instead of hard coded. I'd like to have a table that contains this
> mapping that can be appended to. Creating this type is not very
> difficult. However, for performance reasons, I'd like to cache the
> mapping so that the table is only queried once every connection unless
> it changes. I'm thinking a combination of a flag that can be triggered
> on insert and a transaction id could be used to decide if the table
> needs to be reloaded. Unfortunately, I'm not exactly sure how to get
> started on this, any ideas?
>
>


Are you aware that there is a patch for first class enumeration types
waiting to be reviewed for 8.3? The mapping is kept entirely internal,
and you should never see what it is kept as underneath. It does not
provide for dynamically extending the enumeration set, for various
reasons, but there is an easy workaround, namely to create a new type
with the extra member(s) and then do:

alter table foo alter column bar type newtype using bar::newtype;


My little enumkit tool allows you to create enumerations today very
easily, but its values are completely hardcoded. However, the above
trick still works. The downside is that each enumeration type requires a
tiny bit of compilation.

cheers

andrew



---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-12-2008, 05:36 AM
Tom Dunstan
 
Posts: n/a
Default Re: Custom Data Type Question

Hi Greg

Greg Mitchell wrote:
> I'm trying to create a custom data type similar to an enumeration type.
> However, I'd like the mapping of the int<->string to be dynamic instead
> of hard coded. I'd like to have a table that contains this mapping that
> can be appended to. Creating this type is not very difficult. However,
> for performance reasons, I'd like to cache the mapping so that the table
> is only queried once every connection unless it changes.


A simpler way to do this might be to only cache the list per query
context. In your IO functions, you could whack a pointer to your cache
onto fcinfo->flinfo->fn_extra, and the same flinfo gets passed in for
e.g. all output function calls for that column for that query, IIRC.
This was what I had in mind originally when I did the enum patch, but I
ended up just using syscaches, which I think would be unavailable to you
writing a UDT.

The upside of the above is that for a given query, the contents of your
table shouldn't change, so there's no mucking about with trying to
keep things in other backends up to date. The downside is that you have
to do the lookup per query, but if you're dealing with lots of data then
it'll get dwarfed by the actual query, and if not, who cares?

The other question that leaps to mind is whether you want to have more
than one of these types. If you do, you may have to have multiple
versions of the IO functions, otherwise e.g. your output function might
be passed the value 0, but was that the 0 representing the 'red' string
from the rgb enum, or the 'northern' string from the hemisphere enum?
You don't know, and postgresql won't tell you directly.

There are a few ways around this. In your case, it might be ok to
compile different versions of the IO functions for each enum which point
to different tables, or the same table with a discriminator. Or you
could see the various different proposals when my patch was first
discussed. See the thread starting at
http://archives.postgresql.org/pgsql...8/msg00979.php or if
you want a peek at the patch, see
http://archives.postgresql.org/pgsql...9/msg00000.php. A
rather simpler starting point might be Andrew's enumkit
http://www.oreillynet.com/pub/a/data...owcontent=text,
or possibly Martijn's tagged types at
http://svana.org/kleptog/pgsql/taggedtypes.html.

Cheers

Tom


---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-12-2008, 05:36 AM
Greg Mitchell
 
Posts: n/a
Default Re: Custom Data Type Question


> A simpler way to do this might be to only cache the list per query
> context. In your IO functions, you could whack a pointer to your cache
> onto fcinfo->flinfo->fn_extra, and the same flinfo gets passed in for
> e.g. all output function calls for that column for that query, IIRC.
> This was what I had in mind originally when I did the enum patch, but I
> ended up just using syscaches, which I think would be unavailable to you
> writing a UDT.


If my understanding is correct, if fn_extra is null, I would palloc() my
data cache and store the pointer in fn_extra? What about freeing this
pointer? Or is cleanup automatic?

Also, are there any ADTs like a hash-map or tree-map in the server
libraries (my background is C++ and am use to having std::map<>) or do I
need to role my own?

I am using enumkit for some true enums I have in the DB and like it very
much. Though I tend to customize the C-code to optimize it for my use.

Thanks,
Greg

---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-12-2008, 05:36 AM
Greg Mitchell
 
Posts: n/a
Default Re: Custom Data Type Question

As far as memory management goes, do I just use hash_create() and assign
that pointer to fn_extra and at the end of the query it will be freed?
Or will it not be freed until this end of the transaction? I'm really
having trouble understanding the memory management issues with Postgres.

Greg

Andrew Dunstan wrote:
> Greg Mitchell wrote:
>>
>>
>>
>> Also, are there any ADTs like a hash-map or tree-map in the server
>> libraries (my background is C++ and am use to having std::map<>) or do I
>> need to role my own?
>>
>>

>
> Look at the dynahash code. I just used it for the first time in a plperl
> patch, and it's reasonably straightforward.
>
> cheers
>
> andrew
>


---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-12-2008, 05:36 AM
Andrew Dunstan
 
Posts: n/a
Default Re: Custom Data Type Question

Greg Mitchell wrote:
>
>
>
> Also, are there any ADTs like a hash-map or tree-map in the server
> libraries (my background is C++ and am use to having std::map<>) or do I
> need to role my own?
>
>


Look at the dynahash code. I just used it for the first time in a plperl
patch, and it's reasonably straightforward.

cheers

andrew


---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-12-2008, 05:36 AM
Tom Lane
 
Posts: n/a
Default Re: Custom Data Type Question

Greg Mitchell <gmitchell@atdesk.com> writes:
> As far as memory management goes, do I just use hash_create() and assign
> that pointer to fn_extra and at the end of the query it will be freed?
> Or will it not be freed until this end of the transaction? I'm really
> having trouble understanding the memory management issues with Postgres.


You have to be careful that the hashtable is created in the correct
"memory context" --- in this case you want it to be in a query-lifespan
context, not the short-term (per-tuple-lifespan) context that your
function will be called in. The usual procedure for cases like this is
to use the context identified by fn_mcxt. src/backend/utils/mmgr/README
might make useful reading for you.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-12-2008, 05:38 AM
Simon Riggs
 
Posts: n/a
Default Re: Custom Data Type Question

On Wed, 2006-11-15 at 16:38 -0500, Andrew Dunstan wrote:

> My little enumkit tool allows you to create enumerations today very
> easily, but its values are completely hardcoded. However, the above
> trick still works. The downside is that each enumeration type requires a
> tiny bit of compilation.


Andrew,

Your enum sounds good, apart from the hardcoded/compilation thing. That
is a data management nightmare AFAICS and so restricts the usefulness of
the solution.

It would be much better to read things dynamically into an array, so
using an init function in *preload_libraries would work well.

I'd also love any suggestions as to how we might be able to use a
similar local-data-cacheing mechanism to work when we specify SQL
standard FOREIGN KEYs. It would be really cool to say USING LOCAL CACHE
or some way of avoiding the overhead of all those stored after triggers
and SPI SELECT statements when we've got checks against tables with only
a few rows where the values hardly ever change. The enum concept departs
radically from the declarative Referential Integrity concepts that many
of us are already used to. I'd like to be able to speed things up
without radical re-design of the database... so a few nicely sprinked
ALTER TABLE statements would be a much better way of implementing this
IMHO.

--
Simon Riggs
EnterpriseDB http://www.enterprisedb.com



---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-12-2008, 05:38 AM
Andrew Dunstan
 
Posts: n/a
Default Re: Custom Data Type Question



Simon Riggs wrote:
> On Wed, 2006-11-15 at 16:38 -0500, Andrew Dunstan wrote:
>
>
>> My little enumkit tool allows you to create enumerations today very
>> easily, but its values are completely hardcoded. However, the above
>> trick still works. The downside is that each enumeration type requires a
>> tiny bit of compilation.
>>

>
> Andrew,
>
> Your enum sounds good, apart from the hardcoded/compilation thing. That
> is a data management nightmare AFAICS and so restricts the usefulness of
> the solution.
>
>
>


Simon, Tom Dunstan has submitted a patch for first class enum types that
do not have the compilation requirement - it's in the queue for 8.3. You
might want to review that.

cheers

andrew

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-12-2008, 05:39 AM
Simon Riggs
 
Posts: n/a
Default Re: Custom Data Type Question

On Tue, 2006-11-21 at 02:51 +0000, Tom Dunstan wrote:

> Requiring a table to represent a small fixed set of
> allowable values that a column should take is broken. But because
> it's
> the least ugly solution that we've had using vanilla SQL, it's what
> we've used, and dare I suggest that because we've all done it for so
> long, we start to think that *not* doing it that way is broken.


I do support your goal of higher performance.

Putting data in tables is reasonably accepted practice, round here at
least.

I see the strong need to optimise the case where people want/need to
follow the SQL standard and have defined their databases that way. There
is also the need to support DELETE RESTRICT functionality from the
referenced to the referencing table, as a protection against data
quality problems. A link between two tables is important - otherwise we
introduce another DBA task and the possibility of error that results.

If there is a body of opinion behind enums, then thats good. The MySQL
way is not something to be ignored and that is a good argument for
inclusion. I've got no problem with multiple ways of doing things.

In the long run, as currently envisaged, enums don't do all that I would
like. I see the need to performance tune Referential Integrity more
directly.

--
Simon Riggs
EnterpriseDB http://www.enterprisedb.com



---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 11:35 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
UnixAdminTalk.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613