Storing last N samples in a table coming from multiple clients
My idea is to use new table to store next index to update for each user:

CREATE TABLE next_sample_index (
  id     uuid     NOT NULL,
  index  integer  NOT NULL,

  PRIMARY KEY(id)
);


Just fill it with 0s for each user. I would use it as a kind of serial like this:

UPDATE
    next_sample_index
SET
    index = index + 1
WHERE
    id = XX
RETURNING
    index;


You will get next index and increase it at the same time.

Then you calculate modulus index % 50 so it acts as a round-robin queue.

To insert new sample:

DELETE FROM
    samples
WHERE
    id = XX AND
    index = --index-just-calculated--;


This DELETE command will do nothing if no such an index existed before.

INSERT INTO
    samples(
        id,
        index,
        text
    ) VALUES (
        XX,
        --index-just-calculated--,
        'f85c6f06-ae0b-48de-8c36-bfc86208495c'
    );


You just need to make sure that next_sample_index table is properly initialized or make a function to get next index that checks IF NOT FOUND condition (and makes INSERT if necessary).

XX in the examples is user id.

Hope this helps.