Pointer to structure in ECPG

From: Ashutosh Bapat <ashutosh(dot)bapat(at)enterprisedb(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Pointer to structure in ECPG
Date: 2014-04-09 06:42:36
Message-ID: CAFjFpRciy0Dw5ZFxbLa=O0K=r=C6uRgAnON1RkA9CPVUEYxNhg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi All,
I tried to use pointer to array to fetch results of a query. The test case
test_select.pgc is attached. Changes specific to one's environment are
needed before that test can be tried. Otherwise, you may try file
pointer_to_struct.pgc in the patch attached, by putting it ecpg/test
directory.

The file test_select.pgc compiles fine but when run it produces error -203
and produces garbage because the result of FETCH was not saved in the array
pointed by pointer.

./test_select
SQL error: SQL error -203 on line 40
empno=-163754450, ename=, job=
empno=634004672, ename=�, job=
empno=634004624, ename=�, job=

This happens because of two problems
1. When dumping individual members of structure emp in test_select.pgc,
when a pointer to that structure is dumped, its array size is dumped as "1"
instead of "0" or a negative number like scalar variable pointers.

2. As discussed in other thread "using arrays within structure in ECPG",
the offsets of members of structures are not dumped correctly for an
unbounded array.

Problem 1 reason:
-------------------------
When dumping individual members of structure emp in test_select.pgc, when a
pointer to that structure is dumped, the call is routed through code path
as shown below

*#0* *ECPGdump_a_type* (o=0x6cc4f0, *name=0x6d2190 "empno"*,
type=0x6d2130, brace_level=-1, ind_name=0x4c0ae8 "no_indicator",
ind_type=0x6c8260, ind_brace_l
evel=-1, *prefix=0x6d1a20 "emp2->"*, ind_prefix=0x0, *arr_str_siz=0x6d2070
"0",* *struct_sizeof=0x6d0c60 "sizeof( struct employee )"*,
ind_struct_sizeof=0x0)
at type.c:247
*#1* 0x000000000042f21d in *ECPGdump_a_struct* (o=0x6cc4f0, *name=0x6d20b0
"emp2"*, ind_name=0x435c20 "no_indicator", *arrsiz=0x6d2070 "0"*,
type=0x6d20d0, in
d_type=0x6c8260, *prefix=0x6d1a20 "emp2->"*, ind_prefix=0x0) at type.c:572
*#2* 0x000000000042e2cc in *ECPGdump_a_type *(o=0x6cc4f0, *name=0x6d20b0
"emp2"*, type=0x6d22b0, brace_level=1, ind_name=0x435c20 "no_indicator",
ind_type=0
x6c8260, ind_brace_level=0, prefix=0x0, ind_prefix=0x0, *arr_str_siz=0x6d1e60
"0"*, *struct_sizeof=0x0*, ind_struct_sizeof=0x0) at type.c:293
*#3* 0x000000000043335d in *dump_variables* (list=0x6d2310, mode=1) at
variable.c:452

ECPGdump_a_type() at frame #3, then calls ECPGdump_a_simple() as follows
#0 *ECPGdump_a_simple* (o=0x6cc4f0, *name=0x6d2190 "empno"*,
type=ECPGt_int, varcharsize=0x6d0f70 "1",* arrsize=0x6d2360 "-1"*,
*siz=0x6d0c60 "sizeof( struct employee )"*, prefix=0x6d1a20 "emp2->",
counter=0) at type.c:405

ECPGdump_a_simple() while dumping the member (here empno) dumps with
arrsize = "1" instead of "-1" because of code at line
523│ if (atoi(arrsize) < 0)
524│ strcpy(arrsize, "1");

In ECPG run time library when reading this variable, it is read with array
size 1 and is falsely interpreted as an array with size 1 instead of an
unbounded array (which are read with arrsize < 0). Hence giving error -203.

ECPGdump_a_struct has correctly called ECPGdump_a_type with arrsize "0" to
mean an unbounded array, and ECPGdump_a_type() has correctly converted it
to "-1" again to differentiate between pointer to struct from pointer to
scalar variable. This differentiation is important to dump pointer to
struct and pointer to scalar variable differently in ECPGdump_a_simple().
So, in ECPGdump_a_simple() we should dump the variable with arrsize "-1" if
it's part of an outer structure.

The patch attached, includes solutions to both the problems and also a
testcase preproc/pointer_to_struct.pgc. This test is a copy of test
preproc/array_of_struct.pgc with the structure arrays replaced by pointers
to structures and memory allocations.

The patch is based on development head. Please consider this to be
backpatched to 9.3 as well.

--
Best Wishes,
Ashutosh Bapat
EnterpriseDB Corporation
The Postgres Database Company

Attachment Content-Type Size
test_select.pgc application/octet-stream 922 bytes
pg_ecpg_pointer.patch text/x-patch 29.6 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavan Deolasee 2014-04-09 06:44:06 Re: Autonomous Transaction (WIP)
Previous Message Heikki Linnakangas 2014-04-09 06:37:24 Re: default opclass for jsonb (was Re: Call for GIST/GIN/SP-GIST opclass documentation)