BUG #13575: invalid memory request size while storing call arguments into local variables - Mailing list pgsql-bugs

From greg.davidson+pg@gmail.com
Subject BUG #13575: invalid memory request size while storing call arguments into local variables
Date
Msg-id 20150816040111.2608.10374@wrigleys.postgresql.org
Whole thread Raw
Responses Re: BUG #13575: invalid memory request size while storing call arguments into local variables  (Peter Geoghegan <pg@heroku.com>)
List pgsql-bugs
The following bug has been logged on the website:

Bug reference:      13575
Logged by:          Greg Davidson
Email address:      greg.davidson+pg@gmail.com
PostgreSQL version: 9.5alpha2
Operating system:   Linux 3.13.0-37-generic #64-Ubuntu SMP x86_64
Description:

Both 4.5-alpha1 and 4.5-alpha2 give me some strange errors
when I try to build a database using some custom code which
converts an XML tree into some tables.  The database builds
fine under 4.4 and earlier almost identical versions built
fine going back at least to 4.1.

I hope that these errors give someone a clue as to where
there might be a problem in the alphas.  Unfortunately this
code is part of a large and complex system.  If the errors
persist in the first beta I'll try running PostgreSQL under
gdb and see if I can find the problem.

-- I initially got this error:

ERROR:  XX000: invalid memory alloc request size 18446744073642179576
CONTEXT:  PL/pgSQL function xml_tree_returns_id_pairs(xml_tree_returns[])
while storing call arguments into local variables
SQL function "try_new_xml_id_kind_tree" statement 1
SQL function "xml_tree" statement 1
LOCATION:  MemoryContextAlloc, mcxt.c:678

-- When I added some tracing code (debug_enter) the error changed to this

ERROR:  XX000: cache lookup failed for type 2139062143
CONTEXT:  PL/pgSQL function xml_tree_returns_id_pairs(xml_tree_returns[])
while storing call arguments into local variables
SQL function "try_new_xml_id_kind_tree" statement 1
SQL function "xml_tree" statement 1
LOCATION:  get_typlenbyvalalign, lsyscache.c:1932
make[1]: *** [simple.html-out] Error 3
make[1]: Leaving directory `/home/greg/Projects/Wicci/Core/S5_xml'
make: *** [rebuild-all] Error 1

-- Here's the text of the first function:

CREATE OR REPLACE
FUNCTION xml_tree_returns_id_pairs(xml_tree_returns[])  -- array_length($1,
1) = 2
RETURNS xml_id_node_pairs[] AS $$
DECLARE
    tree_ret xml_tree_returns;
    pairs xml_id_node_pairs[] := '{}';
    pair xml_id_node_pairs;
    this regprocedure
        := 'xml_tree_returns_id_pairs(xml_tree_returns[])';
BEGIN
    IF $1 IS NULL THEN
        RAISE EXCEPTION '%: NULL tree returns!', this;
    END IF;
    FOREACH tree_ret IN ARRAY $1 LOOP
        IF tree_ret.id_pairs IS NULL THEN
            RAISE EXCEPTION '%: NULL id_pair list!', this;
        END IF;
    pairs := pairs || tree_ret.id_pairs;
    END LOOP;
    FOREACH pair IN ARRAY pairs LOOP
        IF pair.id = '' THEN
            RAISE EXCEPTION '%: empty id on node %!',
                this, show_ref(doc_node_kind(pair.node));
        END IF;
    END LOOP;
    RETURN pairs;
END
$$ LANGUAGE plpgsql IMMUTABLE;

-- Here's the function which calls it:

CREATE OR REPLACE FUNCTION try_new_xml_id_kind_tree(
    _id xml_id_name_refs, xml_kind_returns,
    xml_tree_returns[] = '{}'
) RETURNS xml_tree_returns AS $$
    SELECT xml_tree_return(
        new_node,
        CASE WHEN $1 = '' THEN id_pairs
        ELSE xml_id_node_pair($1, new_node) || id_pairs END,
        ($2).ns_pairs || ns_pairs
    ) FROM
        debug_enter(
        'try_new_xml_id_kind_tree(
            xml_id_name_refs, xml_kind_returns,    xml_tree_returns[]
        )',
        array_length($3, 1), 'array_length($3)'            -- here's where it shows me the
length = 2
        ) this,
        new_xml_tree_node(
            ($2).kind, VARIADIC xml_tree_returns_nodes($3)
        ) new_node,
        xml_tree_returns_ns_pairs($3) ns_pairs,
        xml_tree_returns_id_pairs($3) id_pairs
$$ LANGUAGE sql STRICT;

-- And the function which calls that is:

CREATE OR REPLACE FUNCTION xml_tree(
    _id text, xml_kind_returns,
    VARIADIC xml_tree_returns[] = '{}'
) RETURNS xml_tree_returns AS $$
    SELECT new_xml_id_kind_tree( this, $1, $2, CASE ($2).tag
        WHEN html_tag('head') THEN
            $3 || xml_meta('html_head_extra')
        WHEN html_tag('body') THEN
            xml_meta('html_body_top') || $3 || xml_meta('html_body_extra')
        ELSE $3
    END ) FROM debug_enter(
        'xml_tree(text, xml_kind_returns,xml_tree_returns[])',
        array_length($3,1), '$3 array length',
        show_ref(($2).kind), 'node kind'
    ) this
$$ LANGUAGE sql;

-- The data structure details are:

CREATE TYPE xml_tree_returns AS (
    node doc_node_refs,
    id_pairs xml_id_node_pairs[],
    ns_pairs xml_prefix_uri_pairs[]
);

CREATE TABLE IF NOT EXISTS xml_id_node_pairs (
    id xml_id_name_refs NOT NULL        -- 1-word value
        REFERENCES xml_id_name_rows
        CHECK(NOT is_nil(id)),
    node doc_node_refs NOT NULL            -- 1-word value
        REFERENCES tree_doc_node_rows
        CHECK(NOT is_nil(node))
);

CREATE TABLE IF NOT EXISTS xml_prefix_uri_pairs (
    prefix xml_prefix_name_refs NOT NULL    -- 1-word value
        REFERENCES xml_prefix_name_rows,
    uri page_uri_refs NOT NULL                        -- 1-word value
        REFERENCES page_uri_rows
        CHECK(NOT is_nil(uri))
);

-- So we have some (not very large) arrays inside a tuple
-- inside of a larger array.

I hope that this bug report is useful!

_Greg

pgsql-bugs by date:

Previous
From: nlp.sr@shaw.ca
Date:
Subject: BUG #13573: Load Balancing
Next
From: Peter Geoghegan
Date:
Subject: Re: BUG #13575: invalid memory request size while storing call arguments into local variables