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IElOIHF1ZXJ5IExPT1ANCiAgICBz
dGF0ZW1lbnRzDQpFTkQgTE9PUDsNCg0KQnV0IGluIHByYWN0aWNlIG9ubHkg
cmVjb3JkIHR5cGUgY2FuIGJlIHRha2VuLg0KSXMgaXQgYnVnIG9yIEkndmUg
c29tZXRoaW5nIG1pc3NlZD8NCg0KQmVzdCByZWdhcmRzLCBEaW5hciBUYWxp
cG92DQoNCg0KDQoNCg0KDQoNCg==

Re: Record type and ROW type in pl/pgsql functions

From
Tom Lane
Date:
dinar@yantel.ru writes:
> declare
> hosts_str mail_relayhosts%ROWTYPE;
> ...
> FOR hosts_str IN  SELECT hosts from mail_relayhosts LOOP

You would need SELECT * FROM here, if you're selecting the whole row.
This is going to try to stuff "hosts" into the first column of the
rowtype variable, which is the wrong datatype.  (I think it would then
complain about too few columns supplied, but it's not getting that far.)

            regards, tom lane