Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7998363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:11:55+00:00 2026-06-04T15:11:55+00:00

Yesterday we had a PostgreSQL database upgraded to version 9.1.3. We thought we had

  • 0

Yesterday we had a PostgreSQL database upgraded to version 9.1.3. We thought we had everything tested and ready, but there is a function we missed. It returns a table type like this:

CREATE OR REPLACE FUNCTION myfunc(
  patient_number varchar,
  tumor_number_param varchar,
  facility_number varchar
)
RETURNS SETOF patient_for_registrar
LANGUAGE plpgsql
AS
$body$
BEGIN
  RETURN QUERY          
     
  SELECT cast(nfa.patient_id_number as varchar),
  ...

I only only give the first column of the select because that is where the error happens. Before today this function ran fine, but now it gives this error:

ERROR: structure of query does not match function result type
Detail: Returned type character varying does not match expected type
character varying(8) in column 1. Where: PL/pgSQL function
"getwebregistrarpatient_withdeletes" line 3 at RETURN QUERY [SQL
State=42804]

The column nfa.patient_id_number is text and is being cast for the column patient_id_number in patient_for_registrar that is varchar(8). After reading about this some I think the problem is because the column length isn’t being specified when casting from text. But the problem is I’ve tried various combinations of substrings to fix this and none are solving the problem:

substring(cast(nfa.patient_id_number as varchar) from 1 for 8),

cast(substring(nfa.patient_id_number from 1 for 8) as varchar),

cast(substring(nfa.patient_id_number from 1 for 8) as varchar(8)),

Does anyone have any pointers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:11:56+00:00Added an answer on June 4, 2026 at 3:11 pm

    Your function ..

    RETURNS SETOF patient_for_registrar
    

    The returned row type must match the declared type exactly. You did not disclose the definition of patient_for_registrar, probably the associated composite type of a table. I quote the manual about Declaration of Composite Types:

    Whenever you create a table, a composite type is also automatically
    created, with the same name as the table, to represent the table’s row
    type.

    If the first column of that type (table) is defined varchar(8) (with length modifier) – as the error message indicates, you have to return varchar(8) with the same length modifier; varchar won’t do. It is irrelevant for that matter whether the string length is only 8 characters, the data type has to match.

    varchar, varchar(n) and varchar(m) are different data types for PostgreSQL.

    Older versions did not enforce the type modifiers, but with PostgreSQL 9.0 this was changed for plpgsql:

    PL/pgSQL now requires columns of composite results to match the
    expected type modifier as well as base type (Pavel Stehule, Tom Lane)

    For example, if a column of the result type is declared as
    NUMERIC(30,2), it is no longer acceptable to return a NUMERIC of some
    other precision in that column. Previous versions neglected to check
    the type modifier and would thus allow result rows that didn’t
    actually conform to the declared restrictions.

    Two basic ways to fix your problem:

    • You can cast the returned values to match the definition of patient_for_registrar:

      nfa.patient_id_number::varchar(8)
      
    • Or you can change the RETURNS clause. I would use RETURNS TABLE and declare a matching composite type. Here is an example.

      RETURNS TABLE (patient_for_registrar varchar, col2 some_type, ...)
      

    As an aside: I never use varchar if I can avoid it – especially not with length modifier. It offers hardly anything that the type text couldn’t do. If I need a length restriction, I use a column constraint which can be changed without rewriting the whole table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After posting a question yesterday I thought I had this cleared up but I'm
Yesterday i had a question in an interview which i thought i could find
I had this code working yesterday, but it seems like I edited it a
Yesterday I had a few question about OO and classes in PHP here but
Yesterday I had problems with another boost functions but luckily you guys helped me
I had my code work fine yesterday, but today I can no longer get
I had the latest version of OSX installed on my computer yesterday and I've
Following yesterday's question, I did some research and I thought I had a more
I had started the Rails app yesterday and got everything working. Then today I
I had put an question in StackOverflow yesterday but could not get the answer.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.