Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

215
Vistas
How to write a function that return modified row type in PostgreSQL

I want to decide whether to hide some of the fields based on the value of the field:

CREATE FUNCTION hide(messages) RETURNS messages AS
$$
SELECT CASE
           WHEN $1.is_public THEN $1 -- modify only if "is_public" is false
           ELSE ROW (
               $1.id,
               $1.name, -- some other
               E'\\x00000000', -- hide this
               '', -- hide this
               '[]', -- hide this
               $1.created,
               $1.modified,
               )::messages END AS result;
$$ LANGUAGE SQL;

It works, but the maintainability is poor. Every time I change the messages table I have to change this function at the same time.

Is there a better way?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I assume your problem is having to keep adding public fields into the function if they are added to messages. You might have a little more flexibility at the cost of some speed with

CREATE FUNCTION hide(messages) RETURNS messages AS
$$
DECLARE
    retval messages;
BEGIN
    IF $1.is_public THEN
      retval = $1; -- copy
      SELECT 0, '', '[]' INTO retval.private_field1,
          retval.private_field2, retval.private_field3;
      RETURN retval;
    ELSE RETURN $1;
    END IF;
END
$$ LANGUAGE PLPGSQL;
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda