pllua / pllua-deprecated

[DEPRECATED] This repository is no longer maintained. Please follow https://github.com/pllua/pllua
197 stars 16 forks source link

pg_func #35

Closed floatrain closed 8 years ago

floatrain commented 8 years ago

When I was in the use of the extension, I doubt pgfunc () this function is a BUG.Such as the code below: CREATE or replace the FUNCTION test () RETURNS void AS $$ local ibase_get_hashdata = pgfunc ([[ibase_get_hashdata (integer, text)]] and {only_internal = false}) local a = ibase_get_hashdata (4, '368738464') print (" -- -- -- -- -- -- -- -- -- -- - > end ') $$LANGUAGE plluau; When the function ibase_get_hashdata (4, '368738464') to obtain the value is NULL, namely PG_RETURN_NULL, there will be a segment fault. When I was in single step debugging GDB will find in this function error luaP_pushrecord (L, dat) ——> DatumGetHeapTupleHeader (record), the reason is that the record is NULL.

eugwne commented 8 years ago

duplicate #33 , fixed in #34

floatrain commented 8 years ago

I have another question , as follows : local frequency_signal_list = pgfunc ([[frequency_signal_list (nbe_pu)]], {only_internal = false}) local nbe_pu_list_count = pgfunc ([[nbe_pu_list_count (nbe_pu)]], {only_internal = false}) local nbe_pu_list_get_element = pgfunc ([[nbe_pu_list_get_element (nbe_pu, integer)]], {only_internal = false}) local nbe_list = frequency_signal_list (r.pu)

local count = nbe_pu_list_count (nbe_list) for i = 0, count-1 do local psg = nbe_pu_list_get_element (nbe_list, i) end

r is continuous view one tuple , pu field is a tuple . I use GDB debugging Found nbe_list sometimes not the same as the last in a for loop , which can cause access violation occurs mistakes . I think this should not be ah , which is why ah ?

eugwne commented 8 years ago

pllua does not make datum copies and that might be the cause that you have not the same values. It's hard to say without running example

jarwin123 commented 8 years ago

in pllua_pgfunc.c:143 rows for (i=0; inumargs; ++i){ fcinfo.arg[i] = luaP_todatum(L, fi->argtypes[i], 0, &fcinfo.argnull[i], i+1); } I debug luaP_todatum function, In plluaapi.c:1205 rows, { luaP_Datum *d = luaP_toudata(L, idx, PLLUA_DATUM); if (d == NULL) elog(ERROR, "[pllua]: raw datum expected for datum conversion, got %s", lua_typename(L, lua_type(L, idx))); dat = datumcopy(d->datum, ti); }

i modfiy plluaapi.c:1205 rows else { luaP_Datum _d = luaP_toudata(L, idx, PLLUA_DATUM); globlevarlena = *(struct varlena)d->datum; bytea *p = DatumGetByteaP(d->datum); globle_ptr = VARDATA(p); if (d == NULL) elog(ERROR, "[pllua]: raw datum expected for datum conversion, got %s", lua_typename(L, lua_type(L, idx))); dat = datumcopy(d->datum, ti); }

I found d->datum is the same ,but (struct varlena )d->datum is changed. i cann't got it when this memory was free?

Breakpoint 1, luaP_todatum (L=0x1cf647b0, type=5090, typmod=0, isnull=0x7fffe3a0b6a0 "", idx=1, fi=...) at plluaapi.c:1231 $125 = {issaved = 0, datum = 486767288, ti = 0x1d450c58} $126 = {vllen = "\360\001\000", vl_dat = 0x2b9d756d3fc4 ""} $127 = (nbe_list *) 0x1d037abc $128 = {uhd = {endian = 0, packed = 1, type = 2, m = 0, state = 0, uid = 601, offset = 0, tail = 120, avail = 0}, element_min = 20, count = 4, size = 4096}

Breakpoint 1, luaP_todatum (L=0x1cf647b0, type=5090, typmod=0, isnull=0x7fffe3a0b6a0 "", idx=1, fi=...) at plluaapi.c:1231 $129 = {issaved = 0, datum = 486767288, ti = 0x1d450c58} $130 = {vllen = "\177\177\177\177", vl_dat = 0x2b9d756d3fc4 ""} $131 = (nbe_list *) 0x1d03670c $132 = {uhd = {endian = 1, packed = 1, type = 7, m = 1, state = 1, uid = 3967, offset = 2039, tail = 2139062143, avail = 2139062143}, element_min = 32639, count = 2139062143, size = 2139062 143} in pllua_pgfunc.c:143 rows

for (i=0; inumargs; ++i){ fcinfo.arg[i] = luaP_todatum(L, fi->argtypes[i], 0, &fcinfo.argnull[i], i+1); }

I debug luaP_todatum function, In plluaapi.c:1205 rows, { luaP_Datum d = luaP_toudata(L, idx, PLLUA_DATUM); if (d == NULL) elog(ERROR, "[pllua]: raw datum expected for datum conversion, got %s", lua_typename(L, lua_type(L, idx))); dat = datumcopy(d->datum, ti); } I found d->datum is the same ,but (struct varlena *)(d->datum) is changed. i cann't got it when this memory was free?

Breakpoint 1, luaP_todatum (L=0x1cf647b0, type=5090, typmod=0, isnull=0x7fffe3a0b6a0 "", idx=1, fi=...) at plluaapi.c:1231 $125 = {issaved = 0, datum = 486767288, ti = 0x1d450c58} $126 = {vllen = "\360\001\000", vl_dat = 0x2b9d756d3fc4 ""} $127 = (nbe_list *) 0x1d037abc $128 = {uhd = {endian = 0, packed = 1, type = 2, m = 0, state = 0, uid = 601, offset = 0, tail = 120, avail = 0}, element_min = 20, count = 4, size = 4096}

Breakpoint 1, luaP_todatum (L=0x1cf647b0, type=5090, typmod=0, isnull=0x7fffe3a0b6a0 "", idx=1, fi=...) at plluaapi.c:1231 $129 = {issaved = 0, datum = 486767288, ti = 0x1d450c58} $130 = {vllen = "\177\177\177\177", vl_dat = 0x2b9d756d3fc4 ""} $131 = (nbe_list *) 0x1d03670c $132 = {uhd = {endian = 1, packed = 1, type = 7, m = 1, state = 1, uid = 3967, offset = 2039, tail = 2139062143, avail = 2139062143}, element_min = 32639, count = 2139062143, size = 2139062 143}

$125 and $129 value is d->datum = 486767288,but (struct varlena )(d->datum) value is not same. why?

eugwne commented 8 years ago

What is postgres type of nbe_list ?

jarwin123 commented 8 years ago

nbe_list is my defined structure

jarwin123 commented 8 years ago

@eugwne can you debug it througth teamviewer? do you have skype number? my skype:kingkiller869

jarwin123 commented 8 years ago

@eugwne ,i found the macro RESET_CONTEXT_AFTER value will cause my prog segfault mistake,but i cann't got why?

define RESET_CONTEXT_AFTER 1000

define RESET_CONTEXT_AFTER 1000000

eugwne commented 8 years ago

When transaction is over, transaction context released. But in case of doing something like:

local func = pg_func('...')
for i = 1, bigValue do
     func(...)
end

you may become run out of memory, that is why there is temporary context which is dropped after counter. In your case the result of previous function call was dropped because next calls increased that counter. I think that it's better to make this option configurable from lua somehow.

jarwin123 commented 8 years ago

Is this an empirical value? if the value is not appropriate,the postgresql will segment,but the value is too large,it may become run out of memory?i want to know,why the value is 1000 ?the value was based on what? thank you!!!

eugwne commented 8 years ago

No, it's not based on anything. If you make a million function calls inside one transaction without resetting context should see growing memory consuming during that process.

jarwin123 commented 8 years ago

but if the value is not appropriate, the programmer will be segfault? have some ways to check the address is using?

eugwne commented 8 years ago

I have a couple ideas for this, but they are not perfect. Needs some time...

jarwin123 commented 8 years ago

I am waiting for your good news.!!!

jarwin123 commented 8 years ago

have some good news?

eugwne commented 8 years ago

If you have small transactions I think we can turn off autocleanup and use transaction context. I've made this changes in my repo, but haven't tested yet. In other case lua gc should be mixed somehow with postgres memory pools.