plpgsql

Return rows from a PL/pgSQL function

可紊 提交于 2019-11-30 09:42:45
问题 I have a procedure in PostgreSQL: CREATE OR REPLACE FUNCTION get_geom_difference() RETURNS void AS $$ BEGIN SELECT filedata.num,st_area(ST_Difference(ST_TRANSFORM(filedata.the_geom,70066),filedata_temp.the_geom)) FROM filedata, filedata_temp Where filedata.num=filedata_temp.num end; $$ LANGUAGE 'plpgsql' I call it in Java and want to get result of this procedure. How to change this procedure to make it possible get a result? And how to work with it in JDBC? Now I use this: Integer fileId;

Get values from varying columns in a generic trigger

做~自己de王妃 提交于 2019-11-30 09:34:17
问题 I am new to PostgreSQL and found a trigger which serves my purpose completely except for one little thing. The trigger is quite generic and runs across different tables and logs different field changes. I found here. What I now need to do is test for a specific field which changes as the tables change on which the trigger fires. I thought of using substr as all the column will have the same name format e.g. XXX_cust_no but the XXX can change to 2 or 4 characters. I need to log the value in

How to copy structure of one table to another with foreign key constraints in psql?

泄露秘密 提交于 2019-11-30 09:29:24
Foreign key constraints are not copied when using create table table_name ( like source_table INCLUDING ALL)' in Postgres. How can I create a copy of an existing table including all foreign keys. There is no option to automatically create foreign keys in CREATE TABLE ... LIKE ... . For the documentation: LIKE source_table [ like_option ... ] Not-null constraints are always copied to the new table. CHECK constraints will be copied only if INCLUDING CONSTRAINTS is specified [...] Indexes, PRIMARY KEY, and UNIQUE constraints on the original table will be created on the new table only if the

Declare row type variable in PL/pgSQL

走远了吗. 提交于 2019-11-30 08:40:19
As I found SELECT * FROM t INTO my_data; works only if: DO $$ DECLARE my_data t%ROWTYPE; BEGIN SELECT * FROM t INTO my_data WHERE id = ?; END $$; Am I right? If I want to get only 2-3 columns instead of all columns. How can I define my_data ? That is, DO $$ DECLARE my_data <WHAT HERE??>; BEGIN SELECT id,name,surname FROM t INTO my_data WHERE id = ?; END $$; get only 2-3 columns instead of all columns One way: use a record variable: DO $$ DECLARE _rec record; BEGIN SELECT INTO _rec id, name, surname FROM t WHERE id = ?; END $$; Note that the structure of a record type is undefined until

How to check if a row exists in a PostgreSQL stored procedure?

你离开我真会死。 提交于 2019-11-30 06:52:05
I writing a stored procedure in postgres where I need to check if a row exists then act accordingly. something along the line. IF SELECT * FROM foo WHERE x = 'abc' AND y = 'xyz' THEN -- do something here ELSE -- do something else END; I have googled a bit but got no good hits. Use PERFORM and the FOUND automatic variable : PERFORM * FROM foo WHERE x = 'abc' AND y = 'xyz'; IF FOUND THEN .... END IF; This will succeed if one or more rows is returned. If you want to constrain the result to exactly one row use GET DIAGNOSTICS to get the row count , or use SELECT INTO to store the count(...) of the

Disable DELETE on table in PostgreSQL?

て烟熏妆下的殇ゞ 提交于 2019-11-30 06:22:23
For a security sensitive design, I'd like to disable DELETEs on certain tables. The DELETE should merely set a deleted flag on a row (which would be then visible on a view, which would be used by the application layer). As I understand a rule would generate additional queries - so a rule could not suppress the original query. As illustration a toy example with a trigger (not yet tested): -- data in this table should be 'undeletable' CREATE table article ( id serial, content text not null, deleted boolean default false ) -- some view that would only show articles, that are NOT deleted ... --

PostgreSQL - Writing dynamic sql in stored procedure that returns a result set

你。 提交于 2019-11-30 05:14:07
How can I write a stored procedure that contains a dynamically built SQL statement that returns a result set? Here is my sample code: CREATE OR REPLACE FUNCTION reporting.report_get_countries_new ( starts_with varchar, ends_with varchar ) RETURNS TABLE ( country_id integer, country_name varchar ) AS $body$ DECLARE starts_with ALIAS FOR $1; ends_with ALIAS FOR $2; sql VARCHAR; BEGIN sql = 'SELECT * FROM lookups.countries WHERE lookups.countries.country_name >= ' || starts_with ; IF ends_with IS NOT NULL THEN sql = sql || ' AND lookups.countries.country_name <= ' || ends_with ; END IF; RETURN

Postgres SELECT … FOR UPDATE in functions

不打扰是莪最后的温柔 提交于 2019-11-30 04:23:34
I have two questions about using SELECT … FOR UPDATE row-level locking in a Postgres function: Does it matter which columns I select? Do they have any relation to what data I need to lock and then update? SELECT * FROM table WHERE x=y FOR UPDATE; vs SELECT 1 FROM table WHERE x=y FOR UPDATE; I can't do a select in a function without saving the data somewhere, so I save to a dummy variable. This seems hacky; is it the right way to do things? Here is my function: CREATE OR REPLACE FUNCTION update_message(v_1 INTEGER, v_timestamp INTEGER, v_version INTEGER) RETURNS void AS $$ DECLARE v_timestamp

Dynamically generate columns in PostgreSQL

坚强是说给别人听的谎言 提交于 2019-11-30 04:04:53
问题 I have seen that there are quit a few similar questions like this one, but I havent understood how to code it myself. Please have in mind that I am just a beginner in this field. Basically I want to pivot the table like this: zoom | day | point zoom | 2015-10-01 | 2015-10-02 | ...... ------+-----------+------- ---> ------+------------+-------------+ 1 | 2015-10-01 | 201 1 | 201 | 685 | 2 | 2015-10-01 | 43 2 | 43 | 346 | 3 | 2015-10-01 | 80 3 | 80 | 534 | 4 | 2015-10-01 | 324 4 | 324 | 786 | 5

Testing PostgreSQL functions that consume and return refcursor

牧云@^-^@ 提交于 2019-11-30 03:53:23
问题 I want to test results of a Postgres function (changing the function is not a possibility). The function receives as arguments a REFCURSOR and several other things and returns the same RECURSOR. get_function_that_returns_cursor(ret, 4100, 'SOMETHING', 123465) Now I want to create a small test in Postgres to get the results of this FUNCTION. Something Like the code below (this is my approach but it is not working): DO $$ DECLARE ret REFCURSOR; row_to_read table_it_will_return%ROWTYPE ; BEGIN