Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value of a variable in plv8 without using plv8.elog()?

I am having an issue with getting correct output from plv8.elog(). Just to start, I'm using PostgreSQL 9.2, plv8 1.4.1 as installed by pgxn, on Ubuntu 12.10. I have a function I am building that includes nested cursor loops and uses a number of prepared statements. I am properly closing the cursors and freeing the prepared statements. All of this functionality generates an array of objects that are records that will be inserted in a number of different tables.

Where it is breaking down is at the point where I loop through that array using the data in the objects to generate query strings. I am in the building phase of this part of the code, so at the point where I would normally execute the query string, I instead try to output it to the console. This is where the corruption happens.

When outputting the 9 query strings it generates in my test, only 7 of them get displayed. When I go into the postgres logs, I find that on the end of the 2 missing query strings, there is a character that looks like a diamond with a question mark in it. Basically it is some character that can't be displayed in the current character set. I have tested all parts of the query generator code and cannot find any reason for this. I believe the plv8.elog() function or the plv8 engine's string concatenation functionality is corrupting for some reason.

The problem I have is, if it is the string concatenation functionality, I can't trust query strings to execute properly. I have tested the code by removing it from the function and using static information in the array. Under that circumstance, it works fine, so I know there isn't something wrong with that part of the code.

First, has anyone else ever seen this? Corrupt or weird behaviour from plv8.elog()? Any corruption of data when your function got to a certain size or complexity? If so, were you able to figure a way of at least working around it?

Also, if it is the plv8.elog() function, is there another way of getting the values in variables? I know there is supposed to be v8 debugger functionality, but I have been unsuccessful at getting that to work. If anyone has experience with getting that to work, could you please help me to get that working as well? Thanks.

like image 897
Jordan Avatar asked Nov 13 '22 02:11

Jordan


1 Answers

This happens because plv8's log output is limited to a certain number of bytes/bits. If you are concatenating several long strings and sending it to plv8.elog(), it will cut off the strings after it hits the maximum number of bytes/bits. To get around this, I do this:

plv8.elog(INFO, myString.substring(0,800));
plv8.elog(INFO, myString.substring(801,1600));
plv8.elog(INFO, myString.substring(1601,2400));

I think the limit is 1024, but some of that is taken up by the INFO: part of the log output.

like image 55
bendiy Avatar answered Nov 14 '22 23:11

bendiy