Re: SELECT FOR UPDATE performance is bad

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Suppose you have a table codes :
(
    game_id    INT,
    code        TEXT,
    used        BOOL NOT NULL DEFAULT 'f',
    prize        ...
    ...
    PRIMARY KEY (game_id, code)
)

Just UPDATE codes SET used='t' WHERE used='f' AND game_id=... AND code=...

Then check the rowcount : if one row was updated, the code was not used yet. If no row was updated, the code either did not exist, or was already used.

You can use a stored procedure with exceptions no?

Try this:

http://www.postgresql.org/docs/8.1/interactive/plpgsql-control-structures.html#PLPGSQL-UPSERT-EXAMPLE

Chris




[Postgresql General]     [Postgresql PHP]     [PHP Users]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Yosemite]

  Powered by Linux