Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i share composite types across postgres and plpython

I have a composite type called tt to be used by all my plpgsql and plpythonu procedures. is there some kind of plpy. means of accessing the catalogue or schema in a consistent way so as to derive types or iterable structs to return without having to define the class in the plpythonu procedure?

CREATE TYPE tt AS (id integer, name text);
CREATE OR REPLACE FUNCTION python_setof_type() RETURNS SETOF tt AS $$
#-- i want this to be dynamic or to have it's attributes pulled from the tt type
class tt:
    def __init__(self, id, name):
        plpy.info('constructed type')
        self.idx = 0
        self.id = id
        self.name = name

    def __iter__ (self):
        return self

    def next (self):
        if (self.idx == 1):
            raise StopIteration

        self.idx += 1
        return ( self )

return tt(3, 'somename')

#-- was hoping for something like
#-- give me 1 record
#-- return plpy.schema.tt(3, 'somename')

#-- give me 2 records
#-- return plpy.schema.tt([3, 'somename'], [1, 'someornothername'])

#-- give me a no records
#-- return plpy.schema.tt([])
$$ LANGUAGE plpythonu;
like image 965
usbdongle Avatar asked Mar 07 '26 17:03

usbdongle


1 Answers

There isn't anything built in like that, but it's a neat idea. You could probably write it yourself, by querying the system catalogs, as you indicated.

like image 143
Peter Eisentraut Avatar answered Mar 10 '26 15:03

Peter Eisentraut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!