PL/R (plr) returns an error when using dbSendQuery twice in a function; "plr_cursor" already exists

StackOverflow https://stackoverflow.com/questions/20287296

Pergunta

I installed PL/R (plr) and extended my database with it.

I created a function that creates a pdf print from a plot. The data is queried from my PostgreSQL database:

CREATE OR REPLACE FUNCTION f_graph() RETURNS text AS
'
require(RPostgreSQL)
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host="localhost", user="postgres", password="pass",dbname="landslide", port="5432")
rs <- dbSendQuery(con, "SELECT x,y,z FROM section_coordinates WHERE id=1")
section1 <- fetch(rs, 2000)
pdf("/tmp/myplot.pdf", width=18, height=12)
plot(section1$y, section1$z, xlab="distance in m", ylab="altitude in m a.s.l.",main="Section Einbühl Ebermannstadt", type="l", lwd=1.5, lty=3)
dev.off()
print("done")
'
LANGUAGE 'plr' VOLATILE STRICT;

But when I want to query two result sets (section1, section2) like in this extended function:

CREATE OR REPLACE FUNCTION f_graph() RETURNS text AS
'
require(RPostgreSQL)
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host="localhost", user="postgres", password="pass", dbname="landslide", port="5432")
rs <- dbSendQuery(con, "SELECT x,y,z FROM section_coordinates WHERE id=1")
section1 <- fetch(rs, 2000)
rs <- dbSendQuery(con, "SELECT x,y,z FROM section_coordinates WHERE id=2")
section2 <- fetch(rs, 2000)
pdf("/tmp/myplot.pdf", width=18, height=12)
plot(section1$y, section1$z, xlab="distance in m", ylab="altitude in m a.s.l.", main="Section Einbühl Ebermannstadt", type="l", lwd=1.5, lty=3)
lines(section2$y, section2$z, xlab="", ylab="", type="l", lwd=2.5, col="red")
dev.off()
print("done")
'
LANGUAGE 'plr' VOLATILE STRICT;

There appears the following error:

landslide=# SELECT f_graph();
ERROR:  R interpreter expression evaluation error
DETAIL:  Error in pg.spi.cursor_open("plr_cursor", plan) : 
error in SQL statement : cursor "plr_cursor" already exists
CONTEXT:  In R support function pg.spi.cursor_open
In PL/R function f_graph

How can solve this problem? Can I set multiple plr_cursors?

Foi útil?

Solução

You need to clear the result before asking for more data I think.

Try to use dbClearResult after you got the first result

section1 <- fetch(rs, 2000)
dbClearResult(rs)

Even if no errors are generated, use dbClearResult(rs) after the last fetch, before closing the connection with dbDisconnect(con).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top