Skip to content

sqlpage should close open transactions after each HTTP request #711

Open
@lovasoa

Description

@lovasoa

Discussed in #710

Originally posted by bever1337 November 25, 2024
Following example 11: https://sql.datapage.app/component.sql?component=form

I've created a sql script that creates a temporary table, copies to it, and then merges from the temporary table to the permanent table.

Typically, I use 'ON COMMIT DROP' when making temporary tables. On my second upload, sqlpage throws the error that my temporary table already exists. I kill the server, then put begin and commit as the first and last lines of my sql script. I test again, and get a similar error. Sometimes I receive postgres transaction warnings, ex beginning before committing.

Finally, I add commit; begin and commit as the first and last lines. This works, but means I've thrown away a transaction I wasn't managing.

How should users work with sqlpage transactions? Is there a way to align transactions with request lifecycles?

This pseudo code seems to work:

COMMIT;

BEGIN;

CREATE TEMPORARY TABLE example_temp (
  LIKE example
) ON COMMIT DROP;

COPY example_temp (my_id)
FROM
  'example_form_input' (FORMAT 'csv', HEADER);

MERGE INTO example from example_temp;

SELECT
  'redirect' AS component,
  '/example' AS link;

COMMIT;

Judiciously beginning and ending every sqlpage script with begin and commit negates the need for commit;begin; and commit; in some files. Not sure which is best.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions