pg.ddx.io pgsql-performance@postgresql.org mailing list archive
help / color / mirror / Atom feedRe: increment Rows in an SQL Result Set postgresql
4+ messages / 4 participants
[nested] [flat]
* increment Rows in an SQL Result Set postgresql
@ 2006-07-15 17:46 Hassan Adekoya <hechy_man@yahoo.com>
2006-07-15 17:27 ` Re: increment Rows in an SQL Result Set postgresql Craig A. James <cjames@modgraph-usa.com>
2006-07-15 23:53 ` Re: increment Rows in an SQL Result Set postgresql Peter Eisentraut <peter_e@gmx.net>
0 siblings, 2 replies; 4+ messages in thread
From: Hassan Adekoya @ 2006-07-15 17:46 UTC (permalink / raw)
To: pgsql-performance
HI,
1. I have a function that given two parameter produces an arbitrary id, and
text. However arbitrary the id and text are, they are in certain order. i.e. it
is imperative that whatever processing I do, the order is preserved.
2. An odd thing happens when I perform a join on the result set such that the
order that I hope to preserved in destroyed. The same result set but different
ordering. I gather this is due to the query planner. Enough said.
I was hoping to insert a counter in the select query of 1. such that when I
perform the join of 2, I can order by the counter.
i.e.
1. select id, astext from function1(1,2)
2. select id, astext, table2.name from function1(1,2) as tmp, table2 where
tmp.id = table2.id
when I perform 1., I get something of sort
id | astext
2 | abc
6 | efg
3 | fhg
I will like to preserve ordering....
When I perform 2, I get somthing of sort
id | astext | table2.name
6 | efg | joe
2 | abc | zyi
3 | fgh | mec
Can someone help such that I get something like
id | astext | table2.name | increment
6 | efg | joe | 2
2 | abc | zyi | 1
3 | fgh | mec | 3
Thanks!
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: increment Rows in an SQL Result Set postgresql
2006-07-15 17:46 increment Rows in an SQL Result Set postgresql Hassan Adekoya <hechy_man@yahoo.com>
@ 2006-07-15 17:27 ` Craig A. James <cjames@modgraph-usa.com>
2006-07-15 18:53 ` Re: increment Rows in an SQL Result Set postgresql Hassan <hechy_man@yahoo.com>
1 sibling, 1 reply; 4+ messages in thread
From: Craig A. James @ 2006-07-15 17:27 UTC (permalink / raw)
To: Hassan Adekoya <hechy_man@yahoo.com>; +Cc: pgsql-performance
Hassan,
> 1. I have a function that given two parameter produces an arbitrary id, and
> text. However arbitrary the id and text are, they are in certain order. i.e. it
> is imperative that whatever processing I do, the order is preserved.
What type of function is this? Did you write it in C? An SQL procedure?
If the function is written in C, you can create a static local variable which you increment every time you call your function, and which you return along with your other two values. As long as your client is connected to the back-end server, you're guaranteed that it's a single process, and it's not multi-threaded, so this is a safe approach. However, note that if you disconnect and reconnect, your counter will be reset to zero.
If your function is written in a different language or is a procedure, you might create a sequence that your function can query.
The trick is that it is the function itself that must return the incremented value, i.e. you must return three, not two, values from your function. That way, you're not relying on any specific features of the planner, so your three values will stick together.
Craig
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: increment Rows in an SQL Result Set postgresql
2006-07-15 17:46 increment Rows in an SQL Result Set postgresql Hassan Adekoya <hechy_man@yahoo.com>
2006-07-15 17:27 ` Re: increment Rows in an SQL Result Set postgresql Craig A. James <cjames@modgraph-usa.com>
@ 2006-07-15 18:53 ` Hassan <hechy_man@yahoo.com>
0 siblings, 0 replies; 4+ messages in thread
From: Hassan @ 2006-07-15 18:53 UTC (permalink / raw)
To: pgsql-performance
Sadly I didnt write this function. It was written in C and packaged in a shared module .so. I access it thru postgresql as plpgsql function. I cannot edit the function thus.
I tried this
CREATE TEMPORARY SEQUENCE serial START 1;
SELECT nextval('serial'), astext(tmp.the_geom), street FROM shortest_path_as_geometry('bklion', 185, 10953) AS tmp LEFT JOIN (SELECT * FROM bklion) AS ss ON ss.the_geom = tmp.the_geom;
I know this is inefficient, and I surely dont know the repercussion of using the temporary sequence in a web application. Do you?
Appreciate any input.
Thanks!
- Hassan Adekoya
----- Original Message ----
From: Craig A. James <cjames@modgraph-usa.com>
To: Hassan Adekoya <hechy_man@yahoo.com>
Cc: pgsql-performance@postgresql.org
Sent: Saturday, July 15, 2006 1:27:20 PM
Subject: Re: [PERFORM] increment Rows in an SQL Result Set postgresql
Hassan,
> 1. I have a function that given two parameter produces an arbitrary id, and
> text. However arbitrary the id and text are, they are in certain order. i.e. it
> is imperative that whatever processing I do, the order is preserved.
What type of function is this? Did you write it in C? An SQL procedure?
If the function is written in C, you can create a static local variable which you increment every time you call your function, and which you return along with your other two values. As long as your client is connected to the back-end server, you're guaranteed that it's a single process, and it's not multi-threaded, so this is a safe approach. However, note that if you disconnect and reconnect, your counter will be reset to zero.
If your function is written in a different language or is a procedure, you might create a sequence that your function can query.
The trick is that it is the function itself that must return the incremented value, i.e. you must return three, not two, values from your function. That way, you're not relying on any specific features of the planner, so your three values will stick together.
Craig
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: increment Rows in an SQL Result Set postgresql
2006-07-15 17:46 increment Rows in an SQL Result Set postgresql Hassan Adekoya <hechy_man@yahoo.com>
@ 2006-07-15 23:53 ` Peter Eisentraut <peter_e@gmx.net>
1 sibling, 0 replies; 4+ messages in thread
From: Peter Eisentraut @ 2006-07-15 23:53 UTC (permalink / raw)
To: pgsql-performance; +Cc: Hassan Adekoya <hechy_man@yahoo.com>
Hassan Adekoya wrote:
> I will like to preserve ordering....
Tables are inherently unordered. If you want a particular order, you
need to use the ORDER BY clause. And you will need to have a column to
sort by. If you don't have one, the generate_series() function may
help.
This has nothing to do with performance, I gather, so it might be more
appropriate for the pgsql-sql list.
--
Peter Eisentraut
http://developer.postgresql.org/~petere/
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2006-07-15 23:53 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-07-15 17:46 increment Rows in an SQL Result Set postgresql Hassan Adekoya <hechy_man@yahoo.com>
2006-07-15 17:27 ` Craig A. James <cjames@modgraph-usa.com>
2006-07-15 18:53 ` Hassan <hechy_man@yahoo.com>
2006-07-15 23:53 ` Peter Eisentraut <peter_e@gmx.net>
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