Question

I have just read that the => operator for the hstore extension is deprecated and might be removed in future versions. This is bad news to because I have several functions in R that rely on using this operator when storing to Postgres table that contains hstore fields.

I prepare a data.frame or data.table in memory (R Session) before I write to the database. Subsequently I just write the entire table in memory to the database by using the convenient function dbWriteTable. Hence it would be inconvenient to use the postgres function hstore(text,text).

What is a good strategy to handle such an announcement? I am quite surprised they are removing the operator since there must be quite some people writing to hstore alongside fields of other types. Also this is not really R-specific. It has to be the same issue for many languages..

Though I got some helpful insights so far, maybe this helps to clarify what my problem is:

I added a reproducible example for the R folks and a screenshot for the rest of the world..enter image description here I build a data.frame in R (basically a table representation in memory) like this:

mydf <- structure(list(ts_key = c("somekey", 
                      "somekey"), ts_language = c("de", "en"
                      ), ts_labels = c("\"Kurztitel\" => \"Wohlbefinden\",\"mögliche_Antworten\" => \"fantastisch,so lala,total fertig\",\"Wortlaut\" => \"Wie geht es?\"", 
                                       "\"available_items\" => \"awesome,somewhat ok,wasted\",\"short_title\" => \"well being\",\"wording\" => \"How are you?\""
                      )), .Names = c("ts_key", "ts_language", "ts_labels"), row.names = c(NA, 
                                                                                          -2L), class = "data.frame")

So the memory table / data.frame contains an hstore as a character as one of its columns. This is very convenient because I can have a data.frame with lots of rows and then simply use:

dbWriteTable(myconnection,"somePostgresTableWithTheRightStructure",mydf,append=T)

and just write the entire data.frame to the db without having to build the query or explicit looping. I'd love to keep this but I just don't know how tell dbWriteTable to use hstore(text,text).

Was it helpful?

Solution 2

It's actually not specific to hstore: it affects other custom operators too. The reasoning for its removal in 9.2 is that the SQL standard now reserves that token.

What is a good strategy to handle such an announcement?

Consider switching to using json. It has a similar -> operator, a similar set of functions, and I imagine you've R libraries that allow to go from an object to json and back. At the database-level, the change merely involves a cast from hstore to json (see the very last note on the json docs' page).

OTHER TIPS

Only the operator is being removed, not the underlying functionality. So instead of

a => b

write

hstore(a, b)

This is completely backward compatible. So just change your code today and be done.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top