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 857029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:20:05+00:00 2026-05-15T08:20:05+00:00

Problem String concatenation is slowing down a query: date(extract(YEAR FROM m.taken)||’-1-1′) d1, date(extract(YEAR FROM

  • 0

Problem

String concatenation is slowing down a query:

date(extract(YEAR FROM m.taken)||'-1-1') d1,
date(extract(YEAR FROM m.taken)||'-1-31') d2

This is realized in code as part of a string, which follows (where the p_ variables are integers, provided as input by end users):

date(extract(YEAR FROM m.taken)||''-'||p_month1||'-'||p_day1||''') d1,
date(extract(YEAR FROM m.taken)||''-'||p_month2||'-'||p_day2||''') d2

This part of the query runs in 3.2 seconds with the dates, and 1.5 seconds without, leading me to believe there is ample room for improvement.

The query’s total run time is under 10 seconds; am looking to bring the entire query down to about 2 or 3 seconds. A hardware upgrade has already happened. 😉

Version

PostgreSQL 8.4.4.

Question

What is a better way to create the date (presumably without concatenation)?

Update

This looks promising: PGTYPESdate_mdyjul

Many thanks!

  • 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-05-15T08:20:06+00:00Added an answer on May 15, 2026 at 8:20 am

    Wow. I’m surprised, but using the functions from this page – specifically the one to build a date value from three integers – which really do nothing more expose the internal C date functions, really is a lot faster. Benchmarking for me show that creating the dates that way was much faster.

    First one is the implementation of the “dateserial” function:

    postgres=# select to_date(a,1,3) 
    postgres-# from generate_series(100,1000000) as v(a);
    
    Time: 1365.851 ms
    
    postgres=# select (a::text||'-01-03')::date from 
    postgres-# generate_series(100,1000000) as v(a);
    
    Time: 3454.224 ms
    

    Full Solution

    Edit dateserial.c:

    #include "postgres.h"
    #include "utils/date.h"
    #include "utils/nabstime.h"
    
    #ifdef PG_MODULE_MAGIC
    PG_MODULE_MAGIC;
    #endif
    
    Datum dateserial(PG_FUNCTION_ARGS);
    
    PG_FUNCTION_INFO_V1 (dateserial);
    
    Datum
    dateserial(PG_FUNCTION_ARGS) {
      int32 p_year = PG_GETARG_INT32(0);
      int32 p_month = PG_GETARG_INT32(1);
      int32 p_day = PG_GETARG_INT32(2);
    
      PG_RETURN_DATEADT( date2j( p_year, p_month, p_day ) - POSTGRES_EPOCH_JDATE );
    }
    

    Edit Makefile:

    MODULES = dateserial
    PGXS := $(shell pg_config --pgxs)
    include $(PGXS)
    

    Edit inst.sh (optional):

    #!/bin/bash
    
    make clean && make && strip *.so && make install && /etc/init.d/postgresql-8.4 restart
    

    Run bash inst.sh.

    Create a SQL function dateserial:

    CREATE OR REPLACE FUNCTION dateserial(integer, integer, integer)
      RETURNS date AS
    '$libdir/dateserial', 'dateserial'
      LANGUAGE 'c' IMMUTABLE STRICT
      COST 1;
    ALTER FUNCTION dateserial(integer, integer, integer) OWNER TO postgres;
    

    Test the function:

    SELECT dateserial( 2007::int, 5, 5 )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to solve the following question which i can't get to work by
I would like to update my SQL lite database with the native update-method of

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.