7

I am working on a PostgreSQL 9.5.3 db which I haven't setup myself. The issue that I have is that a simple update of the form: UPDATE <table> SET <col> = <val> WHERE <col> = <old_val>; does not apply the change. I mention that the pgAdmin output is:

UPDATE 1

Query returned successfully in .. msec

What could be the cause? Could it be explained by some set constraints?

Thanks

14
  • 1
    @a_horse_with_no_name When I do a SELECT afterwards, the value stayed the same. Commented Oct 12, 2017 at 6:32
  • 4
    Missing a commit maybe? Commented Oct 12, 2017 at 6:37
  • 1
    Without more information this is impossible to answer. Could be you are connected to a different database when you check the update or you have a trigger that reverts the assignment of the value or some other process/transaction reverted your change. But if Postgres returns that one row update, then one row was updated. Commented Oct 12, 2017 at 7:03
  • 1
    @VaoTsun This was it! I found the update trigger that matched on the record in question. Thank you all for the guidance! Commented Oct 12, 2017 at 7:17
  • 1
    please share the definition of update function, so I could answer and other users could learn from our experience Commented Oct 12, 2017 at 7:18

2 Answers 2

2

It could be many resons for real update happening and yet rows values not changed: RULES, triggers, wrong schema from search_path and probably many more if you sit and think seriously. You can update other table, other row and set previous value instead of new with bothe RULES and triggers. Thus a full DDL for table and its "dependants" is needed. Here is probably the simplest example with trigger:

f=# create table ut(i int);
CREATE TABLE
f=# insert into ut select 1;
INSERT 0 1
f=# create or replace function tf() returns trigger as
$$
begin
if true then NEW.i=OLD.i; end if;
return NEW;
end;
$$ language plpgsql;
CREATE FUNCTION
f=# create trigger tn before update on ut for each row execute procedure tf();
CREATE TRIGGER
f=# update ut set i = 2;
UPDATE 1
f=# select * from ut;
 i
---
 1
(1 row)

so you see

f=# update ut set i = 2;
UPDATE 1

and yet data not changed.

Also after you commit changes the value could be simply updated by another transaction, thus you select value, updated after you...

Sign up to request clarification or add additional context in comments.

1 Comment

This. Check your triggers with something like: SELECT * FROM information_schema.triggers You may be accidentally automatically updating your row right after you manually update it.
1

Update privilege requires select as well. Maybe that was the issue or will be an issue for someone else.

In practice, any nontrivial UPDATE command will require SELECT privilege as well, since it must reference table columns to determine which rows to update, and/or to compute new values for columns

For more info check the doc : https://www.postgresql.org/docs/current/static/sql-grant.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.