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

  • Home
  • SEARCH
  • 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 8565709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:29:21+00:00 2026-06-11T17:29:21+00:00

I have a query where I need to call a SQL function to format

  • 0

I have a query where I need to call a SQL function to format a particular column in the query. The formatting needed is very similar to formatting a phone number, ie. changing 1234567890 into (123)456-7890.

I’ve read that calling a function from a select statement could be a performance killer, and it was kind of reflected in my situation, the time the query took more than tripled and I did not think the function would take this much longer. The function runs in linear time but does use SQL loops. To give an idea of the size of the database this particular query returns about 220,000 rows. The run time of the query went from < 3s to > 9s when running without calling the function vs. running calling the function. The column that needs formatting isn’t indexed or used in a join condition or where clause.

Is the performance drop here expected or is there something I can do to improve it?

This is the function in question:

CREATE OR REPLACE FUNCTION fn(bigint)
  RETURNS character varying LANGUAGE plpgsql AS
$BODY$
DECLARE
   v_chars varchar[];
   v_ret   varchar;
   v_length int4;
   v_count  int4;
BEGIN
   if ($1 isnull or $1 = 0) then
      return null;
   end if;

   v_chars  := regexp_split_to_array($1::varchar,'');
   v_ret    := '';
   v_length := array_upper (v_chars,1);
   v_count  := 0;

   for v_index in 1..11   loop
      v_count := v_count + 1;

      if (v_index <= v_length) then
         v_ret := v_chars[v_length - (v_index - 1)] || v_ret;
      else
         v_ret := '0' || v_ret;
      end if;

      if (v_count <= 6 and (v_count % 2) = 0) then
         v_ret := '.' || v_ret;
      end if;
   end loop;

   return v_ret;
END
$BODY$
  • 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-11T17:29:23+00:00Added an answer on June 11, 2026 at 5:29 pm

    It depends on the specifics of the function. To find out how much a bare function call will cost, create dummy functions like:

    CREATE FUNCTION f_bare_plpgsql(text)
      RETURNS text LANGUAGE plpgsql IMMUTABLE AS
    $BODY$
    BEGIN
       RETURN $1;
    END
    $BODY$;
    
    CREATE FUNCTION f_bare_sql(text)
      RETURNS text LANGUAGE sql IMMUTABLE AS
    $BODY$
       SELECT $1;
    $BODY$;
    

    And try your query again.
    If then you wonder why your function is slow, add it to your question.


    Solution for updated question

    Your function could be improved in many places, but there is a more radical solution:

    SELECT to_char(12345678901, '00000"."00"."00"."00')
    

    Many times faster, obviously. More about to_char() in the manual.
    Consider the following demo:

    WITH x(n) AS (
       VALUES  (1::bigint), (12), (123), (1234), (12345), (123456), (1234567)
              ,(12345678), (123456789), (1234567890), (12345678901), (123456789012)
       )
    SELECT n, x.fn(n), to_char(n, '00000"."00"."00"."00')
    FROM x
    
          n       |       fn       |     to_char
    --------------+----------------+-----------------
                1 | 00000.00.00.01 |  00000.00.00.01
               12 | 00000.00.00.12 |  00000.00.00.12
              123 | 00000.00.01.23 |  00000.00.01.23
             1234 | 00000.00.12.34 |  00000.00.12.34
            12345 | 00000.01.23.45 |  00000.01.23.45
           123456 | 00000.12.34.56 |  00000.12.34.56
          1234567 | 00001.23.45.67 |  00001.23.45.67
         12345678 | 00012.34.56.78 |  00012.34.56.78
        123456789 | 00123.45.67.89 |  00123.45.67.89
       1234567890 | 01234.56.78.90 |  01234.56.78.90
      12345678901 | 12345.67.89.01 |  12345.67.89.01
     123456789012 | 23456.78.90.12 |  #####.##.##.##
    

    to_char() is only prepared for up to 11 decimal digits, as you can see.
    Can easily be extended, if need should be.

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

Sidebar

Related Questions

Using Microsoft SQL server 2008 I have a query that is in need of
I have a SQL query that takes a very long time to run on
I have an existing stored procedure in SQL Server that I need to call
I have a query that I need to execute that I do not know
I need to have a query string in my crontab but it isn't working.
I need to have MySQL query like this one: UPDATE table_name SET 1 =
I have a broker query where I need to sort by 2 different fields
I have a query such that I need to get A specific dog All
I have a query being created using OLEDB from access and need to get
I have a query that does what i want joining table but i need

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.