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-11-2008, 06:53 AM
Martijn van Oosterhout
 
Posts: n/a
Default Returning multiple result sets

I've been thinking about this and wondered if this is a way to get it
done without too much work.

1. Create an "anyrecord" type to which any record type can be cast.
It's essentially a heaptuple with a tupledesc.

2. "anyrecord" is opaque to the parser, you cannot dereference it, only
output it or pass it to other functions accepting "anyrecord". Possibly
some dynamic languages may be able to reference these.

3. In the output functions printtup_startup/printtup, if any of the
result fields are of type "anyrecord" it defers the 'T' message until
the tuples arrive. When they do it expands the anyrecord and creates
columns from each and sends an appropriate 'T' message.

4. libpq already supports multiple result sets so no problem there.

The effect of this would be that you get multiple result sets, once for
each time the tupledesc changes. As for creating a split without
changing, maybe you need to return a special empty tuple which
signifies end-of-set.

It also occured to me we could just change the "record" type to do
this, but this would change the behaviour of:

test=# select x from test() as x;
x
-------
(1,2)
(1 row)

to:

a | b
---+---
1 | 2
(1 row)

But that's backward incompatable. OTOH, it would mean we could get rid
of the requirement that functions returning "record" must specify a
column definition list in the query. the only restriction is that you
can't dereference it, but that doesn't seem so big a deal.

So, kill a few birds with one stone. Any thoughts?
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFDf0EmIB7bNG8LQkwRAlDvAJ9MaCk42vWBlDsmkkcQIV LdsttIlACghBWw
vQYiH+X/9RE93P4uDUFW97M=
=aqr5
-----END PGP SIGNATURE-----

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-11-2008, 06:53 AM
Tom Lane
 
Posts: n/a
Default Re: Returning multiple result sets

Martijn van Oosterhout <kleptog@svana.org> writes:
> So, kill a few birds with one stone. Any thoughts?


I don't think any of this will actually work :-(. There's too much code
that assumes that all the tuples returned by a query are alike, and I
for one don't feel like trying to find and fix it all. (Not all of it
is within our control, either --- this will break client code along with
the backend.)

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-11-2008, 06:53 AM
Martijn van Oosterhout
 
Posts: n/a
Default Re: Returning multiple result sets

On Sat, Nov 19, 2005 at 12:43:15PM -0500, Tom Lane wrote:
> Martijn van Oosterhout <kleptog@svana.org> writes:
> > So, kill a few birds with one stone. Any thoughts?

>
> I don't think any of this will actually work :-(. There's too much code
> that assumes that all the tuples returned by a query are alike, and I
> for one don't feel like trying to find and fix it all. (Not all of it
> is within our control, either --- this will break client code along with
> the backend.)


I don't think so, as far as all the functions are concerned, the tuples
are all the same: when a function is called with anyrecord, it's passed
a single argument, the heaptuple+tupledesc. It's an opaque verlena type
that nothing is going to be able to access unless they actually go to
the effort.

All this does is essentially flatten records-in-tuples in the output
function.

Consider:

create function a(anyrecord) returns anyrecord;
create function b(int4) returns anyrecord;

select a(b(2));

Does anything in the backend other than those two functions need to
know the exact format of the "anyrecord"? Even if the actual records
contain 20 values, to everybody else it's just an opaque verlena type.
And in the output (and *only* on output), the printtup function can
examine the tupledesc to tell the client what data to expect.

Seems like it should be possible to me. Another way to put it would be
making records a first-class type. What am I missing?

Thanks in advance,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFDf2jBIB7bNG8LQkwRAjCHAJ4rVzqTIJ3x+hiaz8FBpe OVI0U7zgCcCZ5s
AMTpR6R9E0CPjY5Omb8KJUE=
=t2Ef
-----END PGP SIGNATURE-----

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-11-2008, 06:53 AM
Alvaro Herrera
 
Posts: n/a
Default Re: Returning multiple result sets

Tom Lane wrote:
> Martijn van Oosterhout <kleptog@svana.org> writes:
> > So, kill a few birds with one stone. Any thoughts?

>
> I don't think any of this will actually work :-(. There's too much code
> that assumes that all the tuples returned by a query are alike, and I
> for one don't feel like trying to find and fix it all. (Not all of it
> is within our control, either --- this will break client code along with
> the backend.)


Hmm -- probably we could declare that the current libpq API will not
support multiple result sets from one query, and return only the first
one to the application discarding the rest. (It just occured to me --
what happens if one send multiple SELECTs in a semicolon-separated query
via libpq?). New apps wanting to take advantage of the new
functionality would need to invoke a different function.

At the protocol level this will need an extension anyway, so clients
using the protocol directly would need to be updated to understand
multiple results.

I know people migrating from SQL Server (maybe others?) are already
having trouble because of our inability to return multiple result sets.
The sooner we do it, the sooner all the code will be fixed ...

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-11-2008, 06:53 AM
Pavel Stehule
 
Posts: n/a
Default Re: Returning multiple result sets


>
>Consider:
>
>create function a(anyrecord) returns anyrecord;
>create function b(int4) returns anyrecord;
>
>select a(b(2));
>


for my task I need little different form :-(

create function a(..) returns setof tables

but SQL2003 needs type table, and this can be solution

__________________________________________________ _______________
Emotikony a pozadi programu MSN Messenger ozivi vasi konverzaci.
http://messenger.msn.cz/


---------------------------(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-11-2008, 06:53 AM
Martijn van Oosterhout
 
Posts: n/a
Default Re: Returning multiple result sets

On Sun, Nov 20, 2005 at 08:43:05AM +0100, Pavel Stehule wrote:
> >Consider:
> >
> >create function a(anyrecord) returns anyrecord;
> >create function b(int4) returns anyrecord;
> >
> >select a(b(2));
> >

>
> for my task I need little different form :-(
>
> create function a(..) returns setof tables
>
> but SQL2003 needs type table, and this can be solution


You want a function return entire tables at a time? Why bother when you
can just return rows and signal when the next table starts?

Have a nice day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFDgIOUIB7bNG8LQkwRAgrOAJ9ihFNgbriWgM4ouNB0FX nbyZTRCgCfbc2R
gt1mV9KqCyZKZKs2DtAojCU=
=ZC3N
-----END PGP SIGNATURE-----

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-11-2008, 06:53 AM
Martijn van Oosterhout
 
Posts: n/a
Default Re: Returning multiple result sets

On Sat, Nov 19, 2005 at 08:02:08PM -0300, Alvaro Herrera wrote:
> Hmm -- probably we could declare that the current libpq API will not
> support multiple result sets from one query, and return only the first
> one to the application discarding the rest. (It just occured to me --
> what happens if one send multiple SELECTs in a semicolon-separated query
> via libpq?). New apps wanting to take advantage of the new
> functionality would need to invoke a different function.


libpq supports it just fine. You do a PQsendQuery() and then as many
PQgetResult()s as it takes to get back the results. This worked for a
while AFAIK.

> At the protocol level this will need an extension anyway, so clients
> using the protocol directly would need to be updated to understand
> multiple results.


The protocol supports it fine. If the server sends a new 'T' record,
libpq assumes it's returning a new resultset. When the server sends a
"ready for query" message, libpq knows that the last resultset has
arrived.

It's the backend that needs work, not the protocol.

Have a nice day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFDgIMUIB7bNG8LQkwRAnXcAJ43q5M+RGVaWecszQc71x 2Fvj3JIgCffoOn
GBJcjiWvHLs9sognM4f3qqE=
=79YR
-----END PGP SIGNATURE-----

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-11-2008, 06:53 AM
Tom Lane
 
Posts: n/a
Default Re: Returning multiple result sets

Martijn van Oosterhout <kleptog@svana.org> writes:
> libpq supports it just fine. You do a PQsendQuery() and then as many
> PQgetResult()s as it takes to get back the results. This worked for a
> while AFAIK.


That only works if the caller is prepared to read each result serially,
and not (say) a row at a time in parallel. There are a bunch of
ease-of-use problems as well, such as knowing which resultset is which,
coping with errors detected after the first resultset(s) are sent, etc.

A more realistic way of dealing with multiple resultsets is to deliver
them as named cursor references and allow the client to FETCH
reasonable-sized chunks. We can sort of handle this today, but it's
notationally painful at both the stored-procedure and client ends.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-11-2008, 06:53 AM
Pavel Stehule
 
Posts: n/a
Default Re: Returning multiple result sets

> > for my task I need little different form :-(
> >
> > create function a(..) returns setof tables
> >
> > but SQL2003 needs type table, and this can be solution

>
>You want a function return entire tables at a time? Why bother when you
>can just return rows and signal when the next table starts?
>


what is difference between rows with different structures and tables? Tables
are more logic. But I unlike function which returns setof tables. This need
data type table. I prefere normal clasic solution.

-------------- stored proc -------------- | --------------- client
------------------

function -> scalar, vector, table

procedure -> OUT params
-----------------------------
every free select --------------------------------> table
-----------------------------

I don't have imagine how I can write readable code with your proposal


variants one:

create function aaa returns setof anyrecord
begin
for each a in select * from temptab1
return next a;
end loop;
return next 'next table';
for each a in select * from temptab2
return next a;
end loop;
return next 'ok';
return;
end;

variants two:
create procedure aaa(OUT allok bool)
begin
select * from temptab1;
select * from temptab2;
a := true;
end;

I don't have better words :-). I am sorry. I don't wont to complicate
internal structure of planer, executor, etc ... Procedures are different
than functions, and can be executed different, Isn't possible using
procedure in params list.

Nice day
Pavel

__________________________________________________ _______________
Citite se osamele? Poznejte nekoho vyjmecneho diky Match.com.
http://www.msn.cz/


---------------------------(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
  #10 (permalink)  
Old 04-11-2008, 06:53 AM
Martijn van Oosterhout
 
Posts: n/a
Default Re: Returning multiple result sets

On Sun, Nov 20, 2005 at 11:29:39AM -0500, Tom Lane wrote:
> Martijn van Oosterhout <kleptog@svana.org> writes:
> > libpq supports it just fine. You do a PQsendQuery() and then as many
> > PQgetResult()s as it takes to get back the results. This worked for a
> > while AFAIK.

>
> That only works if the caller is prepared to read each result serially,
> and not (say) a row at a time in parallel. There are a bunch of
> ease-of-use problems as well, such as knowing which resultset is which,
> coping with errors detected after the first resultset(s) are sent, etc.


Urk! I don't think anyone is suggesting that resultsets can be
interleaved. Apart from being extremely unlike the current model in
PostgreSQL, I can't think of a use for it that isn't served just as
well by sending them sequentially.

> A more realistic way of dealing with multiple resultsets is to deliver
> them as named cursor references and allow the client to FETCH
> reasonable-sized chunks. We can sort of handle this today, but it's
> notationally painful at both the stored-procedure and client ends.


But if you run a function, it can only return a single row at a time.
Fiddling with cursors means you would have to queue up. After all, the
function is going to return the tuples in a fixed order that is
independant of when the client asks.

At the end of the day, the client can only accept data in the order the
server sends it. Having to request each row seem somewhat inefficient.

Have a nice day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD4DBQFDgLsDIB7bNG8LQkwRAtXMAJYk5U7wpavgv+QrY3VclS WCrFsrAKCP+fnG
2flMzlREW4B9nJ6otux+WQ==
=+vgj
-----END PGP SIGNATURE-----

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 08:56 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