Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b9dac4a

Browse files
committed
Use the correct tuplestore read pointer in a NamedTuplestoreScan.
Tom Kazimiers reported that transition tables don't work correctly when they are scanned by more than one executor node. That's because commit 18ce3a4 allocated separate read pointers for each executor node, as it must, but failed to make them active at the appropriate times. Repair. Thomas Munro Discussion: https://postgr.es/m/20180224034748.bixarv6632vbxgeb%40dewberry.localdomain
1 parent d6ff2e3 commit b9dac4a

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/backend/executor/nodeNamedtuplestorescan.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ NamedTuplestoreScanNext(NamedTuplestoreScanState *node)
4040
* Get the next tuple from tuplestore. Return NULL if no more tuples.
4141
*/
4242
slot = node->ss.ss_ScanTupleSlot;
43+
tuplestore_select_read_pointer(node->relation, node->readptr);
4344
(void) tuplestore_gettupleslot(node->relation, true, false, slot);
4445
return slot;
4546
}
@@ -116,6 +117,7 @@ ExecInitNamedTuplestoreScan(NamedTuplestoreScan *node, EState *estate, int eflag
116117
* The new read pointer copies its position from read pointer 0, which
117118
* could be anywhere, so explicitly rewind it.
118119
*/
120+
tuplestore_select_read_pointer(scanstate->relation, scanstate->readptr);
119121
tuplestore_rescan(scanstate->relation);
120122

121123
/*

src/test/regress/expected/plpgsql.out

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5985,6 +5985,28 @@ LINE 1: SELECT (SELECT string_agg(id || '=' || name, ',') FROM d)
59855985
QUERY: SELECT (SELECT string_agg(id || '=' || name, ',') FROM d)
59865986
CONTEXT: PL/pgSQL function alter_table_under_transition_tables_upd_func() line 3 at RAISE
59875987
--
5988+
-- Test multiple reference to a transition table
5989+
--
5990+
CREATE TABLE multi_test (i int);
5991+
INSERT INTO multi_test VALUES (1);
5992+
CREATE OR REPLACE FUNCTION multi_test_trig() RETURNS trigger
5993+
LANGUAGE plpgsql AS $$
5994+
BEGIN
5995+
RAISE NOTICE 'count = %', (SELECT COUNT(*) FROM new_test);
5996+
RAISE NOTICE 'count union = %',
5997+
(SELECT COUNT(*)
5998+
FROM (SELECT * FROM new_test UNION ALL SELECT * FROM new_test) ss);
5999+
RETURN NULL;
6000+
END$$;
6001+
CREATE TRIGGER my_trigger AFTER UPDATE ON multi_test
6002+
REFERENCING NEW TABLE AS new_test OLD TABLE as old_test
6003+
FOR EACH STATEMENT EXECUTE PROCEDURE multi_test_trig();
6004+
UPDATE multi_test SET i = i;
6005+
NOTICE: count = 1
6006+
NOTICE: count union = 2
6007+
DROP TABLE multi_test;
6008+
DROP FUNCTION multi_test_trig();
6009+
--
59886010
-- Check type parsing and record fetching from partitioned tables
59896011
--
59906012
CREATE TABLE partitioned_table (a int, b text) PARTITION BY LIST (a);

src/test/regress/sql/plpgsql.sql

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4773,6 +4773,32 @@ ALTER TABLE alter_table_under_transition_tables
47734773
UPDATE alter_table_under_transition_tables
47744774
SET id = id;
47754775

4776+
--
4777+
-- Test multiple reference to a transition table
4778+
--
4779+
4780+
CREATE TABLE multi_test (i int);
4781+
INSERT INTO multi_test VALUES (1);
4782+
4783+
CREATE OR REPLACE FUNCTION multi_test_trig() RETURNS trigger
4784+
LANGUAGE plpgsql AS $$
4785+
BEGIN
4786+
RAISE NOTICE 'count = %', (SELECT COUNT(*) FROM new_test);
4787+
RAISE NOTICE 'count union = %',
4788+
(SELECT COUNT(*)
4789+
FROM (SELECT * FROM new_test UNION ALL SELECT * FROM new_test) ss);
4790+
RETURN NULL;
4791+
END$$;
4792+
4793+
CREATE TRIGGER my_trigger AFTER UPDATE ON multi_test
4794+
REFERENCING NEW TABLE AS new_test OLD TABLE as old_test
4795+
FOR EACH STATEMENT EXECUTE PROCEDURE multi_test_trig();
4796+
4797+
UPDATE multi_test SET i = i;
4798+
4799+
DROP TABLE multi_test;
4800+
DROP FUNCTION multi_test_trig();
4801+
47764802
--
47774803
-- Check type parsing and record fetching from partitioned tables
47784804
--

0 commit comments

Comments
 (0)