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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:50:18+00:00 2026-05-26T12:50:18+00:00

I am trying to make a self-managing partition table setup with Postgres. It all

  • 0

I am trying to make a self-managing partition table setup with Postgres. It all revolves around this function but I can’t seem to get Postgres to accept my table names. Any ideas or examples of self-managing partition table trigger functions?

My current function:

DECLARE
day integer;
year integer;
tablename text;
startdate text;
enddate text;
BEGIN
day:=date_part('doy',to_timestamp(NEW.date));
year:=date_part('year',to_timestamp(NEW.date));
tablename:='pings_'||year||'_'||day||'_'||NEW.id;
-- RAISE EXCEPTION 'tablename=%',tablename;
PERFORM 'tablename' FROM pg_tables WHERE 'schemaname'=tablename;
-- RAISE EXCEPTION 'found=%',FOUND;
IF FOUND <> TRUE THEN
    startdate:=date_part('year',to_timestamp(NEW.date))||'-'||date_part('month',to_timestamp(NEW.date))||'-'||date_part('day',to_timestamp(NEW.date));
    enddate:=startdate::timestamp + INTERVAL '1 day';
    EXECUTE 'CREATE TABLE $1 (
        CHECK ( date >= DATE $2 AND date < DATE $3 )
    ) INHERITS (pings)' USING quote_ident(tablename),startdate,enddate;
END IF;
EXECUTE 'INSERT INTO $1 VALUES (NEW.*)' USING quote_ident(tablename);
RETURN NULL;
END;

I want it to auto-create a table called pings_YEAR_DOY_ID but it always fails with:

2011-10-24 13:39:04 CDT [15804]: [1-1] ERROR:  invalid input syntax for type double precision: "-" at character 45
2011-10-24 13:39:04 CDT [15804]: [2-1] QUERY:  SELECT date_part('year',to_timestamp( $1 ))+'-'+date_part('month',to_timestamp( $2 ))+'-'+date_part('day',to_timestamp( $3 ))
2011-10-24 13:39:04 CDT [15804]: [3-1] CONTEXT:  PL/pgSQL function "ping_partition" line 15 at assignment
2011-10-24 13:39:04 CDT [15804]: [4-1] STATEMENT:  INSERT INTO pings VALUES (0,0,5);

TRY 2

After applying the changes and modifying it some more (date is a unixtimestamp column, my thinking being that an integer column is faster than a timestamp column when selecting). I get the below error, not sure if I am using the proper syntax for USING NEW?

Updated function:

CREATE FUNCTION ping_partition() RETURNS trigger
    LANGUAGE plpgsql
    AS $_$DECLARE
day integer;
year integer;
tablename text;
startdate text;
enddate text;
BEGIN
day:=date_part('doy',to_timestamp(NEW.date));
year:=date_part('year',to_timestamp(NEW.date));
tablename:='pings_'||year||'_'||day||'_'||NEW.id;
-- RAISE EXCEPTION 'tablename=%',tablename;
PERFORM 'tablename' FROM pg_tables WHERE 'schemaname'=tablename;
-- RAISE EXCEPTION 'found=%',FOUND;
IF FOUND <> TRUE THEN
    startdate := to_char(to_timestamp(NEW.date), 'YYYY-MM-DD');
    enddate:=startdate::timestamp + INTERVAL '1 day';
    EXECUTE 'CREATE TABLE ' || quote_ident(tablename) || ' (
        CHECK ( date >= EXTRACT(EPOCH FROM DATE ' || quote_literal(startdate) || ')
            AND date < EXTRACT(EPOCH FROM DATE ' || quote_literal(enddate) || ') )
    ) INHERITS (pings)';
END IF;
EXECUTE 'INSERT INTO ' || quote_ident(tablename) || ' SELECT $1' USING NEW; 
RETURN NULL;
END;
$_$;

My statement:

INSERT INTO pings VALUES (0,0,5);

SQL error:

ERROR:  column "date" is of type integer but expression is of type pings
LINE 1: INSERT INTO pings_1969_365_0 SELECT $1
                                            ^
HINT:  You will need to rewrite or cast the expression.
QUERY:  INSERT INTO pings_1969_365_0 SELECT $1
CONTEXT:  PL/pgSQL function "ping_partition" line 22 at EXECUTE statement
  • 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-26T12:50:18+00:00Added an answer on May 26, 2026 at 12:50 pm

    Note: Since Postgres 10 declarative partitioning is typically superior to partitioning by inheritance as used in this case.


    You are mixing double precision output of date_part() with text '-'. That doesn’t make sense to PostgreSQL. You would need an explicit cast to text. But there is a much simpler way to do all of this:

    startdate:=date_part('year',to_timestamp(NEW.date))
    ||'-'||date_part('month',to_timestamp(NEW.date))
    ||'-'||date_part('day',to_timestamp(NEW.date));

    Use instead:

    startdate := to_char(NEW.date, 'YYYY-MM-DD');
    

    This makes no sense either:

    EXECUTE 'CREATE TABLE $1 (
            CHECK (date >= DATE $2 AND date < DATE $3 )
        ) INHERITS (pings)' USING quote_ident(tablename),startdate,enddate;

    You can only supply values with the USING clause. Read the manual here. Try instead:

    EXECUTE 'CREATE TABLE ' || quote_ident(tablename) || ' (
                CHECK ("date" >= ''' || startdate || ''' AND
                       "date" <  ''' || enddate   || '''))
                INHERITS (ping)';
    

    Or better yet, use format(). See below.

    Also, like @a_horse answered: You need to put your text values in single quotes.

    Similar here:

    EXECUTE 'INSERT INTO $1 VALUES (NEW.*)' USING quote_ident(tablename);

    Instead:

    EXECUTE 'INSERT INTO ' || quote_ident(tablename) || ' VALUES ($1.*)'
    USING NEW;
    

    Related answer:

    • How to dynamically use TG_TABLE_NAME in PostgreSQL 8.2?

    Aside: While "date" is allowed for a column name in PostgreSQL it is a reserved word in every SQL standard. Don’t name your column "date", it leads to confusing syntax errors.

    Complete working demo

    CREATE TABLE ping (ping_id integer, the_date date);
    
    CREATE OR REPLACE FUNCTION trg_ping_partition()
      RETURNS trigger
      LANGUAGE plpgsql SET client_min_messages = 'WARNING' AS
    $func$
    DECLARE
       _schema text := 'public';  -- double-quoted if necessary
       _tbl text := to_char(NEW.the_date, '"ping_"YYYY_DDD_') || NEW.ping_id;
    BEGIN
       EXECUTE format('CREATE TABLE IF NOT EXISTS %1$s.%2$s
                       (CHECK (the_date >= %3$L
                           AND the_date <  %4$L)) INHERITS (%1$s.ping)'
               , _schema   -- %1$s
               , _tbl      -- %2$s  -- legal(!) name needs no quotes
               , to_char(NEW.the_date,     'YYYY-MM-DD')  -- %3$L
               , to_char(NEW.the_date + 1, 'YYYY-MM-DD')  -- %4$L
               );
    
       EXECUTE 'INSERT INTO ' || _tbl || ' VALUES ($1.*)'
       USING NEW; 
    
       RETURN NULL;
    END
    $func$;
    
    CREATE TRIGGER insbef
    BEFORE INSERT ON ping
    FOR EACH ROW EXECUTE FUNCTION trg_ping_partition();
    

    Postgres 9.1 added the clause IF NOT EXISTS for CREATE TABLE. See:

    • PostgreSQL create table if not exists

    Postgres 11 added the more appropriate syntax variant EXECUTE FUNCTION for triggers. Use EXECUTE PROCEDURE in older versions.
    See:

    • Trigger uses a procedure or a function?

    to_char() can take a date as $1. That’s converted to timestamp automatically. See:

    • The manual on date / time functions.

    I SET client_min_messages = 'WARNING' for the scope of the function to silence the flood of notices that would otherwise be raised on conflict by IF NOT EXISTS.

    Multiple other simplifications and improvements. Compare the code.

    Tests:

    INSERT INTO ping VALUES (1, now()::date);
    INSERT INTO ping VALUES (2, now()::date);
    INSERT INTO ping VALUES (2, now()::date + 1);
    INSERT INTO ping VALUES (2, now()::date + 1);
    

    fiddle
    OLD sqlfiddle

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

Sidebar

Related Questions

I've been trying to make my classes completely self contained, but I'm having some
I'm currently trying to make a self referencing table in MySQL, however it seems
I'm trying to make a landscape app that uses navigation controller, but I can't
Trying to make a make generic select control that I can dynamically add elements
trying to make a page which will recursively call a function until a limit
Im trying to make a function that will return an element of type point:
I'm trying to make a query that looks at a single table to see
I am trying to make a class with generic __init__ values, but have defaults
I am trying to make a before_save in a rails app conditional, but it
I am trying to make a combined image of all images added to a

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.