plpgsql

ERROR: must be owner of language plpgsql

会有一股神秘感。 提交于 2019-12-05 09:17:51
问题 I'm using PostgreSQL v9.0.1 with Rails (and it's deps) @ v2.3.8 , owing to the use of the fulltext capability of postgres, I have a table which is defined as: CREATE TABLE affiliate_products ( id integer NOT NULL, name character varying(255), model character varying(255), description text, price numeric(9,2), created_at timestamp without time zone, updated_at timestamp without time zone, textsearch_vector tsvector, ); Note the last line, this ensures that active record isn't able to process

postgres. plpgsql stack depth limit exceeded

雨燕双飞 提交于 2019-12-05 07:46:25
im working on a simple function where it automatically updates something from a table. create or replace function total() returns void as $$ declare sum int; begin sum = (SELECT count(copy_id) FROM copies); update totalbooks set all_books = sum where num = 1; end; $$ language plpgsql; if i execute "select total();" it works perfectly fine so i made a function trigger so that it automatically updates: create or replace function total1() returns trigger as $$ begin perform (select total()); return null; end; $$ language plpgsql; but after i execute this: create trigger total2 after update on

PostgreSQL Syntax error in PGAdmin

亡梦爱人 提交于 2019-12-05 05:22:13
I am new to PostgreSQL and am using the query tool in PGAdmin. I'm trying to run pgsql queries that use variables, but I can't seem to get the syntax right. Here's a sample query that gives a syntax error: DECLARE num INTEGER; BEGIN num := 3; PRINT num; END; Update: Ok, let me try and explain. I come from a SQL server background. In the management studio, I can open a query window and play with (T)-SQL queries. For example, I can write something like this: DECLARE @num INT SET @num = 3 SELECT @num I know this is a dumb example, but I'm just trying to declare a variable and do something with it

How to use `RETURN NEXT`in PL/pgSQL correctly?

笑着哭i 提交于 2019-12-05 05:15:09
问题 I am trying to write a loop using PL/pgSQL (PostgreSQL 9.3) function that returns a table. I used RETURN NEXT; with no parameters after each query in the loop, following examples found like plpgsql error "RETURN NEXT cannot have a parameter in function with OUT parameters" in table-returning function, and elsewhere. However, I am still getting an error that says: ERROR: query has no destination for result data HINT: If you want to discard the results of a SELECT, use PERFORM instead. A

Using prepared statement in stored function

◇◆丶佛笑我妖孽 提交于 2019-12-05 03:57:01
I have a table in the database: create table store ( ... n_status integer not null, t_tag varchar(4) t_name varchar, t_description varchar, dt_modified timestamp not null, ... ); In my stored function I need to execute the same select against this table multiple times: select * from store where n_place_id = [different values] and t_tag is not null and n_status > 0 and (t_name ~* t_search or t_description ~* t_search) order by dt_modified desc limit n_max; Here, t_search and n_max are parameters into the stored function. I thought it would make sense to use a prepared statement for this, but I

How to remove conditions from WHERE clause if parameters are NULL

ⅰ亾dé卋堺 提交于 2019-12-05 02:15:24
问题 I'm passing 2 parameters to a PL/pgSQL function. Here's the query: SELECT * FROM table WHERE col1 = param1 AND col2 = param2 Both parameters can be NULL, in which case the respective expression should be removed from the WHERE clause. How can I do that? With IF conditions? 回答1: Maybe this is doing the trick: SELECT * FROM table WHERE col1 = param1 AND (param2 is null or col2 = param2); This is not removing the AND condition, but should make the unimportant in case of param2 is null. So not

How to use variable settings in trigger functions?

蹲街弑〆低调 提交于 2019-12-05 01:35:24
I would like to record the id of a user in the session/transaction, using SET , so I could be able to access it later in a trigger function, using current_setting . Basically, I'm trying option n2 from a very similar ticket posted previously , with the difference that I'm using PG 10.1 . I've been trying 3 approaches to setting the variable: SET local myvars.user_id = 4 , thereby setting it locally in the transaction; SET myvars.user_id = 4 , thereby setting it in the session; SELECT set_config('myvars.user_id', '4', false) , which depending of the last argument, will be a shortcut for the

ERROR: query has no destination for result data

倾然丶 夕夏残阳落幕 提交于 2019-12-05 01:24:58
CREATE OR REPLACE FUNCTION _chkLogin(userid varchar, pwd varchar) RETURNS BOOLEAN AS $BODY$ DECLARE passed BOOLEAN; BEGIN SELECT (_password = $2) FROM _vRegistration WHERE _userid = $1; RETURN passed; END; $BODY$ LANGUAGE 'plpgsql'; When am executing the code above am getting the following error, SELECT _chkLogin('username','abcd') as passed; ERROR: query has no destination for result data I've used perform then i get a different problem, PERFORM _chkLogin('username','abcd'); ERROR: syntax error at or near "perform" Suggest me what should I be doing in order to overcome this error. You do

Preventing 'invalid input syntax for type json' in Postgres

别来无恙 提交于 2019-12-05 00:45:47
I have a text column that contains JSON and also plan text. I want to convert it to JSON, and then select a particular property. For example: user_data _________ {"user": {"name": "jim"}} {"user": {"name": "sally"}} some random data string I've tried: select user_data::json#>'{user,name}' from users I get: ERROR: invalid input syntax for type json DETAIL: Token "some" is invalid. CONTEXT: JSON user_data, line 1: some... Is it possible to prevent this? If you want to skip the rows with invalid JSON, you must first test if the text is valid JSON. You can do this by creating a function which will

How to join table with dynamic identifier in postgres?

不想你离开。 提交于 2019-12-04 23:36:43
问题 I have a table name table containing two columns foreign_table_name , and foreign_key . Is it possible to write a SELECT query that would JOIN values of this table and the table which name is specified in the column foreign_table_name ? For instance, if we know that all possible targetted foreign tables have a name field, I would like to know if I could write something that would: SELECT table.foo, table.bar, foreign_table.name FROM table JOIN $foreign_table AS foreign_table ON (foreign_table