Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v15pY-004U4q-UV for pgsql-docs@arkaria.postgresql.org; Tue, 23 Sep 2025 16:25:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1v15pX-006vjw-6t for pgsql-docs@arkaria.postgresql.org; Tue, 23 Sep 2025 16:25:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v15pW-006vjo-QK for pgsql-docs@lists.postgresql.org; Tue, 23 Sep 2025 16:25:26 +0000 Received: from mail-pg1-x52b.google.com ([2607:f8b0:4864:20::52b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1v15pS-002PEr-1F for pgsql-docs@lists.postgresql.org; Tue, 23 Sep 2025 16:25:26 +0000 Received: by mail-pg1-x52b.google.com with SMTP id 41be03b00d2f7-b54dc768f11so4271139a12.0 for ; Tue, 23 Sep 2025 09:25:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=equatoria-us.20230601.gappssmtp.com; s=20230601; t=1758644720; x=1759249520; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=Hf/mJdqONm4x76D8CAoU3q6VAxamvrnlXQesnwfB4EY=; b=Z91j/6LPJOY1JhWGajfvmHlxmi9Bytme8dWgAd0dZTfyjMK3LwyNSltU4W4QY6SmOH kr4mElenr08xEGsBYgbZlzNKMKUWgngI2TaROTp1OfXdO7bbKV+fe83Jx8Xf7B6qEWfZ ISPpRKoiYkBPZ9qY/D8tLpTGYX/QjrAlFIm1hm1j+iexrICHVvB7Ub/P3OmHpiO/XHLf n25DrZPCCvLI6RgC/mmRNWxj9OFLxmOsi5U4zcqH6thmX7pI2ZDIdPD29R5CJghqUw8p 1d6vX6LERS0O/JpEYfJphtvSoCTkmx0aOCevYdbEutZZ3+hahsD1QluLUhDdqC6JVlp7 fkWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758644720; x=1759249520; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Hf/mJdqONm4x76D8CAoU3q6VAxamvrnlXQesnwfB4EY=; b=Ss0IAHkWMnpdYf8d4k6i90kMkKH5a8vtu2cnvTfZLqGCwAPNa3d6qn2DZHm4f1pyD8 j9EZVm3eICzJoAXGUTlq6muar0Uzu6YBIcH1PGFTVJtYhzgvg2azNBaqg9cdyGmR/LYw nMDWU+tSW5XO/sttctI+8OaGhJH97hseLksmL4v7hDN+KYCJmyvHA5Cr7n3Y30kJIQS9 mitp3qXC6rX64psTlKFjsU6AXGKN1a6RZHGvQcmljR++G1NQnzDz/Ml1r6qU8CHxiPWl x3jB1V6YB+He/oxvzHORv9hw3xZYw9g0pN7eMVY6/pZfERdLt8zFN/+2zYxHfeK0UTDM S7Zw== X-Gm-Message-State: AOJu0Yxdcbqc3EPfBIs20pmxuFongZlCCid9mc3OtMqCsA44LesG+rZa J7y3RGHehZY3vWX8SZdFdixr4B96LjNyXv477Vpq/rLYdZl+g9y3mZBni/kCoYDXlGB9klNMhAs shmx5p64TMlbB6V4W6NV6BazwfJ82ssKOMi2UbSmdR54rLxbyS1hg X-Gm-Gg: ASbGncs5I/9zs3Eon8rUnhkFXcGb8bwoPhLOxMT64iiR6G5HjsKsc9k6dTupGgnVtMU Cn7PVrk2y63XNVqL4/ARzrVqLr/Zcj6RrrBhX4+xuyr2cI6eC8yEYtomflayxcNfz/z5zDbFgG/ lw3Kq4y+BdOqKWFLHw9+bVumnSvlJHLpd95Ftg/vAik2+o1p4xk71h2FCSZ2Hn8k5xBPD8qH758 vjOIi7i3RpphqpqQo1HLZH/4lFP4QKzSmdcIDdD3wiSPRazP0NoOPfCdgG4kEiiAXq/iverXPlW e9pGaqBDDbaw4ryQohZiTdbO4Wez2lDf0Ltr X-Google-Smtp-Source: AGHT+IHMjle1ag63q9k0zoZAa9EMWwZskIisXPn/tcofpZwzr4ltMnnKHExLYuPdaoG3yEuRzeKj2/tKeKM9zdwKGEI= X-Received: by 2002:a17:90b:1f89:b0:32e:6fae:ba52 with SMTP id 98e67ed59e1d1-332a92d67bfmr4498920a91.6.1758644719956; Tue, 23 Sep 2025 09:25:19 -0700 (PDT) MIME-Version: 1.0 From: Kirk Parker Date: Tue, 23 Sep 2025 09:25:08 -0700 X-Gm-Features: AS18NWBFFEZ-IJLhj6UjRlCUuBVUQjExdhxZZrl4PMaq00Ayjj9P3s9wuJjJZaM Message-ID: Subject: DDL Partitionion Inheritance -- improved trigger function To: pgsql-docs@lists.postgresql.org Content-Type: multipart/alternative; boundary="000000000000d1c88c063f7a61b1" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000d1c88c063f7a61b1 Content-Type: text/plain; charset="UTF-8" I'm a big fan of maintenance-free functions. What would you think about adding the following as an alternative trigger function, or as a replacement for the current function, to https://www.postgresql.org/docs/current/ddl-partitioning.html#DDL-PARTITIONING-INHERITANCE-EXAMPLE , item #5? CREATE OR REPLACE FUNCTION measurement_insert_trigger() RETURNS TRIGGER AS $$ BEGIN EXECUTE format('INSERT INTO measurement_%s VALUES (NEW.*)', to_char( NEW.logdate, 'YYYYMM')); RETURN NULL; END; $$ LANGUAGE plpgsql; For the modest overhead of an extra call to to_char() and using EXECUTE rather than a literal INSERT, you get a trigger function that works forever. Given that the example anticipates one insert per city/day, it doesn't expect an extremely high rate of inserts where every microsecond counts. And yes, bad things happen if the partition table does not exist, but that's true of the other trigger functions shown here, too. --000000000000d1c88c063f7a61b1 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I'm a big fan of maintenance-fre= e functions.=C2=A0 What would you think about adding the following as an al= ternative trigger function, or as a replacement for the current function, t= o=C2=A0
https://= www.postgresql.org/docs/current/ddl-partitioning.html#DDL-PARTITIONING-INHE= RITANCE-EXAMPLE , item #5?=C2=A0=C2=A0

CREATE OR REPLACE FUNCT= ION measurement_insert_trigger()
RETURNS TRIGGER AS $$
= BEGIN
=C2=A0 =C2=A0 EXECUTE format('INSERT INTO measurement_%= s VALUES (NEW.*)', to_char( NEW.logdate, 'YYYYMM'));
= =C2=A0 =C2=A0 RETURN NULL;
END;
$$
LANGUAGE p= lpgsql;

For the modest overhead of an extr= a call to to_char() and using EXECUTE rather than a literal INSERT, you get= a trigger function=C2=A0that works forever. Given that the example anticip= ates one insert per city/day, it doesn't expect an extremely high rate = of inserts where every microsecond counts.

And yes, bad = things happen if the partition table does not exist, but that's true of= the other trigger functions shown here, too.


--000000000000d1c88c063f7a61b1--