@@ -233,10 +233,13 @@ private void syncCommand() { |
233 | 233 | writeReadyForQueryMessage(); |
234 | 234 | } |
235 | 235 |
|
236 | | - private void flushCommand() { |
| 236 | + private void flushCommand() throws IOException { |
237 | 237 | if (DEBUG) |
238 | 238 | LogManager.instance().log(this, Level.INFO, "PSQL: flush (thread=%s)", Thread.currentThread().getId()); |
239 | | - writeReadyForQueryMessage(); |
| 239 | + // Flush message does NOT generate any response according to PostgreSQL protocol. |
| 240 | + // It just forces the backend to deliver any data pending in its output buffers. |
| 241 | + // See: https://www.postgresql.org/docs/current/protocol-message-formats.html |
| 242 | + channel.flush(); |
240 | 243 | } |
241 | 244 |
|
242 | 245 | private void closeCommand() throws IOException { |
@@ -283,14 +286,32 @@ private void describeCommand() throws IOException { |
283 | 286 | portal.cachedResultSet = browseAndCacheResultSet(resultSet, 0); |
284 | 287 | portal.columns = getColumns(portal.cachedResultSet); |
285 | 288 | writeRowDescription(portal.columns); |
| 289 | + portal.rowDescriptionSent = true; |
286 | 290 | } else |
287 | 291 | writeNoData(); |
288 | 292 | } else { |
289 | | - if (portal.columns != null) |
| 293 | + if (portal.columns != null) { |
290 | 294 | writeRowDescription(portal.columns); |
| 295 | + portal.rowDescriptionSent = true; |
| 296 | + } |
291 | 297 | } |
292 | 298 | } else if (type == 'S') { |
293 | | - writeNoData(); |
| 299 | + // Describe Statement: send ParameterDescription followed by RowDescription/NoData |
| 300 | + // This tells the client how many parameters the prepared statement expects |
| 301 | + writeParameterDescription(portal); |
| 302 | + |
| 303 | + // Now send RowDescription or NoData |
| 304 | + // For SELECT queries, we need to determine the columns from the type schema |
| 305 | + if (portal.isExpectingResult && portal.columns == null) { |
| 306 | + portal.columns = getColumnsFromQuerySchema(portal.query); |
| 307 | + } |
| 308 | + |
| 309 | + if (portal.columns != null && !portal.columns.isEmpty()) { |
| 310 | + writeRowDescription(portal.columns); |
| 311 | + portal.rowDescriptionSent = true; |
| 312 | + } else { |
| 313 | + writeNoData(); |
| 314 | + } |
294 | 315 | } else |
295 | 316 | throw new PostgresProtocolException("Unexpected describe type '" + type + "'"); |
296 | 317 | } |
@@ -328,23 +349,40 @@ private void executeCommand() { |
328 | 349 | portal.executed = true; |
329 | 350 | if (portal.isExpectingResult) { |
330 | 351 | portal.cachedResultSet = browseAndCacheResultSet(resultSet, limit); |
331 | | - portal.columns = getColumns(portal.cachedResultSet); |
332 | | - writeRowDescription(portal.columns); |
| 352 | + // Only send RowDescription if not already sent during DESCRIBE |
| 353 | + // But always use columns from actual result for DataRows consistency |
| 354 | + if (!portal.rowDescriptionSent) { |
| 355 | + portal.columns = getColumns(portal.cachedResultSet); |
| 356 | + writeRowDescription(portal.columns); |
| 357 | + portal.rowDescriptionSent = true; |
| 358 | + } |
333 | 359 | } |
334 | 360 | } |
335 | 361 |
|
336 | 362 | if (portal.isExpectingResult) { |
337 | | - if (portal.columns == null) |
338 | | - portal.columns = getColumns(portal.cachedResultSet); |
| 363 | + // Always use columns from actual result to ensure DataRows match |
| 364 | + final Map<String, PostgresType> dataRowColumns = getColumns(portal.cachedResultSet); |
| 365 | + |
| 366 | + // Verify column count matches what was sent in RowDescription |
| 367 | + if (portal.columns != null && portal.columns.size() != dataRowColumns.size()) { |
| 368 | + // Column count mismatch - use the original columns from DESCRIBE |
| 369 | + // This can happen if sample query returned different properties than actual query |
| 370 | + if (DEBUG) |
| 371 | + LogManager.instance().log(this, Level.WARNING, |
| 372 | + "PSQL: Column count mismatch - RowDesc=%d, DataRow=%d (thread=%s)", |
| 373 | + portal.columns.size(), dataRowColumns.size(), Thread.currentThread().threadId()); |
| 374 | + } |
339 | 375 |
|
340 | | - writeDataRows(portal.cachedResultSet, portal.columns); |
| 376 | + // Use the columns that were sent in RowDescription for consistency |
| 377 | + final Map<String, PostgresType> columnsToUse = portal.columns != null ? portal.columns : dataRowColumns; |
| 378 | + writeDataRows(portal.cachedResultSet, columnsToUse); |
341 | 379 | writeCommandComplete(portal.query, portal.cachedResultSet == null ? 0 : portal.cachedResultSet.size()); |
342 | 380 | } else |
343 | 381 | writeNoData(); |
344 | 382 | } |
345 | 383 | } catch (final CommandParsingException e) { |
346 | 384 | setErrorInTx(); |
347 | | - writeError(ERROR_SEVERITY.ERROR, "Syntax error on executing query: " + e.getCause().getMessage(), "42601"); |
| 385 | + writeError(ERROR_SEVERITY.ERROR, "Syntax error on executing query: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), "42601"); |
348 | 386 | } catch (final Exception e) { |
349 | 387 | setErrorInTx(); |
350 | 388 | writeError(ERROR_SEVERITY.ERROR, "Error on executing query: " + e.getMessage(), "XX000"); |
@@ -402,7 +440,7 @@ else if (query.query.equalsIgnoreCase("BEGIN") || |
402 | 440 |
|
403 | 441 | } catch (final CommandParsingException e) { |
404 | 442 | setErrorInTx(); |
405 | | - writeError(ERROR_SEVERITY.ERROR, "Syntax error on executing query: " + e.getCause().getMessage(), "42601"); |
| 443 | + writeError(ERROR_SEVERITY.ERROR, "Syntax error on executing query: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), "42601"); |
406 | 444 | } catch (final Exception e) { |
407 | 445 | setErrorInTx(); |
408 | 446 | writeError(ERROR_SEVERITY.ERROR, "Error on executing query: " + e.getMessage(), "XX000"); |
@@ -504,6 +542,87 @@ private Map<String, PostgresType> getColumns(final List<Result> resultSet) { |
504 | 542 | return columns; |
505 | 543 | } |
506 | 544 |
|
| 545 | + /** |
| 546 | + * Extract column schema from a SELECT query by parsing the type name and querying for a sample row. |
| 547 | + * This is used during DESCRIBE Statement to return RowDescription before the query is executed. |
| 548 | + * ArcadeDB is schema-less so we need to query actual data to discover dynamically-added properties. |
| 549 | + */ |
| 550 | + private Map<String, PostgresType> getColumnsFromQuerySchema(final String query) { |
| 551 | + if (query == null || query.isEmpty()) { |
| 552 | + return null; |
| 553 | + } |
| 554 | + |
| 555 | + // Try to extract the type name from the query |
| 556 | + // Patterns: "SELECT FROM TypeName", "SELECT * FROM TypeName", "SELECT ... FROM TypeName" |
| 557 | + final String upperQuery = query.toUpperCase(); |
| 558 | + final int fromIndex = upperQuery.indexOf(" FROM "); |
| 559 | + if (fromIndex < 0) { |
| 560 | + return null; |
| 561 | + } |
| 562 | + |
| 563 | + String afterFrom = query.substring(fromIndex + 6).trim(); |
| 564 | + |
| 565 | + // Extract type name (ends at WHERE, LIMIT, ORDER, or end of string) |
| 566 | + String typeName = afterFrom; |
| 567 | + for (String terminator : new String[]{" WHERE ", " LIMIT ", " ORDER ", " GROUP ", ";"}) { |
| 568 | + final int idx = typeName.toUpperCase().indexOf(terminator); |
| 569 | + if (idx > 0) { |
| 570 | + typeName = typeName.substring(0, idx); |
| 571 | + } |
| 572 | + } |
| 573 | + typeName = typeName.trim(); |
| 574 | + |
| 575 | + // Skip schema: prefix if present |
| 576 | + if (typeName.toLowerCase().startsWith("schema:")) { |
| 577 | + return null; // Schema queries have different structure |
| 578 | + } |
| 579 | + |
| 580 | + try { |
| 581 | + // First verify the type exists |
| 582 | + final DocumentType docType = database.getSchema().getType(typeName); |
| 583 | + if (docType == null) { |
| 584 | + return null; |
| 585 | + } |
| 586 | + |
| 587 | + // Query for a sample row to discover all properties (including dynamically-added ones) |
| 588 | + // Use LIMIT 1 to minimize overhead |
| 589 | + final String sampleQuery = "SELECT FROM " + typeName + " LIMIT 1"; |
| 590 | + final ResultSet resultSet = database.query("sql", sampleQuery, server.getConfiguration()); |
| 591 | + final List<Result> sampleRows = browseAndCacheResultSet(resultSet, 1); |
| 592 | + |
| 593 | + if (!sampleRows.isEmpty()) { |
| 594 | + // Use the sample row to discover columns |
| 595 | + return getColumns(sampleRows); |
| 596 | + } |
| 597 | + |
| 598 | + // If no rows exist, fall back to schema-defined properties |
| 599 | + final Map<String, PostgresType> columns = new LinkedHashMap<>(); |
| 600 | + |
| 601 | + // Add system properties first (these are returned for document/vertex types) |
| 602 | + columns.put(RID_PROPERTY, PostgresType.VARCHAR); |
| 603 | + columns.put(TYPE_PROPERTY, PostgresType.VARCHAR); |
| 604 | + columns.put(CAT_PROPERTY, PostgresType.CHAR); |
| 605 | + |
| 606 | + // Add all defined properties from the type |
| 607 | + for (final String propName : docType.getPropertyNames()) { |
| 608 | + final com.arcadedb.schema.Property prop = docType.getProperty(propName); |
| 609 | + if (prop != null && prop.getType() != null) { |
| 610 | + columns.put(propName, PostgresType.getTypeFromArcade(prop.getType())); |
| 611 | + } else { |
| 612 | + columns.put(propName, PostgresType.VARCHAR); |
| 613 | + } |
| 614 | + } |
| 615 | + |
| 616 | + return columns; |
| 617 | + |
| 618 | + } catch (Exception e) { |
| 619 | + if (DEBUG) |
| 620 | + LogManager.instance().log(this, Level.WARNING, "PSQL: failed to get columns from schema for query '%s': %s", |
| 621 | + query, e.getMessage()); |
| 622 | + return null; |
| 623 | + } |
| 624 | + } |
| 625 | + |
507 | 626 | private void writeRowDescription(final Map<String, PostgresType> columns) { |
508 | 627 | if (columns == null) |
509 | 628 | return; |
@@ -618,7 +737,13 @@ private void bindCommand() { |
618 | 737 | final String portalName = readString(); |
619 | 738 | final String sourcePreparedStatement = readString(); |
620 | 739 |
|
621 | | - final PostgresPortal portal = getPortal(portalName, false); |
| 740 | + // Look up the prepared statement (stored during PARSE) |
| 741 | + // The portal name may be different (often empty for unnamed portal) |
| 742 | + PostgresPortal portal = getPortal(sourcePreparedStatement, false); |
| 743 | + if (portal == null) { |
| 744 | + // Try with portal name as fallback for backwards compatibility |
| 745 | + portal = getPortal(portalName, false); |
| 746 | + } |
622 | 747 | if (portal == null) { |
623 | 748 | writeMessage("bind complete", null, '2', 4); |
624 | 749 | return; |
@@ -630,6 +755,9 @@ private void bindCommand() { |
630 | 755 | Thread.currentThread().getId()); |
631 | 756 |
|
632 | 757 | final int paramFormatCount = channel.readShort(); |
| 758 | + if (DEBUG) |
| 759 | + LogManager.instance().log(this, Level.INFO, "PSQL: bind paramFormatCount=%d (thread=%s)", |
| 760 | + paramFormatCount, Thread.currentThread().getId()); |
633 | 761 | if (paramFormatCount > 0) { |
634 | 762 | portal.parameterFormats = new ArrayList<>(paramFormatCount); |
635 | 763 | for (int i = 0; i < paramFormatCount; i++) { |
@@ -639,12 +767,21 @@ private void bindCommand() { |
639 | 767 | } |
640 | 768 |
|
641 | 769 | final int paramValuesCount = channel.readShort(); |
| 770 | + if (DEBUG) |
| 771 | + LogManager.instance().log(this, Level.INFO, "PSQL: bind paramValuesCount=%d (thread=%s)", |
| 772 | + paramValuesCount, Thread.currentThread().getId()); |
642 | 773 | if (paramValuesCount > 0) { |
643 | 774 | portal.parameterValues = new ArrayList<>(paramValuesCount); |
644 | 775 | for (int i = 0; i < paramValuesCount; i++) { |
| 776 | + if (DEBUG) |
| 777 | + LogManager.instance().log(this, Level.INFO, "PSQL: bind reading param %d size (thread=%s)", i, Thread.currentThread().threadId()); |
645 | 778 | final long paramSize = channel.readUnsignedInt(); |
| 779 | + if (DEBUG) |
| 780 | + LogManager.instance().log(this, Level.INFO, "PSQL: bind param %d size=%d (thread=%s)", i, paramSize, Thread.currentThread().threadId()); |
646 | 781 | final byte[] paramValue = new byte[(int) paramSize]; |
647 | 782 | channel.readBytes(paramValue); |
| 783 | + if (DEBUG) |
| 784 | + LogManager.instance().log(this, Level.INFO, "PSQL: bind param %d value read (thread=%s)", i, Thread.currentThread().threadId()); |
648 | 785 |
|
649 | 786 | // Determine format code according to PostgreSQL protocol: |
650 | 787 | // - If paramFormatCount == 0: all parameters use text format (0) |
@@ -664,10 +801,17 @@ private void bindCommand() { |
664 | 801 | ? portal.parameterTypes.get(i) |
665 | 802 | : 0L; // UNSPECIFIED type |
666 | 803 |
|
| 804 | + if (DEBUG) |
| 805 | + LogManager.instance().log(this, Level.INFO, "PSQL: bind deserializing param %d typeCode=%d formatCode=%d (thread=%s)", |
| 806 | + i, typeCode, formatCode, Thread.currentThread().getId()); |
667 | 807 | portal.parameterValues.add(PostgresType.deserialize(typeCode, formatCode, paramValue)); |
| 808 | + if (DEBUG) |
| 809 | + LogManager.instance().log(this, Level.INFO, "PSQL: bind param %d deserialized (thread=%s)", i, Thread.currentThread().threadId()); |
668 | 810 | } |
669 | 811 | } |
670 | 812 |
|
| 813 | + if (DEBUG) |
| 814 | + LogManager.instance().log(this, Level.INFO, "PSQL: bind reading resultFormatCount (thread=%s)", Thread.currentThread().threadId()); |
671 | 815 | final int resultFormatCount = channel.readShort(); |
672 | 816 | if (resultFormatCount > 0) { |
673 | 817 | portal.resultFormats = new ArrayList<>(resultFormatCount); |
@@ -680,6 +824,16 @@ private void bindCommand() { |
680 | 824 | if (errorInTransaction) |
681 | 825 | return; |
682 | 826 |
|
| 827 | + // Store the portal under the portal name (which may be empty for unnamed portal) |
| 828 | + // This is necessary because EXECUTE looks up portals by portal name, not prepared statement name |
| 829 | + // PostgreSQL protocol: PARSE creates "prepared statement", BIND creates "portal" from it |
| 830 | + if (!portalName.equals(sourcePreparedStatement)) { |
| 831 | + portals.put(portalName, portal); |
| 832 | + if (DEBUG) |
| 833 | + LogManager.instance().log(this, Level.INFO, "PSQL: bind stored portal under name '%s' (thread=%s)", |
| 834 | + portalName, Thread.currentThread().getId()); |
| 835 | + } |
| 836 | + |
683 | 837 | writeMessage("bind complete", null, '2', 4); |
684 | 838 |
|
685 | 839 | } catch (final Exception e) { |
@@ -704,12 +858,25 @@ private void parseCommand() { |
704 | 858 | final long param = channel.readUnsignedInt(); |
705 | 859 | portal.parameterTypes.add(param); |
706 | 860 | } |
| 861 | + } else { |
| 862 | + // Client sent paramCount=0 (e.g., asyncpg, node-postgres) |
| 863 | + // Detect $N placeholders in the query to determine actual parameter count |
| 864 | + final int detectedParams = detectParameterPlaceholders(query.query); |
| 865 | + if (detectedParams > 0) { |
| 866 | + portal.parameterTypes = new ArrayList<>(detectedParams); |
| 867 | + for (int i = 0; i < detectedParams; i++) { |
| 868 | + // Use VARCHAR (OID 1043) as default type instead of 0 (unspecified) |
| 869 | + // This prevents asyncpg from trying to introspect unknown types via pg_type |
| 870 | + portal.parameterTypes.add((long) PostgresType.VARCHAR.code); |
| 871 | + } |
| 872 | + } |
707 | 873 | } |
708 | 874 |
|
| 875 | + final int actualParamCount = portal.parameterTypes != null ? portal.parameterTypes.size() : 0; |
709 | 876 | if (DEBUG) |
710 | 877 | LogManager.instance() |
711 | | - .log(this, Level.INFO, "PSQL: parse (portal=%s) -> %s (params=%d) (errorInTransaction=%s thread=%s)", portalName, |
712 | | - portal.query, paramCount, errorInTransaction, Thread.currentThread().getId()); |
| 878 | + .log(this, Level.INFO, "PSQL: parse (portal=%s) -> %s (params=%d, detected=%d) (errorInTransaction=%s thread=%s)", |
| 879 | + portalName, portal.query, paramCount, actualParamCount, errorInTransaction, Thread.currentThread().threadId()); |
713 | 880 |
|
714 | 881 | if (errorInTransaction) |
715 | 882 | return; |
@@ -895,7 +1062,7 @@ private void parseCommand() { |
895 | 1062 |
|
896 | 1063 | } catch (final CommandParsingException e) { |
897 | 1064 | setErrorInTx(); |
898 | | - writeError(ERROR_SEVERITY.ERROR, "Syntax error on parsing query: " + e.getCause().getMessage(), "42601"); |
| 1065 | + writeError(ERROR_SEVERITY.ERROR, "Syntax error on parsing query: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), "42601"); |
899 | 1066 | } catch (final Exception e) { |
900 | 1067 | setErrorInTx(); |
901 | 1068 | writeError(ERROR_SEVERITY.ERROR, "Error on parsing query: " + e.getMessage(), "XX000"); |
@@ -1215,6 +1382,43 @@ private void writeNoData() { |
1215 | 1382 | writeMessage("no data", null, 'n', 4); |
1216 | 1383 | } |
1217 | 1384 |
|
| 1385 | + /** |
| 1386 | + * Writes ParameterDescription message ('t') describing the parameters of a prepared statement. |
| 1387 | + * This is required by the PostgreSQL extended query protocol for DESCRIBE 'S' (Statement). |
| 1388 | + */ |
| 1389 | + private void writeParameterDescription(final PostgresPortal portal) { |
| 1390 | + final int paramCount = portal.parameterTypes != null ? portal.parameterTypes.size() : 0; |
| 1391 | + // Message format: 't' + int32 length + int16 param count + int32[] type OIDs |
| 1392 | + final int messageLength = 4 + 2 + (paramCount * 4); |
| 1393 | + |
| 1394 | + writeMessage("parameter description", () -> { |
| 1395 | + channel.writeShort((short) paramCount); |
| 1396 | + if (portal.parameterTypes != null) { |
| 1397 | + for (final Long typeOid : portal.parameterTypes) { |
| 1398 | + channel.writeUnsignedInt(typeOid != null ? typeOid.intValue() : 0); // 0 = unspecified type |
| 1399 | + } |
| 1400 | + } |
| 1401 | + }, 't', messageLength); |
| 1402 | + } |
| 1403 | + |
| 1404 | + /** |
| 1405 | + * Detects $N style parameter placeholders in a query and returns the count. |
| 1406 | + * PostgreSQL uses $1, $2, etc. for positional parameters. |
| 1407 | + * Returns the highest parameter number found (e.g., "$3" returns 3). |
| 1408 | + */ |
| 1409 | + private int detectParameterPlaceholders(final String query) { |
| 1410 | + int maxParam = 0; |
| 1411 | + final Pattern pattern = Pattern.compile("\\$(\\d+)"); |
| 1412 | + final Matcher matcher = pattern.matcher(query); |
| 1413 | + while (matcher.find()) { |
| 1414 | + final int paramNum = Integer.parseInt(matcher.group(1)); |
| 1415 | + if (paramNum > maxParam) { |
| 1416 | + maxParam = paramNum; |
| 1417 | + } |
| 1418 | + } |
| 1419 | + return maxParam; |
| 1420 | + } |
| 1421 | + |
1218 | 1422 | private PostgresPortal getPortal(final String name, final boolean remove) { |
1219 | 1423 | if (remove) |
1220 | 1424 | return portals.remove(name); |
|