Thread: BUG #18887: Inner join returns non-existent data.

BUG #18887: Inner join returns non-existent data.

From
PG Bug reporting form
Date:
The following bug has been logged on the website:

Bug reference:      18887
Logged by:          Freddy Aurelio Bernal Gil
Email address:      fabernal@gmail.com
PostgreSQL version: 16.8
Operating system:   UBUNTU
Description:

I have two tables that share the same primary key composed of three fields.
When I perform an inner join using all the key fields in the statement, it
returns non-existent values. The inner join shouldn't return any rows, but
instead it returns a row with a non-existent value.
Select rp.codactividadeconomica 
from p_contratos pc 
inner join remuner.remunerp_contratos rp 
on pc.codigoempresa = rp.codigoempresa 
and pc.concoanio = rp.concoanio 
and pc.concosecue = rp.concosecue
where pc.codigoempresa = 2 and pc.concoanio = 16 and pc.concosecue =
'017';

 codactividadeconomica 
-----------------------
 LEY_50
(1 fila)

The table remuner.remunerp_contratos don't have any data for the PK:
select count(*)
from remuner.remunerp_contratos pc
where pc.codigoempresa = 2 and pc.concoanio = 16 and pc.concosecue =
'017';
 count 
-------
     0
(1 fila)

This the ddl for the tables:
CREATE TABLE public.p_contratos (
    codigoempresa int2 NOT NULL,
    concoanio int4 NOT NULL,
    concosecue bpchar(5) NOT NULL,
    nitcliente numeric(12) NULL,
    codigosucursal int2 NULL,
    causervi int2 NULL,
    codigocentrocosto varchar(6) NULL,
    sector bpchar(2) DEFAULT ''::bpchar NULL,
    nomcontrato varchar(75) NULL,
    nomsubdivision varchar(50) DEFAULT ''::character varying NULL,
    fechainicio date NULL,
    fechafinal date NULL,
    codigousuario varchar(12) NULL,
    fechacreacion timestamp DEFAULT now() NULL,
    fechamodificacion timestamp NULL,
    habilitado bool DEFAULT true NULL,
    activado bool DEFAULT true NULL,
    moactivacion varchar(50) DEFAULT ''::character varying NULL,
    clasecontrato bpchar(1) NULL,
    nombreproyecto varchar(200) DEFAULT NULL::character varying NULL,
    duracioncontrato int2 DEFAULT 0 NULL,
    tipoventa int2 DEFAULT 0 NULL,
    fechapresentacotiza timestamp NULL,
    nombrecontacto varchar(100) DEFAULT NULL::character varying NULL,
    emailcontacto varchar(80) DEFAULT NULL::character varying NULL,
    telefonocontacto varchar(30) DEFAULT NULL::character varying NULL,
    ubicaciondane int4 NULL,
    tipofacturacion int2 DEFAULT 0 NULL,
    sucursalfactura int2 NULL,
    sena bool DEFAULT false NULL,
    esquemasena int2 DEFAULT 0 NULL,
    formafacturacionsena int2 DEFAULT 0 NULL,
    presupuestoaprobado numeric(12) DEFAULT 0 NULL,
    tiempopresupuesto int2 DEFAULT 0 NULL,
    reservaprestacional int2 DEFAULT 0 NULL,
    vacacionesdistrufadas bool DEFAULT false NULL,
    porcevacacionesdistrufadas numeric(10, 2) DEFAULT 0 NULL,
    modofacturacion int2 DEFAULT 0 NULL,
    requerimientosfacturacion varchar(200) NULL,
    condicionfacturacion int2 DEFAULT 0 NULL,
    frecuenciafacturacion int2 DEFAULT 0 NULL,
    formapagorecaudo int2 DEFAULT 0 NULL,
    bancorecaudo bpchar(3) DEFAULT ''::bpchar NULL,
    plazopagorecaudo int2 DEFAULT 0 NULL,
    exigefactoring bool DEFAULT false NULL,
    nombrefactoring varchar(200) NULL,
    porcefactoring numeric(10, 2) DEFAULT 0 NULL,
    estabilidadreforzada bool DEFAULT false NULL,
    manejapoliza bool DEFAULT false NULL,
    descripcionpolizas varchar(200) NULL,
    porcepoliza numeric(10, 2) DEFAULT 0 NULL,
    tiempopresentacioncandidato int2 DEFAULT 0 NULL,
    numerocandidatos int2 DEFAULT 0 NULL,
    clienterevisahv bool DEFAULT false NULL,
    aplicasaludocupacional bool DEFAULT false NULL,
    porcensaludocupacional numeric(10, 2) DEFAULT 0 NULL,
    aplicabienestar bool DEFAULT false NULL,
    porcenbienestar numeric(10, 2) DEFAULT 0 NULL,
    aplicagestionambiental bool DEFAULT false NULL,
    porcengestionambiental numeric(10, 2) DEFAULT 0 NULL,
    diasrespuestaterna int2 NULL,
    diascartera int2 NULL,
    admonprincipal numeric DEFAULT 0 NULL,
    admonotros numeric DEFAULT 0 NULL,
    obsadmonotros text NULL,
    contratocliente varchar(30) NULL,
    codigosucursalfactura int2 DEFAULT 0 NULL,
    sectorconsolidada bpchar(2) DEFAULT ''::bpchar NULL,
    facturacionconsolidada int2 DEFAULT 1 NULL,
    coddivision int2 DEFAULT 2 NULL,
    diapago1 int4 DEFAULT 15 NULL,
    diapago2 int4 DEFAULT 30 NULL,
    codatep int4 NULL,
    manejasyadoc bool DEFAULT false NULL,
    usuarioaprobacion varchar(20) NULL,
    fechaaprobacion timestamp(0) NULL,
    direccionradicafactura varchar(100) NULL,
    idtiponomina int4 NULL,
    idformapagocolab int4 NULL,
    codbancodispersa bpchar(3) DEFAULT ''::bpchar NULL,
    coordinadornomina varchar(20) NULL,
    idreportanomina int4 NULL,
    requiereaprobcliente bool DEFAULT false NULL,
    ultimodiarecibofact int4 DEFAULT 0 NULL,
    usuarioradicafact varchar(20) NULL,
    formatofactura int4 NULL,
    caracteristicasfactura varchar(2000) NULL,
    formapagofactura int4 NULL,
    codbcopagofactura bpchar(3) DEFAULT ''::bpchar NULL,
    usuariosac varchar(20) NULL,
    envioporemailplataforma varchar(10) NULL,
    emailenvio varchar(100) NULL,
    nitproveedortecnologico varchar(30) NULL,
    valorejecutado numeric(12) DEFAULT 0 NULL,
    valorneto numeric(12) DEFAULT 0 NULL,
    admonincapacidades bool DEFAULT true NULL,
    admonincpatrono bool DEFAULT true NULL,
    ajusteminimo bool DEFAULT true NULL,
    apladmon10 bool DEFAULT true NULL,
    idmodalidadreinversion int4 DEFAULT 4 NULL,
    valorreinversion numeric(3, 2) DEFAULT 0 NULL,
    pagaincpatrono100 bool DEFAULT true NULL,
    pagaincentidad100 bool DEFAULT false NULL,
    factincentidad bool DEFAULT false NULL,
    procesoslegales numeric(3, 2) DEFAULT 0 NULL,
    gastosadmi numeric(3, 2) DEFAULT 0 NULL,
    gastosfinan numeric(3, 2) DEFAULT 0 NULL,
    plan varchar(10) NULL,
    facturaanticipo bool DEFAULT false NULL,
    estabilidadlab bool DEFAULT false NULL,
    poliza bool DEFAULT false NULL,
    pagosegsoc bool DEFAULT false NULL,
    diahabil int2 DEFAULT 1 NULL,
    exclusiva bool DEFAULT false NULL,
    idfrecuencia int4 DEFAULT 0 NULL,
    vlrimplementacion numeric(12) DEFAULT 0 NULL,
    folio int4 DEFAULT 0 NULL,
    vlrfolio numeric(12) DEFAULT 0 NULL,
    almacenamiento int4 DEFAULT 0 NULL,
    vlrproductividad numeric(12) DEFAULT 0 NULL,
    cantidadfolio int4 DEFAULT 0 NULL,
    transporte bool DEFAULT false NULL,
    vlrtransporte numeric(12) DEFAULT 0 NULL,
    nooperativo bool DEFAULT false NULL,
    contratocomercial varchar(10) NULL,
    CONSTRAINT p_contratos_clasecontrato_check CHECK (((clasecontrato =
'C'::bpchar) OR (clasecontrato = 'O'::bpchar))),
    CONSTRAINT p_contratos_pkey PRIMARY KEY (codigoempresa, concoanio,
concosecue)
);
CREATE TABLE remuner.remunerp_contratos (
    codigoempresa int2 NOT NULL,
    concoanio int4 NOT NULL,
    concosecue varchar(5) NOT NULL,
    perdidadominicallnr bool DEFAULT false NULL,
    fechacalendariop date NULL,
    fechacortelegal date NULL,
    codactividadeconomica varchar(15) DEFAULT NULL::character varying NULL,
    procedimientoretefuente bpchar(1) DEFAULT NULL::bpchar NULL,
    primaproyectada bool DEFAULT false NULL,
    factsgsincapacidadpatrono bool DEFAULT false NULL,
    facturasgsincapacidadentidad bool DEFAULT false NULL,
    perdidadominicaausenciainjustificada bool DEFAULT false NULL,
    facturaprestacionesncapacidadpatrono bool DEFAULT false NULL,
    facturaprestacionesincapacidadentidad bool DEFAULT false NULL,
    cedulavendedor numeric(15) NULL,
    nombrevendedor varchar(200) NULL,
    codcentrocostoremuner int2 NULL,
    canalventa int4 NULL,
    admonpatrono bool DEFAULT true NULL,
    usuariocreacion varchar(20) NULL,
    fechacreacion timestamp NULL,
    usuariomodificacion varchar(20) NULL,
    fechamodificacion timestamp NULL,
    CONSTRAINT remunerp_contratos_codcentrocostoremuner_check CHECK
(((codcentrocostoremuner >= 0) AND (codcentrocostoremuner <= 9))),
    CONSTRAINT remunerp_contratos_pkey PRIMARY KEY (codigoempresa, concoanio,
concosecue)
);


Re: BUG #18887: Inner join returns non-existent data.

From
Tom Lane
Date:
PG Bug reporting form <noreply@postgresql.org> writes:
> I have two tables that share the same primary key composed of three fields.
> When I perform an inner join using all the key fields in the statement, it
> returns non-existent values. The inner join shouldn't return any rows, but
> instead it returns a row with a non-existent value.
> Select rp.codactividadeconomica 
> from p_contratos pc 
> inner join remuner.remunerp_contratos rp 
> on pc.codigoempresa = rp.codigoempresa 
> and pc.concoanio = rp.concoanio 
> and pc.concosecue = rp.concosecue
> where pc.codigoempresa = 2 and pc.concoanio = 16 and pc.concosecue =
> '017';

I'm inclined to guess that this is a symptom of a corrupted index;
does REINDEX help?

Since one of the columns in question is text (well, char(N)),
a plausible theory about what caused the corruption is an update
of the underlying system that resulted in a collation change.
See

https://wiki.postgresql.org/wiki/Locale_data_changes

            regards, tom lane