Re: [SQL] Create Assertion -- Question from a newbie
Alvaro Herrera <[EMAIL PROTECTED]> writes: > Well, it's on the SQL standard, so we should look forward to > implementing (some form of) it some day. Do *any* databases implement SQL standard Assertions? -- greg ---(end of broadcast)--- TIP 5: don't forget to increase your free space map settings
Re: [SQL] query to select a linked list
"Louis-David Mitterrand" <[EMAIL PROTECTED]> writes: > Each message a unique id_forum and an id_parent pointing to the replied > post (empty if first post). > > How can I build an elegant query to select all messages in a thread? You would need recursive queries which Postgres doesn't support. There is a patch out there to add support but I don't think it's up-to-date with 8.2 and in any case the resulting queries can be quite intense. I would recommend you look into the contrib module named "ltree". It's easy to use and works well with the gist indexes. It does require changing your data model denormalizing it slightly which makes it hard to "reparent" children, but if that isn't an operation you have to support I think it makes most other operations you might want to do much easier to support. -- Gregory Stark EnterpriseDB http://www.enterprisedb.com ---(end of broadcast)--- TIP 1: if posting/reading through Usenet, please send an appropriate subscribe-nomail command to [EMAIL PROTECTED] so that your message can get through to the mailing list cleanly
Re: [SQL] query to select a linked list
"Scott Marlowe" <[EMAIL PROTECTED]> writes: > Are you sure the tablefunc functions, which include both connectby and > crosstab functions, aren't up to date with 8.2? They certainly are up > to 8.1, where I'm running them right now on my workstation. They built > for 8.2 and installed, but I haven't tried using them. > > I would think that connectby is at least worth looking into. Uhm, no, I guess I'm not sure. I didn't realize it was in the tablefunc module either. -- Gregory Stark 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
Re: [SQL] aggregate query
"Andrew Kroeger" <[EMAIL PROTECTED]> writes: > Raj A wrote: >> I have a table >> >> CREATE TABLE survey_load >> ( >> meter_id character(5) NOT NULL, >> number_of_bays integer NOT NULL, >> bay_1_use integer, >> bay_2_use integer, >> bay_3_use integer, >> bay_4_use integer, >> bay_5_use integer, >> date date NOT NULL, >> inspection_id integer NOT NULL DEFAULT, >> ) >> >> How do i present an aggregate query >> >> inspection_id | meter_id | bay_use >> 1 12345(value of bay_1_use) >> 1 12345(value of bay_2_use) >> 1 12345(value of bay_3_use) >> 2 23456(value of bay_1_use) >> 2 23456(value of bay_2_use) >> 2 23456(value of bay_3_use) >> 2 23456(value of bay_4_use) >> 2 23456(value of bay_5_use) > > If I understand your issue correctly, it seems like the denormalized > nature of your table is causing you some problems. True. Normalizing the tables would make this query easier which is a good sign that that's probably the right direction. If for some reason you can't or won't change the table definition there are a number of possible tricky answers given the current definition. Something like this for example: SELECT inspection_id, meter_id, case when bay=1 then bay_1_use when bay=2 then bay_2_use when bay=3 then bay_3_use when bay=4 then bay_4_use when bay=5 then bay_5_use else null end AS bay_use FROM ( SELECT *, generate_series(1,number_of_bays) AS bay FROM survey_load ) as x -- Gregory Stark EnterpriseDB http://www.enterprisedb.com ---(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
Re: [SQL] Recursively traversing a partially ordered set
"Jason Grout" <[EMAIL PROTECTED]> writes: > 2. Is there a big difference in speed between using an array versus > using a SELECT in a WHERE condition? In other words, which is generally > going to be faster: > > SELECT * from table where field IN (some function returning a SETOF); > > or > > SELECT * from table where field = ANY(some function returning an array); In theory since these are equivalent (well nearly. To be equivalent the optimizer would need to know whether the array could possibly have NULLs in it) the optimizer ought to produce the same plan for each. In practice it's not clear where the optimizer would get the information to decide what plan to use for these two cases and whether it would have the same kinds of information available. In any case in practice the plans available in each of these cases are not the same so you'll have to try them and see which one works better for you. I think there are more plans available for the first case so it may work out better if you're returning quite large sets where those plans help. If you're returning quite small sets where you just need a simple bitmap index scan then the second will be less likely to pick (or actually I think it's incapable of picking) some other plan which works poorly. There was some discussion recently on what to do about exactly this type of case. I would be interested to hear about what plans you got from each and which plan ended up being best. > 3. Is there a strong reason I should strip out duplicates in either of > the two cases in question 2? Or is the performance about the same when > doing the queries whether or not the SETOF or arrays contain duplicates? The plans where it matters will remove the duplicates anyways, but I don't think the array version does if you're not using an bitmap index scan. > 4. Can you see any obvious optimizations to the above functions > (particularly the last one)? > > Thanks for your help. Thanks for the absolutely wonderful database and > solid documentation. I originally did this project in MySQL and had the > weirdest errors (the errors turned out to be due to the default > case-insensitive collation of MySQL!). That's when I decided to move to > postgresql when I updated the project. Well, unfortunately collocation support isn't exactly a strong point in Postgres either. Plenty of people get bitten by their database being initdb'd in a locale they didn't expect. -- Gregory Stark EnterpriseDB http://www.enterprisedb.com ---(end of broadcast)--- TIP 5: don't forget to increase your free space map settings
Re: [SQL] JOIN
"Loredana Curugiu" <[EMAIL PROTECTED]> writes: > I am trying to say that sum column it is not calculated correctly. It looks like your counts are high. That would imply that your join clauses are matching more than one combination of rows. Ie, it's counting some rows multiple times because there are multiple join matches. -- Gregory Stark EnterpriseDB http://www.enterprisedb.com ---(end of broadcast)--- TIP 6: explain analyze is your friend
Re: [SQL] trigger firing order
"chester c young" <[EMAIL PROTECTED]> writes: > tables A and B: a post row trigger on A cause updates on B which has > its own post row trigger. > > does the post trigger on A wait until post trigger on B is executed? - > this seems intuitive to me. How can it wait until the trigger on B is executed if the trigger on B doesn't actually get triggered until someone updates B and it's the trigger on A doing the update? -- Gregory Stark 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
Re: [SQL] percentages of a column
"Andreas" <[EMAIL PROTECTED]> writes: > Example: > > Fruit Count % > -- > Bananas 5 10% > Apples 15 30% > Oranges 30 60% select fruit_name, count(*), round(count(*)::numeric / (select count(*) from basket) * 100, 0)::text||'%' as "%" from basket group by fruit_name order by "%"; fruit_name | count | % +---+- Bananas| 5 | 10% Apples | 15 | 30% Oranges|30 | 60% (3 rows) -- Gregory Stark EnterpriseDB http://www.enterprisedb.com ---(end of broadcast)--- TIP 6: explain analyze is your friend
Re: [SQL] Converting from MS Access field aliases
"Adam Tauno Williams" <[EMAIL PROTECTED]> writes: > "AS" works in Informix, and I believe, in DB2 as well. So it is at > least pretty common; I'm not saying it is correct. Since Informix > predates M$-SQL they at least didn't invent it. AS works in Postgres too. But the defined aliases are only in the resulting output columns, not in scope for the where clause. In fact the expressions used aren't even evaluated for rows which don't match the where clause which is pretty important if those expressions are subqueries or volatile functions. -- Gregory Stark 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
Re: [SQL] Query Plan
"Radhika Sambamurti" <[EMAIL PROTECTED]> writes: > When I run the query with combination of FirmClearingID & status the run > times are approx 3700ms. > But when I add tradedate ie date_trunc('day', tradedate) = '20070703' the > run time becomes a horrendous 19631.958 ms. I'm not really able to make heads or tails of your plans without the query. But any where clause of the form date_trunc('...',col)='...' will always generate crappy plans. And it looks like you already have expressions of that form in the view even in the one which you label "without date_trunc". Instead try to write a where clause that the database can understand the end points of. Something like (col >= '20070703' and col < '20070704'). That's something the database can apply a normal index to and also something it can have a chance at guessing how many rows will fit. -- Gregory Stark EnterpriseDB http://www.enterprisedb.com ---(end of broadcast)--- TIP 6: explain analyze is your friend
Re: [SQL] The nested view from hell - Restricting a subquerry
Nis Jørgensen <[EMAIL PROTECTED]> writes: > Well, the query can be satisfied by looking only at the rows with an > order_id matching the invoice_id given. The condition that this is the > largest invoice in the group then needs to be checked afterwards. > > I certainly did not expect the query planner to be able to deduce this, > though. No, that's not true. If you had two records in eg_order with the same order_id but different invoice_ids then the query would need both records to satisfy the query. The query planner can't deduce that this can't happen because it simply does not have that information. The more I look at this view the more I think it's just seriously broken. Why is it grouping by order_id at all if, I suspect, there will only be one record per order_id in eg_orders?? -- Gregory Stark EnterpriseDB http://www.enterprisedb.com ---(end of broadcast)--- TIP 2: Don't 'kill -9' the postmaster
Re: [SQL] The nested view from hell - Restricting a subquerry
Nis Jørgensen <[EMAIL PROTECTED]> writes: >>> Well, the query can be satisfied by looking only at the rows with an >>> order_id matching the invoice_id given. The condition that this is the >>> largest invoice in the group then needs to be checked afterwards. >>> >>> I certainly did not expect the query planner to be able to deduce this, >>> though. >> >> No, that's not true. If you had two records in eg_order with the same >> order_id >> but different invoice_ids then the query would need both records to satisfy >> the query. > > I assume you mean "... then both records are necessary in order to > calculate the results of the query". This does not contradict what I wrote. Sorry I meant, "the query as written can not be satisfied by looking only at the rows with the specified invoice_id". > SELECT order_id, > max(order_view.invoice_id), > sum(order_view.mileage) > FROM(SELECT order_id,invoice_id, 0 as mileage FROM eg_order > UNION > SELECT order_id, 0, mileage FROM eg_order_line) > order_view GROUP BY order_view.order_id; > > This is then restricted on max(invoice_id) > > As far as I can tell, these steps produce the correct results (without > the later information about primary keys provided by Bryce) > > INPUT: my_invoice_id > > 1. Look up all order_ids for which (order_id,my_invoice_id) appear in > eg_orders > > 2. Find all rows (in both branches of the UNION) with these id_s > > 3. Group the rows, and calculate max(invoice_id) > > 4. Filter the result rows on max(invoice_id) = my_invoice_id. So here's a hypothetical data set for which this algorithm fails: order_idinvoice_id mileage 1 1 100 1 2 100 Your algorithm would produce order_id max(invoice_id) sum(mileage) 1 1 100 Whereas the correct output would be to output no records at all. -- Gregory Stark EnterpriseDB http://www.enterprisedb.com ---(end of broadcast)--- TIP 5: don't forget to increase your free space map settings
Re: [SQL] The nested view from hell - Restricting a subquerry
"Gregory Stark" <[EMAIL PROTECTED]> writes: > Nis Jørgensen <[EMAIL PROTECTED]> writes: > >> 1. Look up all order_ids for which (order_id,my_invoice_id) appear in >> eg_orders >> >> 2. Find all rows (in both branches of the UNION) with these id_s Oh, did you mean look up the order_ids for which there was at least one record with the invoice_id specified, then look up all records with those order_ids regardless of invoice_id? That would work but as you say it would be hard to tell whether it will be any faster than just processing all the order_ids. -- Gregory Stark EnterpriseDB http://www.enterprisedb.com ---(end of broadcast)--- TIP 5: don't forget to increase your free space map settings
Re: [SQL] is there a 'table' data type in pg?
"Louis-David Mitterrand" <[EMAIL PROTECTED]> writes: > Can I use a another column to store the type of the id_subject (ie: the > tabled it belongs to) ? Then I would be able to query that table for > additional info to print alongside the forum posts. There are ways to identifier tables in Postgres but there's no way to run a query against a table using them. I would strongly recommend you define your own list of "object_types", probably even have an object_type table with a primary key, a description column, and a table_name column. Then you can in your application construct the appropriate query depending on the object_type. One alternative you could do is have a set-returning plpgsql function which has a big if statement and performs the right kind of query. I think the records would have to all be the same -- they can't be different kinds of records depending on the type of object. -- Gregory Stark EnterpriseDB http://www.enterprisedb.com ---(end of broadcast)--- TIP 6: explain analyze is your friend
Re: [SQL] PG won't use index on ORDER BY
"Andreas Joseph Krogh" <[EMAIL PROTECTED]> writes: > I create an index: > CREATE INDEX person_lowerfullname_idx ON > person((lower(COALESCE(firstname, '')) || lower(COALESCE(lastname, ''))) > varchar_pattern_ops); Why are you declaring it using the varchar_pattern_ops? The default operator set is the one you want for handling ordering. The pattern_ops operator set is for handling things like x LIKE 'foo%' -- Gregory Stark EnterpriseDB http://www.enterprisedb.com ---(end of broadcast)--- TIP 3: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faq
Re: [SQL] PG won't use index on ORDER BY
"Andreas Joseph Krogh" <[EMAIL PROTECTED]> writes: > Ooops, just fugured that out. But - it still doesn't use the index if I > remove > the "varchar_pattern_ops". I solved it by adding a function: Hm, well it does for me, you would have to post your explain analyze to see what's going on. > Another question then: Why doesn't "varchar_pattern_ops" handle ordering? > This > means I need 2 indexes on the columns I want to match with LIKE and ORDER BY. > Just doesn't seem right to need 2 "similar" indexes... If you initd in the C locale you only need one index. In other locales the collation order and the pattern order are different. -- Gregory Stark EnterpriseDB http://www.enterprisedb.com ---(end of broadcast)--- TIP 4: Have you searched our list archives? http://archives.postgresql.org
Re: [SQL] SELECT syntax synopsis: column_definition?
"Michael Glaesemann" <[EMAIL PROTECTED]> writes: > ERROR: a column definition list is only allowed for functions returning > "record" > > So the *form* is right, but I don't know of an example that works. postgres=# create function testf() returns record as 'select 1' language sql; CREATE FUNCTION postgres=# select * from testf() as (i integer); i --- 1 (1 row) I haven't quite figured out how this is useful though. It probably makes more sense if you use plpgsql but I still don't quite see what the use case is. -- Gregory Stark EnterpriseDB http://www.enterprisedb.com ---(end of broadcast)--- TIP 1: if posting/reading through Usenet, please send an appropriate subscribe-nomail command to [EMAIL PROTECTED] so that your message can get through to the mailing list cleanly
Re: [SQL] Incomprehensible dogged sort in Merge Join
"Aleksandr Vinokurov" <[EMAIL PROTECTED]> writes: > And that is its plan (attached one is the same, but with costs): > >>-< > Merge Left Join >Merge Cond: ("outer".name = "inner".name) >-> Sort > Sort Key: log.name > -> Seq Scan on log_example_3 log >-> Sort > Sort Key: uh.name > -> Subquery Scan uh >-> Sort > Sort Key: name > -> Seq Scan on user_history >>-< What version are you running with? Incidentally with CVS HEAD I see it working: postgres=# explain select * from (select * from a order by i) as a right join b on a.i = b.i ; QUERY PLAN - Merge Right Join (cost=299.56..675.13 rows=22898 width=16) Merge Cond: (public.a.i = b.i) -> Sort (cost=149.78..155.13 rows=2140 width=8) Sort Key: public.a.i -> Seq Scan on a (cost=0.00..31.40 rows=2140 width=8) -> Sort (cost=149.78..155.13 rows=2140 width=8) Sort Key: b.i -> Seq Scan on b (cost=0.00..31.40 rows=2140 width=8) (8 rows) > With best regards to all of you, > Aleksandr. > ÿþ M e r g e L e f t J o i n > ( c o s t = 2 0 7 6 4 6 . 0 0 . . 2 1 3 8 6 4 . 1 2 r o w s = 3 4 7 8 5 1 > w i d t h = 6 2 ) ( a c t u a l > t i m e = 3 0 9 2 2 . 3 6 6 . . 3 2 1 6 6 . 5 1 8 r o w s = 1 3 9 0 8 > l o o p s = 1 ) > M e r g e C o n d : ( "