:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(persons): return explicit person columns instead of * (#77351)
José Sequeira0e6c75ead4cd
diff --git a/nodejs/src/common/persons/repositories/postgres-person-repository.ts b/nodejs/src/common/persons/repositories/postgres-person-repository.tsindex 824a59d1dcab..9e194a4dcd34 100644--- a/nodejs/src/common/persons/repositories/postgres-person-repository.ts+++ b/nodejs/src/common/persons/repositories/postgres-person-repository.ts@@ -44,6 +44,25 @@ import { RawPostgresPersonRepository } from './raw-postgres-person-repository'const DEFAULT_PERSON_PROPERTIES_TRIM_TARGET_BYTES = 512 * 1024const DEFAULT_PERSON_PROPERTIES_DB_CONSTRAINT_LIMIT_BYTES = 655360+// Person write paths return these explicit columns instead of *: the persons+// schema carries personhog-only columns (is_deleted) that must not leak into+// ingestion's InternalPerson objects.+const PERSON_COLUMN_NAMES = [+ 'id',+ 'uuid',+ 'created_at',+ 'team_id',+ 'properties',+ 'properties_last_updated_at',+ 'properties_last_operation',+ 'is_user_id',+ 'version',+ 'is_identified',+ 'last_seen_at',+]+export const PERSON_COLUMNS = PERSON_COLUMN_NAMES.join(', ')+const PERSON_COLUMNS_PREFIXED = PERSON_COLUMN_NAMES.map((column) => `p.${column}`).join(', ')+function queryTag(base: string, callerTag?: string): string {return callerTag ? `${base}:${callerTag}` : base}@@ -581,7 +600,7 @@ export class PostgresPersonRepository`WITH inserted_person AS (INSERT INTO posthog_person (${columns.join(', ')})VALUES (${valuePlaceholders})- RETURNING *+ RETURNING ${PERSON_COLUMNS})` +distinctIdsCTE +` SELECT * FROM inserted_person;`@@ -1151,14 +1170,14 @@ export class PostgresPersonRepository).map((field, index) => `"${sanitizeSqlIdentifier(field)}" = $${index + 1}`)} WHERE id = $${idParamIndex} AND team_id = $${teamIdParamIndex}- RETURNING *, COALESCE(pg_column_size(properties)::bigint, 0::bigint) as properties_size_bytes+ RETURNING ${PERSON_COLUMNS}, COALESCE(pg_column_size(properties)::bigint, 0::bigint) as properties_size_bytes/* operation='updatePersonWithPropertiesSize',purpose='${tag || 'update'}' */`// Potentially overriding values badly if there was an update to the person after computing updateValues aboveconst queryString = `UPDATE posthog_person SET version = ${versionString}, ${Object.keys(update).map((field, index) => `"${sanitizeSqlIdentifier(field)}" = $${index + 1}`)} WHERE id = $${idParamIndex} AND team_id = $${teamIdParamIndex}- RETURNING *+ RETURNING ${PERSON_COLUMNS}/* operation='updatePerson',purpose='${tag || 'update'}' */`const shouldCalculatePropertiesSize =@@ -1238,7 +1257,7 @@ export class PostgresPersonRepositorylast_seen_at = $5,version = COALESCE(version, 0)::numeric + 1WHERE team_id = $6 AND uuid = $7 AND version = $8- RETURNING *+ RETURNING ${PERSON_COLUMNS}`,[JSON.stringify(finalProperties),@@ -1364,7 +1383,7 @@ export class PostgresPersonRepository$8::text[]) AS batch(batch_uuid, batch_team_id, new_properties, new_properties_last_updated_at, new_properties_last_operation, new_is_identified, new_created_at, new_last_seen_at)WHERE p.uuid = batch.batch_uuid AND p.team_id = batch.batch_team_id- RETURNING p.*+ RETURNING ${PERSON_COLUMNS_PREFIXED}`,[uuids,diff --git a/nodejs/src/common/persons/repositories/test-helpers.ts b/nodejs/src/common/persons/repositories/test-helpers.tsindex cee05fcec3bc..6dc815e84703 100644--- a/nodejs/src/common/persons/repositories/test-helpers.ts+++ b/nodejs/src/common/persons/repositories/test-helpers.ts@@ -1,5 +1,6 @@import { DateTime } from 'luxon'+import { PERSON_COLUMNS } from '~/common/persons/repositories/postgres-person-repository'import { PostgresRouter, PostgresUse } from '~/common/utils/db/postgres'import { InternalPerson, PersonDistinctId, PersonUpdateFields, RawPerson, Team } from '~/types'@@ -22,7 +23,9 @@ export async function getFirstTeam(postgres: PostgresRouter): Promise<Team> {export async function fetchPersons(postgres: PostgresRouter, teamId?: number): Promise<InternalPerson[]> {const query =- teamId !== undefined ? 'SELECT * FROM posthog_person WHERE team_id = $1' : 'SELECT * FROM posthog_person'+ teamId !== undefined+ ? `SELECT ${PERSON_COLUMNS} FROM posthog_person WHERE team_id = $1`+ : `SELECT ${PERSON_COLUMNS} FROM posthog_person`const params = teamId !== undefined ? [teamId] : undefinedreturn await postgres.query<RawPerson>(PostgresUse.PERSONS_WRITE, query, params, 'fetchPersons')diff --git a/nodejs/tests/helpers/sql.ts b/nodejs/tests/helpers/sql.tsindex 276e67966736..c7f18c8f4fb0 100644--- a/nodejs/tests/helpers/sql.ts+++ b/nodejs/tests/helpers/sql.ts@@ -1,6 +1,7 @@import { DateTime } from 'luxon'import { defaultConfig } from '~/common/config/config'+import { PERSON_COLUMNS } from '~/common/persons/repositories/postgres-person-repository'import { PostgresRouter, PostgresUse } from '~/common/utils/db/postgres'import { UUIDT } from '~/common/utils/utils'@@ -475,7 +476,7 @@ export const createOrganizationMembership = async (pg: PostgresRouter, organizat}export async function fetchPostgresPersons(postgres: PostgresRouter, teamId: number) {- const query = `SELECT * FROM posthog_person WHERE team_id = ${teamId} ORDER BY id`+ const query = `SELECT ${PERSON_COLUMNS} FROM posthog_person WHERE team_id = ${teamId} ORDER BY id`return (await postgres.query(PostgresUse.PERSONS_READ, query, undefined, 'persons')).rows.map(// NOTE: we map to update some values here to maintain// compatibility with `hub.fetchPersons`.