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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:31:35+00:00 2026-05-26T15:31:35+00:00

I’m trying to come up with a PostgreSQL schema for host data that’s currently

  • 0

I’m trying to come up with a PostgreSQL schema for host data that’s currently in an LDAP store. Part of that data is the list of hostnames a machine can have, and that attribute is generally the key that most people use to find the host records.

One thing I’d like to get out of moving this data to an RDBMS is the ability to set a uniqueness constraint on the hostname column so that duplicate hostnames can’t be assigned. This would be easy if hosts could only have one name, but since they can have more than one it’s more complicated.

I realize that the fully-normalized way to do this would be to have a hostnames table with a foreign key pointing back to the hosts table, but I’d like to avoid having everybody need to do joins for even the simplest query:

select hostnames.name,hosts.*
  from hostnames,hosts
 where hostnames.name = 'foobar'
   and hostnames.host_id = hosts.id;

I figured using PostgreSQL arrays could work for this, and they certainly make the simple queries simple:

select * from hosts where names @> '{foobar}';

When I set a uniqueness constraint on the hostnames attribute, though, it of course treats the entire list of names as the unique value instead of each name. Is there a way to make each name unique across every row instead?

If not, does anyone know of another data-modeling approach that would make more sense?

  • 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-26T15:31:36+00:00Added an answer on May 26, 2026 at 3:31 pm

    The righteous path

    You might want to reconsider normalizing your schema. It is not necessary for everyone to "join for even the simplest query". Create a VIEW for that.

    Table could look like this:

    CREATE TABLE hostname (
      hostname_id serial PRIMARY KEY
    , host_id     int  REFERENCES host(host_id) ON UPDATE CASCADE ON DELETE CASCADE
    , hostname    text UNIQUE
    );
    

    The surrogate primary key hostname_id is optional. I prefer to have one. In your case hostname could be the primary key. But many operations are faster with a simple, small integer key. Create a foreign key constraint to link to the table host.
    Create a view like this:

    CREATE VIEW v_host AS
    SELECT h.*
         , array_agg(hn.hostname) AS hostnames
    --   , string_agg(hn.hostname, ', ') AS hostnames  -- text instead of array
    FROM   host h
    JOIN   hostname hn USING (host_id)
    GROUP  BY h.host_id;   -- works in v9.1+
    

    Starting with pg 9.1, the primary key in the GROUP BY covers all columns of that table in the SELECT list. The release notes for version 9.1:

    Allow non-GROUP BY columns in the query target list when the primary
    key is specified in the GROUP BY clause

    Queries can use the view like a table. Searching for a hostname will be much faster this way:

    SELECT *
    FROM   host h
    JOIN   hostname hn USING (host_id)
    WHERE  hn.hostname = 'foobar';
    

    Provided you have an index on host(host_id), which should be the case as it should be the primary key. Plus, the UNIQUE constraint on hostname(hostname) implements the other needed index automatically.

    In Postgres 9.2+ a multicolumn index would be even better if you can get an index-only scan out of it:

    CREATE INDEX hn_multi_idx ON hostname (hostname, host_id);
    

    Starting with Postgres 9.3, you could use a MATERIALIZED VIEW, circumstances permitting. Especially if you read much more often than you write to the table.

    The dark side (what you actually asked)

    If I can’t convince you of the righteous path, here is some assistance for the dark side:

    Here is a demo how to enforce uniqueness of hostnames. I use a table hostname to collect hostnames and a trigger on the table host to keep it up to date. Unique violations raise an exception and abort the operation.

    CREATE TABLE host(hostnames text[]);
    CREATE TABLE hostname(hostname text PRIMARY KEY);  --  pk enforces uniqueness
    

    Trigger function:

    CREATE OR REPLACE FUNCTION trg_host_insupdelbef()
      RETURNS trigger
      LANGUAGE plpgsql AS
    $func$
    BEGIN
       -- split UPDATE into DELETE & INSERT
       IF TG_OP = 'UPDATE' THEN
          IF OLD.hostnames IS DISTINCT FROM NEW.hostnames THEN -- keep going
          ELSE
             RETURN NEW;  -- exit, nothing to do
          END IF;
       END IF;
    
       IF TG_OP IN ('DELETE', 'UPDATE') THEN
          DELETE FROM hostname h
          USING  unnest(OLD.hostnames) d(x)
          WHERE  h.hostname = d.x;
    
          IF TG_OP = 'DELETE' THEN RETURN OLD;  -- exit, we are done
          END IF;
       END IF;
    
       -- control only reaches here for INSERT or UPDATE (with actual changes)
       INSERT INTO hostname(hostname)
       SELECT h
       FROM   unnest(NEW.hostnames) h;
    
       RETURN NEW;
    END
    $func$;
    

    Trigger:

    CREATE TRIGGER host_insupdelbef
    BEFORE INSERT OR DELETE OR UPDATE OF hostnames ON host
    FOR EACH ROW EXECUTE FUNCTION trg_host_insupdelbef();
    

    SQL Fiddle with test run.

    Use a GIN index on the array column host.hostnames and array operators to work with it:

    • Why isn't my PostgreSQL array index getting used (Rails 4)?
    • Check if any of a given array of values are present in a Postgres array
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from

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.