Postgresql - return column of referenced table that is not primary key I have tables:

CREATE TABLE users (
  id text PRIMARY KEY,
  internal_id serial,
  other columns...
)

CREATE TABLE listings (
  id bigserial PRIMARY KEY,
  created_at timestamptz default current_timestamp,
  posted_by text REFERENCES users ON UPDATE CASCADE ON DELETE CASCADE,
  other columns...
)


Using node-postgres, I am inserting a new record into table listings and returning the listings.id:

 pool.query(`INSERT INTO listings(${columns}) VALUES(${args}) \
           RETURNING listings.id`, [...values]);


In addition to listings.id, who could I also return users.internal_id?
You can just use a CTE for the insert, and then check the users table via SELECT:

WITH ins AS
(
INSERT INTO
    listings
    (posted_by, other_columns)
VALUES
    ('just_me', 'and that''s what I post')
RETURNING
    listings.id, posted_by
)
SELECT
    ins.id, users.internal_id
FROM
    ins 
    JOIN users ON users.id = ins.posted_by ;


dbfiddle here