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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:29:17+00:00 2026-06-06T13:29:17+00:00

Background In a PostgreSQL 9.0 database, there are various tables that have many-to-many relationships.

  • 0

Background

In a PostgreSQL 9.0 database, there are various tables that have many-to-many relationships. The number of those relationships must be restricted. A couple of example tables include:

CREATE TABLE authentication (
  id bigserial NOT NULL, -- Primary key
  cookie character varying(64) NOT NULL, -- Authenticates the user with a cookie
  ip_address character varying(40) NOT NULL -- Device IP address (IPv6-friendly)
)

CREATE TABLE tag_comment (
  id bigserial NOT NULL, -- Primary key
  comment_id bigint, -- Foreign key to the comment table
  tag_name_id bigint -- Foreign key to the tag name table
)

Different relationships, however, have different limitations. For example, in the authentication table, a given ip_address is allowed 1024 cookie values; whereas, in the tag_comment table, each comment_id can have 10 associated tag_name_ids.

Problem

Currently, a number of functions have these restrictions hard-coded; scattering the limitations throughout the database, and preventing them from being changed dynamically.

Question

How would you impose a maximum many-to-many relationship limit on tables in a generic fashion?

Idea

Create a table to track the limits:

CREATE TABLE imposed_maximums (
  id serial NOT NULL,
  table_name  character varying(128) NOT NULL,
  column_group character varying(128) NOT NULL,
  column_count character varying(128) NOT NULL,
  max_size INTEGER
)

Establish the restrictions:

INSERT INTO imposed_maximums
  (table_name, column_group, column_count, max_size) VALUES
  ('authentication', 'ip_address', 'cookie', 1024);
INSERT INTO imposed_maximums
  (table_name, column_group, column_count, max_size) VALUES
  ('tag_comment', 'comment_id', 'tag_id', 10);

Create a trigger function:

CREATE OR REPLACE FUNCTION impose_maximum()
  RETURNS trigger AS
$BODY$
BEGIN
  -- Join this up with imposed_maximums somehow?
  select
    count(1)
  from
    -- the table name
  where
    -- the group column = NEW value to INSERT;

  RETURN NEW;
END;

Attach the trigger to every table:

CREATE TRIGGER trigger_authentication_impose_maximum
  BEFORE INSERT
  ON authentication
  FOR EACH ROW
  EXECUTE PROCEDURE impose_maximum();

Obviously it won’t work as written… is there a way to make it work, or otherwise enforce the restrictions such that they are:

  • in a single location; and
  • not hard-coded?

Thank you!

  • 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-06T13:29:18+00:00Added an answer on June 6, 2026 at 1:29 pm

    I’ve been doing a similar type of generic triggers.
    The most tricky part is to get the value entry in the NEW record based on the column name.

    I’m doing it the following way:

    • convert NEW data into array;
    • find the attnum of the column and use it as an index for the array.

    This approach works as long as there’re no commas in the data 🙁 I don’t know of other ways how to convert NEW or OLD variables into the array of values.

    The following function might help:

    CREATE OR REPLACE FUNCTION impose_maximum() RETURNS trigger AS $impose_maximum$
    DECLARE
      _sql  text;
      _cnt  int8;
      _vals text[];
      _anum int4;
      _im   record;
    
    BEGIN
     _vals := string_to_array(translate(trim(NEW::text), '()', ''), ',');
    
     FOR _im IN SELECT * FROM imposed_maximums WHERE table_name = TG_TABLE_NAME LOOP
      SELECT attnum INTO _anum FROM pg_catalog.pg_attribute a
        JOIN pg_catalog.pg_class t ON t.oid = a.attrelid
       WHERE t.relkind = 'r' AND t.relname = TG_TABLE_NAME
         AND NOT a.attisdropped AND a.attname = _im.column_group;
    
      _sql := 'SELECT count('||quote_ident(_im.column_count)||')'||
              ' FROM '||quote_ident(_im.table_name)||
              ' WHERE '||quote_ident(_im.column_group)||' = $1';
    
      EXECUTE _sql INTO _cnt USING _vals[_anum];
    
      IF _cnt > CAST(_im.max_size AS int8) THEN
        RAISE EXCEPTION 'Maximum of % hit for column % in table %(%=%)',
          _im.max_size, _im.column_count,
          _im.table_name, _im.column_group, _vals[_anum];
      END IF;
     END LOOP;
    
     RETURN NEW;
    END; $impose_maximum$ LANGUAGE plpgsql;
    

    This function will check for all conditions defined for a given table.

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

Sidebar

Related Questions

Background: I have a PostgreSQL (v8.3) database that is heavily optimized for OLTP. I
I have a backend process that maintains state in a PostgreSQL database, which needs
In PostgreSQL 8.4.8 database I have 2 tables: noreset and track . They have
My database background is with Oracle, so I was surprised to discover that Postgres
Background : Running a PostgreSQL database for a Django app (Django 1.1.1, Python2.4, psycopg2
I am from DW/BI background using SAS for many years now I have task
Who has any documents that about PostgreSQL background processes? I'd like to learn the
I have a piece of code that works in a background process which looks
I have been playing around with the postgresql.conf file for a couple days now.
A little background: I have a perl script which is performing a number 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.