postgresql-9.1

limit field's value with value from another table before write

与世无争的帅哥 提交于 2019-12-25 09:35:03
问题 Are Postgres TRIGGER s transactional by default like (I've read) in MySQL? I've created a TRIGGER procedure that uses a simple IF to limit a column's value with a value from another TABLE with a subsequent UPDATE if the limit is breached. I'd prefer that to be in one single TRANSACTION , but if I wrap the IF ... THEN UPDATE with BEGIN ... COMMIT , it gives error SQL error: ERROR: syntax error at or near ";" LINE 2: BEGIN; ^ Are TRIGGER s TRANSACTION al by default? If not, how can a TRIGGER be

postgres: Joining a small table with large table

◇◆丶佛笑我妖孽 提交于 2019-12-25 07:48:57
问题 Postgres Server Version: server 9.1.9 explain analyze select * from A, B where A.groupid = B.groupid; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------- Merge Join (cost=1.23..8.64 rows=2 width=104) (actual time=0.076..204.212 rows=3 loops=1) Merge Cond: (A.groupid = B.groupid) -> Index Scan using A_pkey on A (cost=0.00..68144.37 rows=1065413 width=88) (actual time=0.008..115.366 rows=120938

How to get a whole row from database using SOCI?

核能气质少年 提交于 2019-12-25 06:45:33
问题 ... and save it into self-defined object type? I'm using PostgreSQL. When I have everything in one file, it works. But I wanted to split this into class-files like you always do when writing in cpp. When I divided my code into *.h and *.cpp files, I'm getting errors. Here are my files: test.h class MyInt { public: MyInt(); MyInt(int i); void set(int i); int get() const; private: int i_; }; test.cpp #include "test.h" #include <soci.h> #include <postgresql/soci-postgresql.h> MyInt::MyInt() { }

Apply a single trigger procedure to many different tables

元气小坏坏 提交于 2019-12-25 02:57:14
问题 In my PostgreSQL 9.1 database I have multiple tables and one trigger function. Right now I am creating the trigger for each table by using that trigger function. This methodology working fine. My boss has asked me to create the trigger commonly (only one time) by re-using that trigger function. That one trigger function should get used by all the tables in my database. 回答1: You can find an example of creating a trigger with dynamic SQL using PL/PgSQL in the Audit Trigger sample for PostgreSQL

postgres query to get first row based on multiple copies of some columns

会有一股神秘感。 提交于 2019-12-25 01:53:42
问题 Suppose I have a table - A B C 1 3 5 1 3 7 1 3 9 2 4 3 2 4 6 2 4 1 here there are multiple copies for the same combination of A and B. for each combination I want back the first entry of it. so the result for this table i want to be- A B C 1 3 5 2 4 3 How can I do this in postgres sql? 回答1: Assuming you can define "first" in terms of a sort on a, b, and c you want DISTINCT ON for this. SELECT DISTINCT ON ("A", "B") "A", "B", "C" FROM Table1 ORDER BY "A", "B", "C"; E.g. http://sqlfiddle.com/#

Why is Rails dropping the Postgres connection on large bulk inserts?

本小妞迷上赌 提交于 2019-12-24 14:34:52
问题 I'm brand-spanking new to the world of linux and server administration, and I'm stuck. I have a rails app that occasionally needs to perform large data inserts, usually around 20,000 rows. The code seems to work fine in development (osx), but on the production servers (ubunto, on a linode vps), it fails every time, usually after about 1,700 insertions. The precise number varies (1655, 1697, 1756), but it's consistently in that ballpark. I'm not seeing much that's helpful in the production.log

No operator matches the given name and argument type(s)

╄→гoц情女王★ 提交于 2019-12-24 14:33:52
问题 I set up my Rails 3.2.x app to use PostgreSQL HStore but I'm getting an error. It looks like the hstore extension hasn't been picked up by the environment. I already rebooted my machine, checked database extensions, etc. When I try to execute: User.where("settings @> (:key => :value)", :key => "setting_x", :value => "test") I get an error: ( @> is not recognized, i.e., extension hstore is not installed?) HINT: No operator matches the given name and argument type(s). You might need to add

How to include count equal to 0 on this query?

时光毁灭记忆、已成空白 提交于 2019-12-24 11:03:18
问题 My query select state,count(*) from crm_lead group by state; which return state count done 794 open 560 pending 3 draft 8 cancel 1 but sometime when I have no 'cancel' task in my table state count done 794 open 560 pending 4 draft 8 I would like to include count = 0 in the result, what is the best query ? 回答1: SELECT a.STATE , COALESCE(b.count, 0) AS Count FROM ( SELECT 'done' AS STATE UNION SELECT 'open' AS STATE UNION SELECT 'pending' AS STATE UNION SELECT 'draft' AS STATE UNION SELECT

postgresql-sort array by words in each elements

你。 提交于 2019-12-24 00:53:16
问题 There is string array ARRAY['CAT','CAT DOG CAT','DOG Cat'] Now i want to sort this array on count of words in each element. I have tried but cannot get any success. I want this output ARRAY['CAT DOG CAT','DOG CAT','Cat'] How i can do this? 回答1: This does feel rather clumsy, but I can't think of a simpler solution right now: with val (col) as ( values (ARRAY['CAT','CAT DOG CAT','DOG Cat']) ), word_list as ( select unnest(col) as pc from val ), wc as ( select array_length(string_to_array(pc, '

Can PostgreSQL 9.1 leak locks? (out of shared memory/increase max_pred_locks_per_transaction)

南笙酒味 提交于 2019-12-23 22:54:47
问题 We recently upgraded to postgresql 9.1.6 (from 8.3). Our test server indicated that the max_pred_locks_per_transaction should be set at least as high as 900 (which is way beyond the recommended setting of 64). We're now in production, and I've had to increase this parameter many times, as our log will start filling with: ERROR: 53200: out of shared memory HINT: You might need to increase max_pred_locks_per_transaction. With a client connection setting of 600 (but a pooling system that never