Skip to content

Commit

Permalink
Fix error on insert from table #32
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Orlov committed Dec 8, 2020
1 parent 4408d5a commit 1b37d66
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 18 deletions.
56 changes: 56 additions & 0 deletions expected/pg_variables.out
Original file line number Diff line number Diff line change
Expand Up @@ -929,3 +929,59 @@ SELECT * FROM pgv_list() order by package, name;
---------+------+------------------
(0 rows)

-- Check insert of record with various amount of fields
CREATE TEMP TABLE foo(id int, t text);
INSERT INTO foo VALUES (0, 'str00');
SELECT pgv_insert('vars', 'r1', row(1, 'str1'::text, 'str2'::text));
pgv_insert
------------

(1 row)

SELECT pgv_select('vars', 'r1');
pgv_select
---------------
(1,str1,str2)
(1 row)

SELECT pgv_insert('vars', 'r1', foo) FROM foo;
ERROR: new record structure have 2 attributes, but variable "r1" structure have 3.
SELECT pgv_select('vars', 'r1');
pgv_select
---------------
(1,str1,str2)
(1 row)

SELECT pgv_insert('vars', 'r2', row(1, 'str1'));
pgv_insert
------------

(1 row)

SELECT pgv_insert('vars', 'r2', foo) FROM foo;
ERROR: new record attribute type for attribute number 2 differs from variable "r2" structure. You may need explicit type casts.
SELECT pgv_select('vars', 'r2');
pgv_select
------------
(1,str1)
(1 row)

SELECT pgv_insert('vars', 'r3', row(1, 'str1'::text));
pgv_insert
------------

(1 row)

SELECT pgv_insert('vars', 'r3', foo) FROM foo;
pgv_insert
------------

(1 row)

SELECT pgv_select('vars', 'r3');
pgv_select
------------
(1,str1)
(0,str00)
(2 rows)

56 changes: 56 additions & 0 deletions expected/pg_variables_trans.out
Original file line number Diff line number Diff line change
Expand Up @@ -3773,3 +3773,59 @@ SELECT pgv_free();

(1 row)

---
--- Test case for issue #32 [PGPRO-4456]
---
CREATE TEMP TABLE tab (id int, t varchar);
INSERT INTO tab VALUES (0, 'str00');
SELECT pgv_insert('vars', 'r1', row(1, 'str1', 'str2'));
pgv_insert
------------

(1 row)

SELECT pgv_insert('vars', 'a', tab) FROM tab;
pgv_insert
------------

(1 row)

SELECT pgv_insert('vars', 'r1', tab) FROM tab;
ERROR: new record structure have 2 attributes, but variable "r1" structure have 3.
SELECT pgv_select('vars', 'r1');
pgv_select
---------------
(1,str1,str2)
(1 row)

SELECT pgv_insert('vars', 'r2', row(1, 'str1'::varchar));
pgv_insert
------------

(1 row)

SELECT pgv_insert('vars', 'b', tab) FROM tab;
pgv_insert
------------

(1 row)

SELECT pgv_insert('vars', 'r2', tab) FROM tab;
pgv_insert
------------

(1 row)

SELECT pgv_select('vars', 'r2');
pgv_select
------------
(1,str1)
(0,str00)
(2 rows)

SELECT pgv_free();
pgv_free
----------

(1 row)

23 changes: 5 additions & 18 deletions pg_variables.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ static MemoryContext ModuleContext = NULL;
static Package *LastPackage = NULL;
/* Recent variable */
static Variable *LastVariable = NULL;
/* Recent row type id */
static Oid LastTypeId = InvalidOid;

/* Saved hook values for recall */
static ExecutorEnd_hook_type prev_ExecutorEnd = NULL;
Expand Down Expand Up @@ -706,8 +704,7 @@ variable_insert(PG_FUNCTION_ARGS)
init_record(record, tupdesc, variable);
variable->is_deleted = false;
}
else if (LastTypeId == RECORDOID || !OidIsValid(LastTypeId) ||
LastTypeId != tupType)
else
{
/*
* We need to check attributes of the new row if this is a transient
Expand All @@ -716,8 +713,6 @@ variable_insert(PG_FUNCTION_ARGS)
check_attributes(variable, tupdesc);
}

LastTypeId = tupType;

insert_record(variable, rec);

/* Release resources */
Expand All @@ -742,6 +737,7 @@ variable_update(PG_FUNCTION_ARGS)
bool res;
Oid tupType;
int32 tupTypmod;
TupleDesc tupdesc = NULL;

/* Checks */
CHECK_ARGS_FOR_NULL();
Expand Down Expand Up @@ -794,17 +790,9 @@ variable_update(PG_FUNCTION_ARGS)
tupType = HeapTupleHeaderGetTypeId(rec);
tupTypmod = HeapTupleHeaderGetTypMod(rec);

if (LastTypeId == RECORDOID || !OidIsValid(LastTypeId) ||
LastTypeId != tupType)
{
TupleDesc tupdesc = NULL;

tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
check_attributes(variable, tupdesc);
ReleaseTupleDesc(tupdesc);
}

LastTypeId = tupType;
tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
check_attributes(variable, tupdesc);
ReleaseTupleDesc(tupdesc);

res = update_record(variable, rec);

Expand Down Expand Up @@ -1330,7 +1318,6 @@ resetVariablesCache(void)
/* Remove package and variable from cache */
LastPackage = NULL;
LastVariable = NULL;
LastTypeId = InvalidOid;
}

/*
Expand Down
16 changes: 16 additions & 0 deletions sql/pg_variables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,19 @@ SELECT pgv_free();
SELECT pgv_exists('vars');

SELECT * FROM pgv_list() order by package, name;
-- Check insert of record with various amount of fields
CREATE TEMP TABLE foo(id int, t text);
INSERT INTO foo VALUES (0, 'str00');

SELECT pgv_insert('vars', 'r1', row(1, 'str1'::text, 'str2'::text));
SELECT pgv_select('vars', 'r1');
SELECT pgv_insert('vars', 'r1', foo) FROM foo;
SELECT pgv_select('vars', 'r1');

SELECT pgv_insert('vars', 'r2', row(1, 'str1'));
SELECT pgv_insert('vars', 'r2', foo) FROM foo;
SELECT pgv_select('vars', 'r2');

SELECT pgv_insert('vars', 'r3', row(1, 'str1'::text));
SELECT pgv_insert('vars', 'r3', foo) FROM foo;
SELECT pgv_select('vars', 'r3');
18 changes: 18 additions & 0 deletions sql/pg_variables_trans.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1145,3 +1145,21 @@ COMMIT;

DROP VIEW pgv_stats_view;
SELECT pgv_free();

---
--- Test case for issue #32 [PGPRO-4456]
---
CREATE TEMP TABLE tab (id int, t varchar);
INSERT INTO tab VALUES (0, 'str00');

SELECT pgv_insert('vars', 'r1', row(1, 'str1', 'str2'));
SELECT pgv_insert('vars', 'a', tab) FROM tab;
SELECT pgv_insert('vars', 'r1', tab) FROM tab;
SELECT pgv_select('vars', 'r1');

SELECT pgv_insert('vars', 'r2', row(1, 'str1'::varchar));
SELECT pgv_insert('vars', 'b', tab) FROM tab;
SELECT pgv_insert('vars', 'r2', tab) FROM tab;
SELECT pgv_select('vars', 'r2');

SELECT pgv_free();

0 comments on commit 1b37d66

Please sign in to comment.