Help With Stored Procedures In Postgresql

Joined: 11/28/2008

Here's a run down of what I'm working with. I have a table that contains game information (home team, away team, location, etc.). I have a second table that contains game assignments (who is working the game); this table has a game_id to reference the, well, game's id. I'm wanting to use a stored procedure to store snapshots of both tables whenever a change is made to a game. Initially I was going to create a stored procedure for each table and set a trigger before an update occurred, but there is information I want to store that I don't have access to when the triggers go off. Because of this, I'm trying to create a function that will perform both actions when I call it. I'm not accustomed to writing stored procedures, so I may be doing unnecessary actions (such as nothing := "INSERT ..."; it's the only way I could create the function without getting some kind of compile error). Here's what I have so far:

CODE
CREATE OR REPLACE FUNCTION game_snapshot(int) RETURNS void AS '
DECLARE
    current_game_id ALIAS FOR $1;
    changed_game_id integer;
    game_data RECORD;
    game_assignments RECORD;
    nothing RECORD;
BEGIN
    game_data := "SELECT * FROM operations.games WHERE id = current_game_id";
    changed_game_id := "SELECT nextval(operations.game_change_id_seq)";

    nothing := "INSERT INTO operations.game_changes(id, game_id, ts, level, hometeam, awayteam, location, status, user_id)
    VALUES(changed_game_id, current_game_id, game_data.ts, game_data.level, game_data.hometeam, game_data.awayteam, game_data.location, game_data.status, game_data.user_id)";

    FOR game_assignments IN SELECT * FROM operations.assignments WHERE game_id = current_game_id LOOP
        nothing := "INSERT INTO operations.assignment_changes(game_change_id, user_id, game_fee, travel_fee, perdiem_fee, status)
        VALUES(changed_game_id, game_assignments.user_id, game_assignments.fee_id, game_assignments.travel_fee, game_assignments.perdiem_fee, game_assignments.status)";
    END LOOP;

    RETURN;
END
' LANGUAGE plpgsql

The intent is to pass the game_id, select all data of the specified game, store it in a separate table, grab all assignments for that game and store that information in a separate table as well. After calling this function I would update the game and assignments accordingly so that I can maintain a history of changes for the games. Any ideas on what I'm doing wrong?

Who can bring a charge to God's elect? It is God who justifies!

Joined: 11/28/2008
After getting a second set of

After getting a second set of eyes to look and critique my actions, I finally have a working solution. Not sure if anyone would be interested, but if so, I can post the solution.

Who can bring a charge to God's elect? It is God who justifies!

Joined: 11/28/2008
Could you just make use of a

Could you just make use of a "date_expired" type column, so that you can archive off the original row, and then do a "INSERT INTO operations.games SELECT FROM ..."

Paul Davey
Whitford Church
"Everyone who calls on the name of the Lord will be saved." Romans 10:13
"For all have sinned and fall short of the glory of God, and are justified

Joined: 11/28/2008
I'm not sure how an

I'm not sure how an additional date field will help. The goal is to, no matter what, make a snapshot of the game's details before it is updated, regardless of how much or how little is changed. INSERT INTO table SELECT FROM would work on my games table, but would not be as useful for my assignments table where there are multiple rows to duplicate. Thanks for the feedback though, I do appreciate it.

Who can bring a charge to God's elect? It is God who justifies!

Joined: 11/28/2008
The only purpose of the date

The only purpose of the date expired column would be to separate out the otherwise duplicate rows (or mostly duplicate), so you knew which one was newest. I guess you can use a primary key for that.

Paul Davey
Whitford Church
"Everyone who calls on the name of the Lord will be saved." Romans 10:13
"For all have sinned and fall short of the glory of God, and are justified

Joined: 11/28/2008
Ah ok. I have a timestamp

Ah ok. I have a timestamp field in my archive table so that I know when the changes were made, but not in my active game table.

Who can bring a charge to God's elect? It is God who justifies!