Thread: Basic problems using plpythonu - bug?
I'm trying to do something pretty basic per the documentation using plpython triggers. When I change the "cfgCmd" field in the SysConfig table below, the trigger works fine and if I have a null function that does nothing but write to the pipe, that all works fine. The trouble is when the function attemts to read from the SysConfig database, "cfgCmd" field which tells the function what to do. When I write a value to the SysConfig table, cfgCmd field, the server log says:
ERROR: plppython: function "send_ctl_msg" failed
DETAIL: exceptions.KeyError: 'cfgCmd'
What's a key error? I can't find any documentation on it? Also, when I try to reference TD["old"][<column name>] this gives me an error too. Does this have something to do with the untrusted language part or am I doing something wrong? Thanks much for any help,
Curt
Here is the trigger function:
-- Send Control Message (send_ctl_msg)
CREATE FUNCTION send_ctl_msg() RETURNS trigger AS $$
import os
import os.path
CREATE FUNCTION send_ctl_msg() RETURNS trigger AS $$
import os
import os.path
pipe_loc = TD["args"][0]
old = TD["old"]
new = TD["new"]
new = TD["new"]
rv = plpy.execute("SELECT * from ucfg.SysConfig",1)
ens_cmd = rv[0]["cfgCmd"]
plpy.log(cmd)
ens_cmd = rv[0]["cfgCmd"]
plpy.log(cmd)
if os.path.exists(pipe_loc):
pipeout = open(pipe_loc,"w")
print >>pipeout,ens_cmd
else:
plpy.error("Build System cmd FIFO not found. Make sure VMD is running")
$$ LANGUAGE plpythonu;
pipeout = open(pipe_loc,"w")
print >>pipeout,ens_cmd
else:
plpy.error("Build System cmd FIFO not found. Make sure VMD is running")
$$ LANGUAGE plpythonu;
Here is the table it's trying to access:
CREATE TABLE ucfg.SysConfig (
-- System map selection and running
startupSysMapName VARCHAR(64) REFERENCES ucfg.SysMaps(sysMapName),
-- System map to load and run at system boot.
activeSysMapName VARCHAR(64) REFERENCES ucfg.SysMaps(sysMapName),
-- System map that is currently loaded.
checkSysMapName VARCHAR(64) REFERENCES ucfg.SysMaps(sysMapName),
-- System map to be checked to see if it builds properly
cfgCmd VARCHAR(16), -- ENS configuration control command
-- "NONE", "CHECK", "LOAD", "RUN", "STOP"
startupSysMapName VARCHAR(64) REFERENCES ucfg.SysMaps(sysMapName),
-- System map to load and run at system boot.
activeSysMapName VARCHAR(64) REFERENCES ucfg.SysMaps(sysMapName),
-- System map that is currently loaded.
checkSysMapName VARCHAR(64) REFERENCES ucfg.SysMaps(sysMapName),
-- System map to be checked to see if it builds properly
cfgCmd VARCHAR(16), -- ENS configuration control command
-- "NONE", "CHECK", "LOAD", "RUN", "STOP"
);
Here is the trigger function:
CREATE TRIGGER tr_exec
AFTER UPDATE ON ucfg.SysConfig for EACH ROW
EXECUTE PROCEDURE public.send_ctl_msg("/var/ens/cmdfifo");
AFTER UPDATE ON ucfg.SysConfig for EACH ROW
EXECUTE PROCEDURE public.send_ctl_msg("/var/ens/cmdfifo");
On Fri, Feb 17, 2006 at 11:38:21AM -0600, Curt Schwaderer wrote: > ERROR: plppython: function "send_ctl_msg" failed > DETAIL: exceptions.KeyError: 'cfgCmd' You're getting bit by case folding of identifiers. You created the column as: > cfgCmd VARCHAR(16), -- ENS configuration control command Since you didn't quote the identifier it's folded to lowercase, so when you refer to it as > ens_cmd = rv[0]["cfgCmd"] you get a KeyError exception. Try using rv[0]["cfgcmd"]. You might want to read the documentation about quoted identifiers: http://www.postgresql.org/docs/8.1/interactive/sql-syntax.html#SQL-SYNTAX-IDENTIFIERS -- Michael Fuhr
Thanks a bunch - that cleaned everything up! Curt ----- Original Message ----- From: "Michael Fuhr" <mike@fuhr.org> To: "Curt Schwaderer" <curt.schwaderer@mchsi.com> Cc: <pgsql-general@postgresql.org> Sent: Friday, February 17, 2006 11:57 AM Subject: Re: [GENERAL] Basic problems using plpythonu - bug? > On Fri, Feb 17, 2006 at 11:38:21AM -0600, Curt Schwaderer wrote: >> ERROR: plppython: function "send_ctl_msg" failed >> DETAIL: exceptions.KeyError: 'cfgCmd' > > You're getting bit by case folding of identifiers. You created > the column as: > >> cfgCmd VARCHAR(16), -- ENS configuration control command > > Since you didn't quote the identifier it's folded to lowercase, so > when you refer to it as > >> ens_cmd = rv[0]["cfgCmd"] > > you get a KeyError exception. Try using rv[0]["cfgcmd"]. You might > want to read the documentation about quoted identifiers: > > http://www.postgresql.org/docs/8.1/interactive/sql-syntax.html#SQL-SYNTAX-IDENTIFIERS > > -- > Michael Fuhr