Skip to main content
Glama

Upsert Row

db_upsert
DestructiveIdempotent

Insert a new row or update an existing one using PostgreSQL's ON CONFLICT. Specify a unique key filter, create values for new rows, and optional update values for matches.

Instructions

Inserts a new row or updates an existing one using PostgreSQL's ON CONFLICT mechanism. The 'where' filter's columns must match a unique constraint or primary key on the table — this is how PostgreSQL determines whether to insert or update. If a matching row exists, only the columns specified in 'update' are changed. If no match exists, a new row is created with the values from 'create'. Use dry_run=true to preview.

When to use:

  • "Create this user if they don't exist, otherwise update their last_login"

  • "Upsert product SKU-123 with price $29.99"

  • Idempotent insert-or-update operations

Parameter guidance:

  • table: the target table name (required)

  • where: JSON filter identifying the conflict target (required). Columns must match a unique constraint or primary key. Example: {"email": "jane@example.com"}

  • create: JSON object of column-value pairs for the INSERT case (required)

  • update: JSON object of column-value pairs for the UPDATE case (optional). If omitted, no update occurs on conflict — the existing row is returned unchanged.

  • dry_run: set to true to validate without writing (default: false)

Behavioral notes:

  • The where columns MUST match a unique constraint or primary key — the tool validates this against the schema and returns an error if no matching constraint exists.

  • Returns the final row (either newly inserted or updated).

  • This is idempotent — calling it multiple times with the same data has the same effect as calling it once.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
tableYesName of the table to query
whereYesJSON filter identifying the row, must match a unique constraint
createYesJSON object of column-value pairs to insert if not found
dryRunNoIf true, simulates without writing
updateNoJSON object of column-value pairs to set if found
databaseNoName of the database to query (from pgautopilot.json). Omit to use the current default database.
Install Server

TDQS

A4.6/5.0
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

The description adds substantial behavioral detail beyond the annotations: it explains the conflict-target constraint, the validation against schema constraints, the exact behavior when a match exists versus not, the return of the final row, and dry-run usage. This meaningfully informs an agent about side effects and safety.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is well-organized with clear sections and front-loaded mechanics. It is somewhat lengthy and repeats the dry-run and ON CONFLICT concepts, but the extra detail is mostly purposeful and improves usability.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a mutating tool with no output schema, this description is complete: it defines required parameters, conflict resolution, optional update behavior, dry-run preview, validation failure, returned value, and idempotency. Nothing critical to calling the tool correctly is missing.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the baseline is 3. The description adds rich guidance for table, where, create, update, and dry_run, including examples and conflict semantics. However, it refers to 'dry_run' while the schema property is 'dryRun', a naming mismatch that could confuse an agent.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's function: 'Inserts a new row or updates an existing one using PostgreSQL's ON CONFLICT mechanism.' This specific verb-plus-resource framing distinguishes it from sibling tools like db_create and db_update_many.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The 'When to use' section provides concrete examples and identifies idempotent insert-or-update operations as the target scenario. It does not explicitly state when not to use the tool or name alternatives, so it stops short of full exclusion guidance.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/cyberreinxy/pgautopilot'

If you have feedback or need assistance with the MCP directory API, please join our Discord server